Series This post is in a series on New Features in Laravel 5.3.

Routing changes in Laravel 5.3

Posted on July 27, 2016

!
Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.

The last few versions of Laravel have showed the way routing works shifting around a bit. This is usually a sign that we're feeling some sort of pain—something feels off—but haven't found the perfect solution yet. In 5.3, we may have found it.

Looking back: Routing changes in 5.2

In Laravel 5.2 we temporarily saw two separate route groups in routes.php, one for "web" and one for "API", but that went away mid-5.2.

What stuck around, though, was the idea of multiple middleware groups, and out of the box there's one for "web" routes and one for "API" routes.

The "web" group gets everything you'd expect your normal web users to need: sessions, cookies, CSRF protection, etc. The "API" group is lighter, and came by default with the "throttle" middleware, making the case for a stateless REST API.

Routing changes in 5.3

In 5.3, the app/Http/routes.php file has now moved to the root routes/ directory, and it's now split into two files: web.php and api.php. As you can probably guess, the routes in routes/web.php are wrapped with the web middleware group and the routes in routes/api.php are wrapped with the api middleware group.

There are a few benefits of this. First, we get the suggestion and easy implementation of the distinction between our web routes and our API routes. Second, it's now an application-level convention to have multiple routes files, which will likely free more developers up to feel comfortable organizing their routes file this way. And third, this moves the routes directory out of app/, which both makes the routes directory more accessible to new users and makes app/ a fully PSR-4-autoloaded directory, which feels just a bit pure-r.

If you want to customize this or add your own separate routes files, check out App\Providers\RouteServiceProvider for inspiration:

...
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        //
    }

    protected function mapApiRoutes()
    {
        Route::group([
            'middleware' => ['api', 'auth:api'],
            'namespace' => $this->namespace,
            'prefix' => 'api',
        ], function ($router) {
            require base_path('routes/api.php');
        });
    }

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

As you can see, there's an easy syntax for wrapping the results of any given routes file with a route group and then applying whatever prefixes or middleware or whatever else that you'd like.

That's it! Enjoy!


Comments? I'm @stauffermatt on Twitter


Tags: laravel  •  laravel 5.3  •  routing


This is part of a series of posts on New Features in Laravel 5.3:

  1. Jun 16, 2016 | laravel, laravel 5.3, echo, websockets
  2. Jun 27, 2016 | laravel, laravel 5.3
  3. Jun 29, 2016 | laravel, laravel 5.3, eloquent
  4. Jul 6, 2016 | laravel, laravel 5.3
  5. Jul 8, 2016 | laravel, laravel 5.3, eloquent
  6. Jul 25, 2016 | laravel, laravel 5.3
  7. Jul 26, 2016 | laravel, laravel 5.3
  8. Jul 27, 2016 | laravel, laravel 5.3, laravel scout, laravel passport, mailable
  9. Jul 27, 2016 | laravel, laravel 5.3, routing
  10. Jul 29, 2016 | laravel, laravel 5.3, laravel scout
  11. Jul 30, 2016 | laravel, laravel 5.3, laravel passport, oauth
  12. Aug 5, 2016 | laravel, laravel 5.3, mail, laravel mailables
  13. Aug 8, 2016 | laravel, laravel 5.3
  14. Oct 19, 2016 | laravel, laravel 5.3, laravel notifications, notifications
  15. Dec 21, 2016 | laravel, laravel 5.3, queues
  16. Dec 21, 2016 | laravel, laravel 5.3, vuejs, vueify, authorization
  17. Jan 30, 2017 | laravel, laravel 5.3, artisan

Subscribe

For quick links to fresh content, and more thoughts that don't make it to the blog