pebblecms/laravel-database-routes
Installation
composer require pebblecms/laravel-database-routes
Publish the migration and config:
php artisan vendor:publish --provider="PebbleCMS\DatabaseRoutes\ServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="PebbleCMS\DatabaseRoutes\ServiceProvider" --tag="config"
Run the migration:
php artisan migrate
First Use Case
Define a route in the database via database_routes table:
INSERT INTO database_routes (name, uri, action, middleware, priority)
VALUES ('admin.dashboard', '/admin', 'AdminController@index', 'auth', 10);
Clear route cache to apply changes:
php artisan route:clear
Where to Look First
config/database-routes.php (adjust table name, caching behavior).app/Providers/DatabaseRoutesServiceProvider.php (register custom route models).database/migrations/[timestamp]_create_database_routes_table.php (extend with custom fields).Define Routes in Database
Use the database_routes table to store routes dynamically:
// Example: Programmatically add a route
\PebbleCMS\DatabaseRoutes\Facades\DatabaseRoutes::add([
'name' => 'api.user.profile',
'uri' => 'api/user/profile',
'action' => 'App\Http\Controllers\UserController@show',
'middleware' => 'api,auth:sanctum',
'priority' => 20,
]);
Integrate with Existing Routes
Combine database routes with static routes in routes/web.php or routes/api.php:
// routes/web.php
Route::get('/static', function () { return 'Static Route'; });
// Database routes (auto-loaded after `route:clear`)
Route::get('/dynamic', 'DynamicController@index');
Middleware and Groups Use middleware columns to attach groups:
-- Example: Route group with middleware
INSERT INTO database_routes (name, uri, action, middleware, priority)
VALUES ('admin.*', 'admin/{controller}', 'AdminController@__invoke', 'web,auth', 5);
Note: Supports Laravel’s middleware syntax (e.g., auth:sanctum|api).
Caching and Performance
Disable caching during development (set cache_enabled to false in config).
Clear cache after changes:
php artisan route:clear
Custom Route Models
Extend the default Route model by binding a custom Eloquent model:
// app/Providers/DatabaseRoutesServiceProvider.php
public function boot()
{
DatabaseRoutes::extend(function ($app) {
return new \App\Models\CustomRoute();
});
}
Route Cache Invalidation
database_routes won’t reflect until route:clear is run.php artisan route:clear --force in CI/CD.Name Collisions
admin.dashboard) will cause conflicts.// app/Observers/RouteObserver.php
public function saving(Route $route)
{
if (Route::named($route->name)->count()) {
throw new \Exception("Route name {$route->name} already exists.");
}
}
Middleware Parsing Quirks
throttle:60,1) may not parse correctly.|) syntax or simplify middleware in the database.Priority Conflicts
priority may load unpredictably.10, 20, 30) or sort alphabetically in the model.Dynamic URI Validation
/user/{id}) may not validate properly.Route::hasRoute() or custom validation in a model observer.php artisan route:list --path=/admin
SELECT * FROM database_routes WHERE uri LIKE '%admin%';
config/database.php:
'log_queries' => true,
Custom Route Actions Override the default action resolver to support closures or controller methods dynamically:
// app/Providers/DatabaseRoutesServiceProvider.php
DatabaseRoutes::extend(function ($app) {
$app->bind('route.action.resolver', function () {
return new \App\Services\CustomRouteActionResolver();
});
});
Route Events Listen for route changes via events:
// app/Providers/EventServiceProvider.php
protected $listen = [
\PebbleCMS\DatabaseRoutes\Events\RouteAdded::class => [
\App\Listeners\LogRouteChange::class,
],
];
API for Route Management Expose CRUD endpoints for routes:
// routes/api.php
Route::apiResource('routes', \App\Http\Controllers\RouteController::class);
Soft Deletes Enable soft deletes in the model to "disable" routes without removing them:
// app/Models/Route.php
use SoftDeletes;
protected $dates = ['deleted_at'];
How can I help you explore Laravel packages today?