laravel routes


Laravel routes accept a URI and a Closure, providing a very simple and expressive method of defining routes.

Route::get('welcome', function () {
    return 'Welcome to Laravel';
});

All Laravel routes are defined in the route files which are located in the routes directory. These files are automatically loaded by the framework. web.php file defines routes that are for web interface. For most applications, all the routes are defined in this file.

ROUTER METHODS

The router allows you to register routes that respond to any HTTP verb.

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

At times, you might need to register a route that responds to multiple HTTP verbs. In such cases, you can use the match method or register a route that responds to all HTTP verbs using any method.

Route::match(['get', 'post'], '/', function () {

});

Route::any('welcome', function () {

});

ROUTE PARAMETERS

At times, you might need to retrieve segments of the URI within your route. For that, you can define route parameters in alphabetic characters encased within {} braces.

Route::get('user/{id}', function ($id) {
    return 'User '.$id;
});

OPTIONAL PARAMETERS

You can pass optional parameters to the route by placing a ? after the parameter name with a default value assigned to the corresponding variables.

Route::get('user/{name?}', function ($name = null) {
    return $name;
});

Route::get('user/{name?}', function ($name = 'John') {
    return $name;
});

REGULAR EXPRESSION CONSTRAINTS

The format of route parameters can be restricted using where method on a route instance. It accepts the name of the parameter and a regular expression defining how the parameter should be limited.

Route::get('user/{name}', function ($name) {

})->where('name', '[A-Za-z]+');

Route::get('user/{id}', function ($id) {

})->where('id', '[0-9]+');

Route::get('user/{id}/{name}', function ($id, $name) {

})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);

NAMED ROUTES

Named routes allow clean URLs for specific routes. You may specify a name for a route by chaining the name method into the route definition.

Route::get('user/profile', function () {

})->name('profile');

For the named routes, you can use that name when generating URLs.

$url = route('profile');

ROUTE GROUPS

Route groups allow you to share middleware or namespaces across a large number of routes with a single command. Shared attributes are specified in an array format as the first parameter to the Route::group method.

MIDDLEWARE

Middleware provides a convenient mechanism for filtering HTTP requests entering the application. There are several middleware included in the Laravel framework. Add the middleware key in the group attribute array to assign any middleware to all routes within a group.

Route::group(['middleware' => 'auth'], function () {
    Route::get('/', function ()    {

    });

    Route::get('user/profile', function () {

    });
});

NAMESPACES

Assigning the same namespace to a group of controllers can be done the same way.

Route::group(['namespace' => 'Admin'], function () {
    
});

ROUTE PREFIXES

The prefix group attribute can be used to prefix each route in the group with a given URI.

Route::group(['prefix' => 'admin'], function () {
    Route::get('users', function ()    {
        // matches "/admin/users" URL
    });
});