Extracting Laravel 11 Routing Configurations
April 3, 2024
Hello, world!
This continues my previous post on Extracting Laravel 11 Bootstrap Configurations.
In this post, I will discuss how to extract Laravel 11 routing configurations so let's begin.
Extracting the Laravel 11 routing configurations to a separate file is similar to other extractions that was discussed in the previous post but let me list some caveats that you need to be aware of:
- You will need to manually register each route group yourself since
withRoutingignores every other parameters if you are passing theusingparameter which we will be using in this post. - You will need to manually register the new health route because of the reason above.
- And finally, because you are registering things manually, you might miss out from future updates that Laravel will
introduce to
withRoutingmethod.
So, if you are fine with the caveats above, let's get started.
Let's start by creating the bootstrapper file:
artisan make:class -i Bootstrappers/RoutingBootstrapper
Next, let's open the newly created file and add the following code:
<?php
namespace App\Bootstrappers;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Events\DiagnosingHealth;
class RoutingBootstrapper
{
/**
* Create a new class instance.
*/
public function __construct()
{
//
}
/**
* Invoke the class instance.
*/
public function __invoke(Router $router): void
{
// API routes
$router->middleware('api')
->prefix('api')
->group(base_path('routes/api.php'));
// Web routes
$router->middleware('web')
->group(base_path('routes/web.php'));
// Health route
$router->middleware('web')->get('/up', function () {
$viewFile = base_path('/vendor/laravel/framework/src/Illuminate/Foundation/resources/health-up.blade.php');
Event::dispatch(new DiagnosingHealth);
return View::file($viewFile);
});
}
}
You are free to add or remove routes as you see fit then you can now use the RoutingBootstrapper class in the
bootstrap/app.php file:
<?php
use Illuminate\Foundation\Application;
return Application::configure(basePath: dirname(__DIR__))
->withRouting((new App\Bootstrappers\RoutingBootstrapper)(...))
->withMiddleware(new App\Bootstrappers\MiddlewareBootstrapper)
->withExceptions(new App\Bootstrappers\ExceptionsBootstrapper)
->create();
As I mentioned earlier, the withRouting configuration is kind of different from the other methods because it accepts a
Closure not callable which is why it is important to add the (...) after the invocation of the class which
essentially converts a callable to a Closure, you can read more about the first-callable
syntax here.
And that's it! Until next time, happy coding!