Autoroute helps you register Laravel routes as YAML.
"La route? Là où on va, on a pas besoin... De route."
composer require eyf/laravel-autoroute
<?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.
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@updateOr 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.updateAutoroute 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 # PRODAnd in your api.yaml file:
domain: api.%app_domain%
prefix: v1
# ...If you don't provide an as option in your route definition:
"users/{id}":
get:
uses: UserController@find
as: my_user_find_route_nameAutoroute 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)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);
}
}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.updateYou can customize the shorthand syntax by implementing RouteNamerInterface::getUses(string $compact).