Git Product home page Git Product logo

laravel-yml-routes's Introduction

Laravel YAML Routes

Latest Version on Packagist Total Downloads

This package is created for Laravel Developers to create routes using YAML files. This package makes it easy for many routes to be created and arranged with well organized prefixes and middleware.

Installation

You can install the package via composer:

composer require broswilli/laravel-yml-routes

You can publish configuration files

php artisan vendor:publish --tag laravel-yml-routes-config

Edit the Root YAML File:

resource_1:
  file: admin.yaml
  prefix: admin
  middleware: ['can:isAdmin']

Create a yaml file of choice for the sake of this example create admin.yaml file in the same directory as the root.yaml file

admin_org:
  path: /org
  controller: [Adapt\SchAdmin\Http\Controllers\Administration\OrganizationController]
  methods: ['resource']

Output routes in the routes.php file

\Broswilli\LaravelYmlRoutes\LaravelYmlRoutesFacade::createRoutes();

Usage

Edit the root YAML file

resource_1:
  file: example.yaml
  prefix: front
  middleware: ['auth', 'can:isAdmin', auth]
resource_2:
  file: example2.yaml
  prefix: admin,
  name: admin
  middleware: ['guest']
resource_3:
  path: /root-test
  controller: [Adapt\SchAdmin\Http\Controllers\SampleController, index]
  methods: ['get']
resource_4:
  path: /root-invoke
  controller: [Adapt\SchAdmin\Http\Controllers\InvokableController]
  methods: ['get']

From the YAML above the root nodes resource_1 and resource_2 points to other yaml files that describes a group of routes. The root nodes resource_3 and resource_4 creates 2 new routes with url example.com/root-test and example.com/root-invoke.

example.yaml

test_resource:
  path: /test
  controller: [Adapt\SchAdmin\Http\Controllers\SampleController, index]
  methods: ['get']
test_resource_2:
  path: /resc
  controller: [Adapt\SchAdmin\Http\Controllers\ResourceController]
  methods: ['resource']
test_resource_3:
  path: /api
  controller: [Adapt\SchAdmin\Http\Controllers\ResourceController]
  methods: ['apiResource']
test_invokable:
  path: /invoke
  controller: [Adapt\SchAdmin\Http\Controllers\InvokableController]
  methods: ['get']

All urls from example.yaml will have a prefix of front and a common middleware of 'can:isAdmin' applied:

  • example.com/front/test
  • example.com/front/invoke

The front prefix and the middleware was defined in the root.yaml file

Route Names and Methods

  • test_resource
    • Route Name(s): test_resource
    • Method(s): GET
    • Controller Class: Adapt\SchAdmin\Http\Controllers\SampleController
    • Action: index
  • test_resource_2
    • Route Names(s): resc.index, resc.store, resc.create, resc.show, resc.edit, resc.update, resc.destroy
    • Methods: GET, POST, PUT, DELETE
    • Controller Class: Adapt\SchAdmin\Http\Controllers\ResourceController
  • test_resource_3: This is similar to test_resource_2 suitable for restful API's. It comes without routes for the create, and edit actions which are not needed for restfull API's
  • test_invokable: A controller that has the __invoke method or a single action controller. You can create the route without stating the action

Please visit https://laravel.com/docs/11.x/controllers#resource-controllers to know more about resource controllers.

Visit https://laravel.com/docs/11.x/controllers#api-resource-routes to know more about API Resource.

Visit https://laravel.com/docs/11.x/controllers#single-action-controllers to know more about single action controllers

Route Prefixes

The example2.yaml is used to demonstrate route prefixes. In the root.yaml file it was referenced as follows:

resource_2:
  file: example2.yaml
  prefix: admin,
  name: admin
  middleware: ['guest']

example2.yaml

admin-index:
  path: /
  controller: [Adapt\SchAdmin\Http\Controllers\AdminController, index]
  methods: ['get']

The root.yaml file where the example2 routes were defined has a node with a key of name. The function of the name node is to create route name prefix.

  • admin-index
    • Route Name(s): admin.admin-index
    • Method(s): GET
    • Controller Class: Adapt\SchAdmin\Http\Controllers\AdminController
    • Action: index

Notice that the Route Name is admin.admin-index instead of admin-index because of the name node defined in the root.yaml file

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

laravel-yml-routes's People

Contributors

broswilli avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.