Laravel Routing

Laravel Routing

Route that responds to multiple HTTP verbs

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

Route::any('foo', function () {
    //
});

You may do so using the match method. Or, you may even register a route that responds to all HTTP verbs using the any method.

Route Parameters

Are always encased within {} braces and should consist of alphabetic characters, and may not contain a - character. Instead of using the - character, use an underscore (_).

Are injected into route callbacks / controllers based on their order - the names of the callback / controller arguments do not matter.

Route Groups

Allow you to share route attributes, such as middleware or namespaces, across a large number of routes without needing to define those attributes on each individual route.

Another common use-case for route groups is assigning the same PHP namespace to a group of controllers using the namespace method.

Route groups may also be used to handle sub-domain routing.

Route Model Binding

Provides a convenient way to automatically inject the model instances directly into your routes.

Accessing The Current Route

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

Basic Routing

Route::get('foo', function () {
    return 'Hello World';
});

The most basic Laravel routes accept a URI and a Closure.

Fallback Routes

Using the Route::fallback method, you may define a route that will be executed when no other route matches the incoming request.

Since you may define the fallback route within your routes/web.php file, all middleware in the web midddleware group will apply to the route.

  • Routes/web.php.

Syntax

Route::fallback(function () {
    //
});

Rate Limiting

Route::middleware('auth:api', 'throttle:rate_limit,1')->group(function () {
    Route::get('/user', function () {
        //
    });
});

Laravel includes a middleware  to rate limit access to routes within your application.

Form Method Spoofing

<form action="https://laravel.com/foo/bar" method="POST">
    <input type="hidden" name="_method" _cke_saved_name="_method" value="PUT">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
<form action="https://laravel.com/foo/bar" method="POST">
    @method('PUT')
    @csrf
</form>
  • So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form.
  • The value sent with the _method field will be used as the HTTP request method.
  • Ou may use the @method Blade directive to generate the _method input.

Related concepts

Laravel Routing

Laravel Routing — Structure map

Clickable & Draggable!

Laravel Routing — Related pages: