jonob/restful

此软件包最新版本(v1.0.0)没有可用的许可证信息。

为Laravel 4提供命名和嵌套路由的Restful

v1.0.0 2013-01-13 14:13 UTC

This package is not auto-updated.

Last update: 2024-09-14 12:59:48 UTC


README

为Laravel 4创建restful路由,包括嵌套控制器、命名路由和自定义模板。这大大增强了目前Laravel 4中可用的Route::Resource方法,该方法目前不提供这些功能。

入门指南

Composer

"jonob/restful": ">=1.0.*"添加到composer.json文件的require部分

"require": {
	"jonob/restful": ">=1.0.*"
},

现在运行composer install

Laravel

将以下代码添加到app/config/app.php文件的aliases部分

'Restful' => 'Jonob\Restful\Restful',

使其看起来像以下这样

'aliases' => array(
	...
	'Restful'       => 'Jonob\Restful\Restful',
	...
),

添加Restful路由

Restful路由是在app\routes.php中按以下方式创建的

// Create a new bunch of restful routes for the 'products' resource
new Restful('products', 'ProductsController');

// Or use the static method
Restful::make('products', 'ProductsController');

这将自动为您创建一系列restful路由,如下所示

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');

嵌套路由

处理嵌套路由有两种主要选项。您可以将控制器嵌套在子文件夹中,或者可以直接引用主控制器文件夹

嵌套控制器

如果您有嵌套控制器,Restful也可以处理。

Restful::make('products.categories', 'products_CategoriesController');

注意,下划线代表目录分隔符,所以我们预期以下内容

// app/controllers/products/CategoriesController.php
class Products_CategoriesController extends SiteController
{
	...
}

这将为您创建以下restful路由

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');

根控制器目录中的控制器

当然,如果您愿意,您仍然可以有一个嵌套路由,它将路由到主控制器文件夹

Restful::make('products.categories', 'CategoriesController');

// app/controllers/CategoriesController.php
class CategoriesController extends SiteController
{
	...
}

这将为您创建以下restful路由

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路由使用默认模板来创建上述路由,但您可以通过传递它作为第三个参数来轻松覆盖此模板以创建自己的路由。

Restful::make('products', 'ProductsController', $myTemplate);