nckrtl / route-maker
This is my package route-maker
Fund package maintenance!
NckRtl
Requires
- php: ^8.4
- illuminate/contracts: ^10.0||^11.0||^12.0
- inertiajs/inertia-laravel: ^2.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
README
This Laravel packages lets you generate a routes file based on your public controller methods. This package works particularly well with Laravel Wayfinder, as it allows you to reference controller methods instead of just routes. Based on the method signature in your controllers we could generate a routes file, automating route management entirely.
Installation
You can install the package via composer:
composer require nckrtl/route-maker
You can publish and run the migrations with:
php artisan vendor:publish --tag="route-maker-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="route-maker-config"
This is the contents of the published config file:
return [ 'method_defaults' => [ 'GET' => ['index', 'show'], 'POST' => ['store'], 'PUT' => ['update'], 'DELETE' => ['destroy'],, ], ];
Usage
Update your vite config to include an additional run command:
import { run } from "vite-plugin-run"; export default defineConfig({ plugins: [ run([ { name: "route-maker", run: ["php", "artisan", "route-maker:make"], pattern: ["app/**/Http/**/*.php"], }, ]), ], });
Next, update your main routes file to include the generated routes with:
use NckRtl\RouteMaker\Facades\RouteMaker; RouteMaker::routes();
Now you're all set. Running vite dev should nog generate the routes based on your controller methods. On file change of any controller the routes file will be regenerated.
Route definition structure
The way routes are generated are pretty opionated. The naming convention of routes is inspired by how Laravel Wayfinder exposes routes/actions. For this controller:
<?php namespace App\Http\Controllers; class ContactController extends Controller { public function show(): \Inertia\Response { return inertia('Contact'); } }
The generated route definition will look like:
Route::get('/contact', [\App\Http\Controllers\ContactController::class, 'show'])->name('Controllers.ContactController.show');
Setting route parameters and other properties.
To influence the route that is being generated you can you the Route
attribute. For example you can define a route parameter like so:
use NckRtl\RouteMaker\Route; ... #[Route(parameters: ['article:slug'])] public function show(Article $article): \Inertia\Response { return inertia('Article/Show', [ 'article' => $article->data->forDisplay(), ]); }
Other route properties are also supported like middleware
. Besides setting middelware on specific methods you can also set them at the controller level, just as a prefix:
class ArticleController extends Controller { protected static string $routePrefix = 'articles'; protected static string $routeMiddleware = 'auth:verified'; ...
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Feel free to contribute. Make sure to add/update tests for new or improved features.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.