This functionality is redily available in later versions of Laravel using Route::resource().
An extension to the Laravel Router to add an extra method ->rest()
which
creates RESTful routes similar to Laravel's built in resource.
Add the package to your composer.json
and run composer update
.
{
"require": {
"cyber-duck/restrouter": "dev-master"
}
}
Add the service provider in app/config/app.php
:
'CyberDuck\RestRouter\RestRouterServiceProvider',
This will add our route class to the IoC container in place of the default,
everything will work as standard with the addition of Route::rest()
[app/routes.php]
<?php
$options = ['model' => 'ResourceModel'];
Route::rest('resource-name', 'ResourceController', ['model' => 'Models\Resource']);
This is will register the following routes:
URI | Name | Action |
---|---|---|
GET | HEAD resource-name | resource.index |
POST resource-name | resource.store | ResourceController@store |
GET | HEAD resource-name/{models_resource}/{_path?} | resource.show |
PUT resource-name/{models_resource}/{_path?} | resource.replace | ResourceController@replace |
PATCH resource-name/{models_resource}/{_path?} | resource.update | ResourceController@update |
DELETE resource-name/{models_resource}/{_path?} | resource.destroy | ResourceController@destroy |
GET | HEAD | POST |
{_path}
will capture the remainder of the path after matching the first
part. The controller is also RESTful if you need to add any additional routes.
Options include model, except and only and work the same as resource()