Code Monkey home page Code Monkey logo

laravel-route-module-macro's Introduction

Exposes a module concept to enforce a CRUD and Restful way of routing and naming convention.

Latest Version on Packagist Build Status Quality Score Total Downloads

This package works with Laravel 5.5. It is very much inspired by the great Freek van der Herten with their Blender Package, where he has a Macro for a module. This really resonates with the CRUD/Restful approach on routing.

Installation

Require the package from Composer:

composer require thejawker/laravel-route-module-macro

As of Laravel 5.5 it will magically register the package.

Usage

You can add Route::module('name', ['only'](optional), ['options](optional)) in any of your routes files. The second parameter will allow you to only use specific actions, and the third being general options. Refer to the Laravel docs for those.

Examples:

Full Resource

routes/api.php

Route::module('posts');
$ php artisan route:list
+-----------+-----------------------------------------+---------------+---------------------------------------------------------------------------+
| Method    | URI                                     | Name          | Action                                                                    |
+-----------+-----------------------------------------+---------------+---------------------------------------------------------------------------+
| POST      | api/posts                               | posts.store   | App\Http\Controllers\PostsController@store                                |
| GET|HEAD  | api/posts                               | posts.index   | App\Http\Controllers\PostsController@index                                |
| GET|HEAD  | api/posts/create                        | posts.create  | App\Http\Controllers\PostsController@create                               |
| DELETE    | api/posts/{post}                        | posts.destroy | App\Http\Controllers\PostsController@destroy                              |
| PUT|PATCH | api/posts/{post}                        | posts.update  | App\Http\Controllers\PostsController@update                               |
| GET|HEAD  | api/posts/{post}                        | posts.show    | App\Http\Controllers\PostsController@show                                 |
| GET|HEAD  | api/posts/{post}/edit                   | posts.edit    | App\Http\Controllers\PostsController@edit                                 |
+-----------+-----------------------------------------+---------------+---------------------------------------------------------------------------+

Only Resource

routes/api.php

Route::module('posts', ['store']);
$ php artisan route:list
+-----------+-----------------------------------------+---------------+----------------------------------------------------------------------------+
| Method    | URI                                     | Name          | Action                                                                     |
+-----------+-----------------------------------------+---------------+----------------------------------------------------------------------------+
| POST      | api/posts                               | posts.store   | App\Http\Controllers\PostsController@store                                 |
+-----------+-----------------------------------------+---------------+----------------------------------------------------------------------------+

Nested Resources

This will enforce you to write Controllers that make sense. A nested users.posts will require you to create a UserPostsController with the required actions. routes/api.php

Route::module('users.posts');
$ php artisan route:list
+-----------+-----------------------------------------+---------------------+----------------------------------------------------------------------------+
| Method    | URI                                     | Name                | Action                                                                     |
+-----------+-----------------------------------------+---------------------+----------------------------------------------------------------------------+
| POST      | api/users/{user}/posts                  | users.posts.store   | App\Http\Controllers\UserPostsController@store                             |
| GET|HEAD  | api/users/{user}/posts                  | users.posts.index   | App\Http\Controllers\UserPostsController@index                             |
| GET|HEAD  | api/users/{user}/posts/create           | users.posts.create  | App\Http\Controllers\UserPostsController@create                            |
| DELETE    | api/users/{user}/posts/{post}           | users.posts.destroy | App\Http\Controllers\UserPostsController@destroy                           |
| PUT|PATCH | api/users/{user}/posts/{post}           | users.posts.update  | App\Http\Controllers\UserPostsController@update                            |
| GET|HEAD  | api/users/{user}/posts/{post}           | users.posts.show    | App\Http\Controllers\UserPostsController@show                              |
| GET|HEAD  | api/users/{user}/posts/{post}/edit      | users.posts.edit    | App\Http\Controllers\UserPostsController@edit                              |
+-----------+-----------------------------------------+---------------------+----------------------------------------------------------------------------+

Test

This package definitely needs some extensive testing.

composer test

License

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

laravel-route-module-macro's People

Contributors

dependabot[bot] avatar thejawker avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

a-schuurman

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.