Laravel 拉威尔的通配符路线?
有没有办法建立一个通配符路由?但是只有特定的名字 例如 我有很多通往同一个地方的路线:Laravel 拉威尔的通配符路线?,laravel,laravel-4,Laravel,Laravel 4,有没有办法建立一个通配符路由?但是只有特定的名字 例如 我有很多通往同一个地方的路线: /archive/gallery/1/picture/1 /masters/gallery/1/picture/1 /browse/gallery/1/picture/1 这些都加载了同一张图片,但如果我能这样做就太好了: Route::get('{???}/gallery/{galleryId}/picture/{pictureId}', array( 'as'=>'picture',
/archive/gallery/1/picture/1
/masters/gallery/1/picture/1
/browse/gallery/1/picture/1
这些都加载了同一张图片,但如果我能这样做就太好了:
Route::get('{???}/gallery/{galleryId}/picture/{pictureId}', array(
'as'=>'picture',
'uses'=>'PictureController@getPicture'
));
但只能使用存档、主控或浏览作为通配符。根据通配符,您不能定义不同的控制器。您必须在控制器中定义它
Route::get('{page}/gallery/{galleryId}/picture/{pictureId}', array(
'as'=>'picture',
'uses'=>'PictureController@getPicture'
));
public function getPicture($page)
{
if ($page == "archive")
return View::make('archive');
else if ($page == "browse")
return View::make('browse');
else if ($page == "masters")
return View::make('masters');
}
但是,请确保将路由放置在路由文件的底部,否则它将覆盖其他路由:)因为laravel在您的
{???}
中使用先进先出这可以是一个正则表达式
可能是这样的{(存档|浏览|主控)}
更新:我认为上面的工作在L3,但L4必须做不同的
Route::get('/{variable}', function()
{
return View::make('view');
})->where('masters', 'browse', 'archive');
你可以试试这个
Route::get('{type}/gallery/{galleryId}/picture/{pictureId}', array(
'as'=>'picture',
'uses'=>'PictureController@getPicture'
))->where('type', 'masters|browse|archive');
图片控制器:
public function getPicture($type, $galleryId, $pictureId)
{
// $type could be only masters or browse or archive
// otherwise requested route won't match
// If you want to load view depending on type (using type)
return View::make($type);
}
所以您有三个资源,但您想使用通配符使路由器通用吗?