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.
Require the package from Composer:
composer require thejawker/laravel-route-module-macro
As of Laravel 5.5 it will magically register the package.
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.
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 |
+-----------+-----------------------------------------+---------------+---------------------------------------------------------------------------+
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 |
+-----------+-----------------------------------------+---------------+----------------------------------------------------------------------------+
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 |
+-----------+-----------------------------------------+---------------------+----------------------------------------------------------------------------+
This package definitely needs some extensive testing.
composer test
The MIT License (MIT). Please see License File for more information.