eyf / laravel-autoroute
Laravel YAML routes
Installs: 1 884
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 4
Forks: 0
Open Issues: 1
Requires
- php: >=7.2.5
- cebe/php-openapi: ^1.7
Requires (Dev)
- laravel/framework: ^9.34
- laravel/sanctum: ^3.0
- orchestra/testbench: ^7.9
- phpunit/phpunit: ^9.5
- dev-master
- v3.0.0-alpha.19
- v3.0.0-alpha.18
- v3.0.0-alpha.17
- v3.0.0-alpha.16
- v3.0.0-alpha.15
- v3.0.0-alpha.14
- v3.0.0-alpha.13
- v3.0.0-alpha.12
- v3.0.0-alpha.11
- v3.0.0-alpha.10
- v3.0.0-alpha.9
- v3.0.0-alpha.8
- v3.0.0-alpha.7
- v3.0.0-alpha.6
- v3.0.0-alpha.5
- v3.0.0-alpha.4
- v3.0.0-alpha.3
- v3.0.0-alpha.2
- v3.0.0-alpha.1
- v3.0.0-alpha
- v2.0.0
- v1.0.0
- v0.5.3
- v0.5.2
- v0.5.1
- v0.5.0
- 0.4.1
- 0.4
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- 0.0.9
- 0.0.8
- 0.0.7
- 0.0.6
- 0.0.5
- 0.0.4
- 0.0.3
- 0.0.2
- 0.0.1
- dev-dependabot/composer/symfony/http-kernel-5.4.20
This package is auto-updated.
Last update: 2025-01-25 08:42:12 UTC
README
Autoroute helps you register Laravel routes as YAML.
"La route? Là où on va, on a pas besoin... De route."
Install
composer require eyf/laravel-autoroute
Usage
<?php // app/Providers/RouteServiceProvider.php use Eyf\Autoroute\Autoroute; class RouteServiceProvider extends ServiceProvider { public function map(Autoroute $autoroute) { $autoroute->load(["api.yaml"]); } }
Note: It will automatically look for files inside the Laravel routes/
folder.
Sample api.yaml
domain: api.example.org prefix: v1 middleware: - api namespace: App\Http\Controllers\Api paths: "users": get: uses: UserController@index post: uses: UserController@store "users/{id}": get: uses: UserController@find put: uses: UserController@update
Or using the compact syntax:
domain: api.example.org prefix: v1 middleware: - api namespace: App\Http\Controllers\Api paths: "users": get: user.index post: user.store "users/{id}": get: user.find put: user.update
Template parameters
Autoroute supports light parameters in YAML files. The format is %<parameter_name>%
.
For instance let's say you need to work with a local API subdomain:
<?php // app/Providers/RouteServiceProvider.php use Eyf\Autoroute\Autoroute; class RouteServiceProvider extends ServiceProvider { public function map(Autoroute $autoroute) { $parameters = [ "app_domain" => env("APP_DOMAIN", "example.org"), ]; $autoroute->load(["api.yaml"], $parameters); } }
And in your local .env
file:
APP_DOMAIN=localhost:8000 # APP_DOMAIN=example.org # PROD
And in your api.yaml
file:
domain: api.%app_domain% prefix: v1 # ...
Default route names
If you don't provide an as
option in your route definition:
"users/{id}": get: uses: UserController@find as: my_user_find_route_name
Autoroute will generate a default route name based on the current namespace, controller and action names:
"users/{id}": get: uses: UserController@find # as: api.user.find (generated)
Custom default route name
If you're not happy with the default route name format, you can implement your own Eyf\Autoroute\RouteNamerInterface
and bind it accordingly in your Laravel app service provider:
<?php // app/Providers/AppServiceProvider.php use Eyf\Autoroute\RouteNamerInterface; use App\Services\MyRouteNamer; class AppServiceProvider extends ServiceProvider { public function register() { $this->app->bind(RouteNamerInterface::class, MyRouteNamer::class); } }
uses
compact syntax
If you're not using any route options (as
, etc...), you can use a "compact" syntax to specify your controllers:
domain: api.%app_domain% prefix: v1 middleware: - api namespace: App\Http\Controllers\Api paths: "users": get: user.index post: user.store "users/{id}": get: user.find put: user.update
Custom compact syntax
You can customize the shorthand syntax by implementing RouteNamerInterface::getUses(string $compact)
.