serabass / yaroute
Requires (Dev)
- matthewbdaly/artisan-standalone: 0.0.*
- mockery/mockery: ~1.0
- orchestra/testbench: ^4.0
- orchestra/testbench-browser-kit: ^4.0
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: ^8.0
- psy/psysh: ^0.9.9
- sebastian/phpcpd: ^4.0
- squizlabs/php_codesniffer: ^3.4
- vimeo/psalm: ^3.5
This package is auto-updated.
Last update: 2024-11-12 00:28:37 UTC
README
Yaroute is a simple route-organizer that uses YAML to register routes in Laravel.
Installation
$ composer require serabass/yaroute
Docs
The format of simple route must look like <METHOD> /<PATH> [as <NAME>] [uses <MIDDLEWARE>]: <ACTION>
The format of group must look like:
<PREFIX> [uses <MIDDLEWARE>]: <METHOD> /<PATH> [as <NAME>]: <ACTION>
Groups can be nested
Examples
GET / as home uses guest: HomeController@index
This simple config creates a route with url /
, named home
, that uses guest
middleware and executes
HomeController@index
action
/api uses api: GET /entity: EntityController@list
This simple config creates a group that uses api
middleware and contains /entity
route
You can see all examples in Examples directory.
Usage
- Create your
.yaml
file, e.g.api.yaml
in any directory (e.g.routes
) - Write just one line in your
routes/web.php
orroutes/api.php
and you can register all routes in your.yaml
\Serabass\Yaroute\Yaroute::registerFile(__DIR__ . '/api.yaml');
Simple group config:
/api uses api: GET /entity: EntityController@list GET /entity/{id ~ \d+}: EntityController@get POST /entity/{id ~ \d+}: EntityController@save GET /entity/{id}/getComments: action: EntityController@getComments /admin: GET /index: AdminController@index GET /entity/{id ~ \d+}: AdminController@entity /subroute: GET /entity/{id ~ \d+}: AdminController@entity GET /data/{alias ~ .+}: AdminController@getData
It'll be converted to this:
Route::group(['prefix' => 'api', 'uses' => 'api'], function () { Route::get('entity', 'EntityController@list'); Route::get('entity/{id}', 'EntityController@get')->where('id', '\d+'); Route::post('entity/{id}', 'EntityController@save')->where('id', '\d+'); Route::get('entity/{id}/getComments', 'EntityController@getComments')->where('id', '\d+'); Route::group('admin', function () { Route::get('index', 'AdminController@index'); Route::get('entity/{id}', 'AdminController@entity')->where('id', '\d+'); Route::group('subroute', function () { Route::get('entity/{id}', 'AdminController@entity')->where('id', '\d+'); Route::get('data/{alias}', 'AdminController@getData')->where('alias', '.+'); }); }); });
You can see all examples in Examples directory.
Mixins
+myResourceMixin(ControllerName, Alias = myResource): GET / as ${Alias}.list: ${ControllerName}@list /{id ~ \d+} as .${Alias}.element: GET / as .show: ${ControllerName}@show POST / as .update: ${ControllerName}@update PUT / as .create: ${ControllerName}@create DELETE / as .delete: ${ControllerName}@destroy /entity as entityResource: +: myResourceMixin(MyEntityController, myEntity)
Regular Expressions presets
You can create predefined names for RegExps that using in uri params. It's simple to do. See the example below:
~hex: '[a-f0-9]+' ~urlAlias: '[A-Z-]+' # Note: all regexes must be quoted because yaml-parser recognizes [...] as array
And if you want to use it in route you can write as:
GET /entity/{id ~hex} as entity: EntityController@show
Please note that there is no space. It's important. If you placed a space char there,
the value will passed as plain regex /numeric/
Yaroute has few prefedined aliases:
- numeric:
\d+
- hex:
[\da-fA-F]+
- alias:
[\w-]+
- boolean:
[01]
Generating YAML
You can also generate new YAML document (based on registered routes in app)
with $ php artisan yaroute:generate
.
It will be printed to stdout so you can pipe it to needed file, e.g.:
$ php artisan yaroute:generate > routes/api.yaml