jonob/restful is a Laravel package for restful, named and nested routes for laravel 4.
It currently has 8 GitHub stars and 674 downloads on Packagist (latest version v1.0.0).
Install it with composer require jonob/restful.
Discover more Laravel packages by jonob
or browse all Laravel packages to compare alternatives.
Last updated
Create restful routes for Laravel 4, included nested controllers, named routes and custom templates.
This greatly enhances the Route::Resource method currently available in Laravel 4, which currently
offers none of these features.
Add "jonob/restful": ">=1.0.*" to the require section of your composer.json:
"require": {
"jonob/restful": ">=1.0.*"
},
Now run composer install.
Add the following code to the aliases section of the app/config/app.php file
'Restful' => 'Jonob\Restful\Restful',
so that it looks something like the following:
'aliases' => array(
...
'Restful' => 'Jonob\Restful\Restful',
...
),
Restful Routes are created in app\routes.php as follows:
// Create a new bunch of restful routes for the 'products' resource
new Restful('products', 'ProductsController');
// Or use the static method
Restful::make('products', 'ProductsController');
This will automatically create a whole bunch of restful routes for you as follows:
Route::get('products/{id}/edit', array('as' => 'ProductEdit', 'uses' => 'ProductsController@edit');
Route::get('products/add', array('as' => 'ProductAdd', 'uses' => 'ProductsController@create');
Route::get('products/{id}', array('as' => 'Product', 'uses' => 'ProductsController@show');
Route::get('products', array('as' => 'Products', 'uses' => 'ProductsController@index');
Route::post('products', array('as' => 'ProductStore', 'uses' => 'ProductsController@store');
Route::put('products/{id}', array('as' => 'ProductUpdate', 'uses' => 'ProductsController@update');
Route::delete('products/{id}', array('as' => 'ProductDelete', 'uses' => 'ProductsController@destroy');
There are two main options for handling nested routes. You can either nest your controllers in sub-folders as well, or you can refer directly to the main controllers folder
If you have nested controllers, then Restful can handle that too.
Restful::make('products.categories', 'products_CategoriesController');
Note here that the underscore represents a directory seperator, so we would expect the following:
// app/controllers/products/CategoriesController.php
class Products_CategoriesController extends SiteController
{
...
}
This would create the following restful routes for you:
Route::get('products/{product_id}/categories/{id}/edit', array('as' => 'ProductCategoryEdit', 'uses' => 'products.CategoriesController@edit');
Route::get('products/{product_id}/categories/add', array('as' => 'ProductCommentAdd', 'uses' => 'products.CategoriesController@create');
Route::get('products/{product_id}/categories/{id}', array('as' => 'ProductCategory', 'uses' => 'products.CategoriesController@show');
Route::get('products/{product_id}/categories', array('as' => 'ProductCategories', 'uses' => 'products.CategoriesController@index');
Route::post('products/{product_id}/categories', array('as' => 'ProductCategoryAdd', 'uses' => 'products.CategoriesController@store');
Route::put('products/{product_id}/categories/{id}', array('as' => 'ProductCategoryUpdate', 'uses' => 'products.CategoriesController@update');
Route::delete('products/{product_id}/categories/{id}', array('as' => 'ProductCategoryDelete', 'uses' => 'products.CategoriesController@destroy');
You can, of course, still have a nested route that routes to the main controllers folder if you wish:
Restful::make('products.categories', 'CategoriesController');
// app/controllers/CategoriesController.php
class CategoriesController extends SiteController
{
...
}
This would create the following restful routes for you:
Route::get('products/{product_id}/categories/{id}/edit', array('as' => 'ProductCategoryEdit', 'uses' => 'CategoriesController@edit');
Route::get('products/{product_id}/categories/add', array('as' => 'ProductCommentAdd', 'uses' => 'CategoriesController@create');
Route::get('products/{product_id}/categories/{id}', array('as' => 'ProductCategory', 'uses' => 'CategoriesController@show');
Route::get('products/{product_id}/categories', array('as' => 'ProductCategories', 'uses' => 'CategoriesController@index');
Route::post('products/{product_id}/categories', array('as' => 'ProductCategoryAdd', 'uses' => 'CategoriesController@store');
Route::put('products/{product_id}/categories/{id}', array('as' => 'ProductCategoryUpdate', 'uses' => 'CategoriesController@update');
Route::delete('products/{product_id}/categories/{id}', array('as' => 'ProductCategoryDelete', 'uses' => 'CategoriesController@destroy');
Restful Routes uses a default template to create the above routes, but you can easily override this template to create your own routes by passing this as the third parameter.
Restful::make('products', 'ProductsController', $myTemplate);