Routing
Basic Routing
The most basic Atom routes accept a URI and a Closure, providing a very simple and expressive method of defining routes:
router()->get('hello', function () {
return 'Hello World!';
};
The Default Route File
All Atom routes are defined in your routes
files, which are located in the config
directory. These file are automatically loaded by the framework. The config/routes.php
file defines routes that are for your web interface.
For most applications, you will begin by defining routes in your config/routes.php
file. The routes defined in config/routes.php
may be accessed by entering the defined route's URL in your browser. For example, you may access the following route by navigating to http://your-app.dev/user
in your browser:
router()->('/user', 'UserController@index');
Available Router Methods
The router allows you to register routes that respond to any these verbs:
router()->get($uri, $callback);
router()->post($uri, $callback);
router()->resource($model, $controller);
The Resource Method
The resource
method generate routes for CRUD. From the resource
method, will be generated:
Route for showing all resources:
GET
on /resourcesRoute for showing specific resource:
GET
on /resources/:resourceRoute for showing resource creation form:
GET
on /resources/createRoute for creating resource:
POST
on /resourcesRoute for showing resource edition form:
GET
on /resources/:resource/editRoute for updating resource:
POST
on /resources/:resourceRoute for deleting resource:
POST
on /resources/:resource/delete
Route Parameters
Of course, sometimes you will need to capture segments of the URI within your route. For example, you may need to capture a user's ID from the URL. You may do so by defining route parameters:
router()->get('user/:id'), function ($id) {
return 'User = ' . $id;
});
You may define as many route parameters as required by your route:
router()->get('posts/:post/comments/:comment'), function ($postId, $commentId) {
//
});
Route parameters always starts with : comma and should consist of alphabetic characters, and may not contain a - character. Instead of using the - character, use an underscore (_). Route parameters are injected into route callbacks / controllers based on their order - the names of the callback / controller arguments do not matter.
Regular Expression Constraints
You may constrain the format of your route parameters using the where
method on a route instance. The where
method accepts the name of the parameter and a regular expression defining how the parameter should be constrained:
router()->get('user/:name', function ($name) {
//
})->where('name', '[A-Za-z]+');
router()->get('user/:id', function ($id) {
//
})->where('id', '[0-9]+');
Named Routes
Named routes allow the convenient generation of URLs or redirects for specific routes. You may specify a name for a route by chaining the name method onto the route definition:
router()->get('user/profile', function () {
//
})->name('profile');
You may also specify route names for controller actions:
router()->get('user/profile', 'UserProfileController@show')->name('profile');
Generating URLs To Named Routes
Once you have assigned a name to a given route, you may use the route's name when generating URLs or redirects via the global route
function:
// Generating URLs...
$url = route('profile');
// Generating Redirects...
return redirect('profile');
If the named route defines parameters, you may pass the parameters as the second argument to the route
function. The given parameters will automatically be inserted into the URL in their correct positions:
router()->get('/user/:id/profile', function ($id) {
//
})->name('profile');
$url = route('profile', ['id' => 1]);
Route Middleware
To assign middleware to a route, you may use the middleware
method:
router()->get('user/profile', 'UserController@profile')->middleware('middleware_class');
Dernière mise à jour
Cet article vous a-t-il été utile ?