Laravel

Laravel TutorialChange default routing behaviour in Laravel 5.2.31 +CustomException class in LaravelDeploy Laravel 5 App on Shared Hosting on Linux ServerLaravel ArtisanLaravel AuthenticationLaravel AuthorizationLaravel Blade TemplatesLaravel CashierLaravel CollectionsLaravel Common Issues & Quick FixesLaravel ConstantsLaravel ControllersLaravel Cron basicsLaravel Cross Domain RequestLaravel Custom Helper functionLaravel DatabaseLaravel Database MigrationsLaravel Database SeedingLaravel Directory StructureLaravel DockerLaravel EloquentLaravel Eloquent : RelationshipLaravel Eloquent: Accessors & MutatorsLaravel Eloquent: ModelLaravel Error HandlingLaravel Events and ListenersLaravel Filesystem / Cloud StorageLaravel Form Request(s)Laravel Getting started with laravel-5.3Laravel HelpersLaravel HTML and Form BuilderLaravel InstallationLaravel Installation GuideLaravel Introduction to laravel-5.2Laravel Introduction to laravel-5.3Laravel lumen frameworkLaravel Macros in Eloquent RelationshipLaravel MailLaravel MiddlewareLaravel ObserverLaravel PackagesLaravel PaginationLaravel Permissions for storageLaravel PoliciesLaravel QueuesLaravel RequestsLaravel Route Model BindingLaravel RoutingLaravel SeedingLaravel ServicesLaravel SocialiteLaravel Task SchedulingLaravel TestingLaravel Token Mismatch Error in AJAXLaravel use fields aliases in EloquentLaravel Useful linksLaravel ValetLaravel ValidationMultiple DB Connections in LaravelNaming Files when uploading with Laravel on WindowsRemove public from URL in laravelSparkpost integration with Laravel 5.4



Change default routing behaviour in Laravel 5.2.31 +

From WikiOD

Syntax[edit | edit source]

  • public function map(Router $router) // Define the routes for the application.
  • protected function mapWebRoutes(Router $router) // Define the "web" routes for the application.

Parameters[edit | edit source]

Parameter Header
Router $router \Illuminate\Routing\Router $router

Remarks[edit | edit source]

Middleware means that every call to a route will go through the middleware before actually hitting your route specific code. In Laravel the web middleware is used to ensure session handling or the csrf token check for example.

There are other middlewares like auth or api by default. You can also easily create your own middleware.

Adding api-routes with other middleware and keep default web middleware[edit | edit source]

Since Laravel version 5.2.31 the web middleware is applied by default within the RouteServiceProvider (https://github.com/laravel/laravel/commit/5c30c98db96459b4cc878d085490e4677b0b67ed)

In app/Providers/RouteServiceProvider.php you will find the following functions which apply the middleware on every route within your app/Http/routes.php

public function map(Router $router)
{
    $this->mapWebRoutes($router);
}

// ...

protected function mapWebRoutes(Router $router)
{
    $router->group([
        'namespace' => $this->namespace, 'middleware' => 'web',
    ], function ($router) {
        require app_path('Http/routes.php');
    });
}

As you can see the middleware web is applied. You could change this here. However, you can also easily add another entry to be able to put your api routes for example into another file (e.g. routes-api.php)

public function map(Router $router)
{
    $this->mapWebRoutes($router);
    $this->mapApiRoutes($router);
}

protected function mapWebRoutes(Router $router)
{
    $router->group([
        'namespace' => $this->namespace, 'middleware' => 'web',
    ], function ($router) {
        require app_path('Http/routes.php');
    });
}

protected function mapApiRoutes(Router $router) 
{
    $router->group([
        'namespace' => $this->namespace, 'middleware' => 'api',
    ], function ($router) {
        require app_path('Http/routes-api.php');
    });
}

With this you can easily seperate you api routes from your application routes without the messy group wrapper within your routes.php

Credit:Stack_Overflow_Documentation