-
Notifications
You must be signed in to change notification settings - Fork 0
02_Routing
The Route
class is responsible for defining routes and their associated handlers in the application. It provides methods for specifying various types of HTTP routes such as GET, POST, etc. and mapping them to corresponding controller actions or closures.
To define a route, you can use one of the following methods on the Route
class:
-
Route::get($path, $controller, $prefix = null)
- Defines a route for the GET HTTP method. -
Route::post($path, $controller, $prefix = null)
- Defines a route for the POST HTTP method. -
Route::put($path, $controller, $prefix = null)
- Defines a route for the PUT HTTP method. -
Route::delete($path, $controller, $prefix = null)
- Defines a route for the DELETE HTTP method. -
Route::any($path, $controller, $prefix = null)
- Defines a route for any HTTP method. -
Route::options($path, $controller, $prefix = null)
- Defines a route for the OPTIONS HTTP method. -
Route::patch($path, $controller, $prefix = null)
- Defines a route for the PATCH HTTP method.
The $path
parameter specifies the path for the route. The $controller
parameter specifies the controller method to execute when the route is matched. The $prefix
parameter is an optional prefix to prepend to the path.
Route::get('/users', 'UserController@index');
Route::post('/users', 'UserController@store');
Route::put('/users/{id}', 'UserController@update');
Route::delete('/users/{id}', 'UserController@delete');
You can also create your routes as closures like this:
Route::get('/api', function () {
echo "Hello SyntoraPHP";
});
You can group related routes together and apply a common prefix to all of them using the group
method. The group
method takes two parameters: a prefix and a callback function that defines the routes within the group.
Route::group('/api', function () {
Route::get('/users', 'UserController@index');
Route::post('/users', 'UserController@store');
Route::put('/users/{id}', 'UserController@update');
Route::delete('/users/{id}', 'UserController@delete');
});
SyntoraPHP supports the use of controllers, enabling you to organize your API logic into separate classes for better code structure and maintainability.
To create a controller, you can create a new PHP file in the App/Controllers
directory of your SyntoraPHP project. The file should contain a class that extends the Controller
class.
namespace SyntoraPHP\App\Controllers;
class UserController
{
public function index()
{
// Handle GET request to /users
}
public function store()
{
// Handle POST request to /users
}
public function update($id)
{
// Handle PUT request to /users/{id}
}
public function delete($id)
{
// Handle DELETE request to /users/{id}
}
}
To map a controller method to a route, you can specify the controller and method in the second parameter of the Route
methods.
Route::get('/users', 'UserController@index');
Route::post('/users', 'UserController@store');
Route::put('/users/{id}', 'UserController@update');
Route::delete('/users/{id}', 'UserController@delete');
To pass parameters to a controller method, you can include them as parts of the route path using curly braces.
Route::get('/users/{id}', 'UserController@show');
In the controller method, you can retrieve the parameter value using the corresponding variable name.
namespace SyntoraPHP\App\Controllers;
class UserController
{
public function show($id)
{
// Handle GET request to /users/{id}
echo "user id: " . $id;
}
}
SyntoraPHP provides a powerful and intuitive routing system that enables you to build high-performance APIs with ease. By following the conventions outlined in this document, you can create well-structured and maintainable API applications.
- 1 - Installation
- 2 - Routing
- 3 - HTTP Request
- 4 - CORS
- 5 - Environment Variables
- 6 - Views
- 7 - Database
- 1 - Installation
- 2 - Routing
- 3 - HTTP Request
- 4 - CORS
- 5 - Environment Variables
- 6 - Views
- 7 - Database