我想在我的网站中使用这些url
模式构建花哨的url
:
http://domain.com/specialization/eye
http://domain.com/clinic-dr-house
http://domain.com/faq
第一个url
有一个简单的路由模式:
Route::get('/specialization/{slug}', 'FrontController@specialization');
第二个和第三个url
指的是两个不同的控制器动作:
SiteController@clinic
SiteController@page
我尝试使用此过滤器:
Route::filter('/{slug}',function()
{
if(Clinic::where('slug',$slug)->count() == 1)
Route::get('/{slug}','FrontController@clinic');
if(Page::where('slug',$slug)->count() == 1)
Route::get('/{slug}','FrontController@page');
});
我有一个例外......有一种不太痛苦的方法吗?
答案 0 :(得分:1)
要声明过滤器,您应该使用过滤器作为静态名称,例如:
Route::filter('filtername',function()
{
// ...
});
然后你可以像这样在你的路线中使用这个过滤器:
Route::get('/specialization/{slug}', array('before' => 'filtername', 'uses' => 'FrontController@specialization'));
因此,每当您使用http://domain.com/specialization/eye
时,将在分派路由之前执行附加到此路由的过滤器。详细了解documentation。
更新:对于第二和第三条路线,您可以检查过滤器中的路线参数,并根据参数执行不同的操作。此外,您可以对两个网址使用一种方法,从技术上讲,两个网址都与一条路线相同,因此请使用一条路线并根据参数执行不同的操作,例如您有以下url
s:
http://domain.com/clinic-dr-house
http://domain.com/faq
对两个网址使用单一路由,例如,使用:
Route::get('/{param}', 'FrontController@common');
在common
中创建FrontController
方法,如下所示:
public function common($param)
{
// Check the param, if param is clinic-dr-house
// the do something or do something else for faq
// or you may use redirect as well
}