如何在Laravel 4中重新路由资源以在$ id之外包括其他字段 [英] How to reroute a resource to include a different field besides $id in Laravel 4
问题描述
我有一个资源可以控制我的一张桌子粉丝".我有一个视图,显示了每个视图的信息.现在,这些网址是"url.com/fans/{$id}",其中{$ id}是表中行/对象的唯一ID.
I have a resource controlling one of my tables "fans". I have a view that shows the information for each respective. The urls for these right now is "url.com/fans/{$id}", where {$id} is the unique id for the row/object in the table.
我想保持这种关系,但我也想将url指向表中的另一列(另一个唯一标识符).像"ulr.com/fans/{$new_column}"之类的东西.
I would like to maintain this relationship, but I would also like to the url be pointed to another column in the table (another unique identifier). So something like "ulr.com/fans/{$new_column}".
我将如何重新路由此视图/URL,使其看起来像这样?这是我到目前为止的内容:
How would I reroute this view/url so that it appears like that? This is what I have so far:
路线:
Route::resource('fans', 'FansController');
FansController:
FansController:
public function show($id) {
$fan = Fan::find($id);
return View::make('fans.show', compact('fan'));
}
因此,最终,我希望"url.com/fans/{$id}"仍然可以工作,但是随后它将被重新路由到"url.com/fans/{$new_column}".并直接转到"url.com/fans/{$new_column}"就应该呆在那里.
So ultimately, I would like "url.com/fans/{$id}" to still work, but then it will be rerouted to "url.com/fans/{$new_column}". And going directly to "url.com/fans/{$new_column} should just stay there.
推荐答案
如果您能够区分 id 和* new_column *之间的每个参数值,则可以声明的所有路由粉丝使用 Route :: get 而不是 Route :: resource 进行路由,并定义正则表达式路由约束,例如,如果您的 id 列仅包含数字,而您的* new_column *列具有alpha值:
If you are able to differentiate between each parameter value between id and *new_column* you can declare all your routes for fans route with Route::get instead of Route::resource and define Regular Expression Route Constraints, so for example if your id column has numbers only and your *new_column* column has alpha values:
Route::get('fans/{new_column}', array('as' => 'anotherColumn', function($new_column)
{
$fan = Fan::where('new_column','=',$new_column);
return View::make('fans.show', compact('fan'));
}))
->where('new_column', '[A-Za-z]+');
Route::get('fans/{id}', function($id)
{
// do something with $id or
return Redirect::route('anotherColumn');
})
->where('id', '[0-9]+');
然后声明其余自动生成的资源路由.
Then declare the rest of the automatically generated resource routes.
这篇关于如何在Laravel 4中重新路由资源以在$ id之外包括其他字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!