2017 © Pedro Peláez
 

library yaroute

image

serabass/yaroute

  • Tuesday, April 10, 2018
  • by Serabass
  • Repository
  • 1 Watchers
  • 0 Stars
  • 27 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 18 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Build Status StyleCI, (*1)

Yaroute is a simple route-organizer that uses YAML to register routes in Laravel., (*2)

  1. Installation
  2. Docs
  3. Examples
  4. Usage
  5. Mixins
  6. Regular Expressions presets
  7. Generating YAML

Installation

$ composer require serabass/yaroute, (*3)

Docs

The format of simple route must look like <METHOD> /<PATH> [as <NAME>] [uses <MIDDLEWARE>]: <ACTION>, (*4)

The format of group must look like:, (*5)

<PREFIX> [uses <MIDDLEWARE>]:
  <METHOD> /<PATH> [as <NAME>]: <ACTION>

Groups can be nested, (*6)

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, (*7)

/api uses api:
  GET /entity: EntityController@list

This simple config creates a group that uses api middleware and contains /entity route, (*8)

You can see all examples in Examples directory., (*9)

Usage

  1. Create your .yaml file, e.g. api.yaml in any directory (e.g. routes)
  2. Write just one line in your routes/web.php or routes/api.php and you can register all routes in your .yaml
\Serabass\Yaroute\Yaroute::registerFile(__DIR__ . '/api.yaml');

Simple group config:, (*10)

/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:, (*11)

    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., (*12)

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:, (*13)

~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:, (*14)

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/, (*15)

Yaroute has few prefedined aliases:, (*16)

  • 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.:, (*17)

$ php artisan yaroute:generate > routes/api.yaml, (*18)

The Versions

09/04 2018

dev-analysis-8jkJeR

dev-analysis-8jkJeR https://github.com/Serabass/yaroute

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Serabass