This Route manager
component for Laravel is
an extra layer on top of Laravel routing mechanism to
be able to store routes in the database.
composer require douma/laravel-database-routes
Run migrations php artisan migrate
Douma\Routes\ServiceProviders\DbServiceProvider::class
.
This service provider loads routes from the database, the routes
table.
Douma\Routes\ServiceProviders\InMemoryServiceProvider::class
.
This service provider loads routes from the memory.
Register the \Douma\Routes\Middleware\RouteMiddleware::class
within App\Http\Kernel.php
.
If the DbServiceProvider
enabled, the RouteMiddleware
only loads the active route into memory.
This leads to increased performance.
Anywhere you need the RouteManager
you can inject Douma\Routes\Contracts\RouteManager
into the constructor. Based on the selected service provider the right
RouteManager
will load.
For example, with php artisan routes:generate
.
use Douma\Routes\Contracts\RouteManager;
class RoutesGenerateCommand extends Command
{
protected $signature = 'routes:generate';
private $routeManager;
public function __construct(RouteManager $routeManager /*, Your other dependencies */)
{
$this->routeManager = $routeManager;
}
public function handle()
{
$this->info('Generating routes');
$this->routeManager->addRoute(
new Route('/my-route',false, 'test1', Controller::class, 'index', ['middleware'])
);
}
}
Run the command for example every 2-5 minutes.
$this->routeManager->addRoute(
new Route(
'/test-test/{id}', true, 'test01234', Controller::class, 'index', ['middleware']
));
Hide the construction logic in a named construct, for different routes.
class MyPageRoute extends Route
{
public static function from(Page $page)
{
return new self(
"/" . $page->getColumn('slug'), false, "page" .$page->getColumn('id'),
PageController::class, 'index', ['middleware']
);
}
}
You can use the construct in the RoutesGenerateCommand
-command.
You need routes to be available elsewhere in your code. Since the routes
are loaded in the database, you can load the RouteManager
anywhere and
ask for the route by name:
$route = $this->routeManager->routeByName('page1');
$url= $route->url();
By default if the route is not found, a NullObject
is returned with an url #
.
If you want to catch the NullObject
, compare to the NullObject
or the url:
if($route == \Douma\Routes\Route::$NULL) {
//... your fallback code
}
//...or compare to the anchor
if($url == '#') {
//... your fallback code
}
Create your own implementation of DbRouteManagerProxy
.
Routes are immutable by default. So for every mutation a new route is returned. If you wish to alter any route argument, simply use the following functions:
$route = new Route('/my-route', false, 'test1', Controller::class, 'index');
//...or
$route = $this->routeManager->routeByName('test1');
//mutations
$newRoute = $route->withName('test2');
$newRoute = $newRoute->withUrl('/my-new-route');
$newRoute = $newRoute->withIsPattern(false);
$newRoute = $newRoute->withController(Controller2::class);
$newRoute = $newRoute->withMiddleware(['test']);
$newRoute = $newRoute->withAction('another-action');
Or chain:
$route = new Route('/my-route', false, 'test1', Controller::class, 'index');
echo $route->withUrl('/product')
->withGetParameters(['a'=>'b'])
->url();
If you would like to use get parameters:
$route = new Route('/my-route', false, 'test1', Controller::class, 'index');
//mutations
$newRoute = $route->withGetParameters([
'id'=>1
]);
//output: /my-route?id=1
echo $newRoute->url();
If you would like to replace pattern parameters, simply use:
$route = new Route('/my-route/{id}', true, 'test1', Controller::class, 'index');
//mutations
$newRoute = $route->withParameters([
'id'=>1
]);
//or...
$newRoute = $route->withParameter('id', 1);
//output: /my-route/1
echo $newRoute->url();
Register an alias in app.php
:
'aliases'=>[
//...
'RouteManager' => Douma\Routes\Facades\RouteManager::class,
]
You can use the RouteManager
-facade in blade views:
{{ RouteManager::routeByName('test')->url() }}
When using the Db service provider, through a Proxy layer the routes
can be cached. They can be cached if you add the following flag to your .env
file:
ROUTE_CACHE_TIME=3600