2014-02-14 206 views
0

有沒有辦法讓通配符路由?但只限於特定的名稱。Laravel的通配符路線?

例如,

我有很多途徑,導致在同一個地方:

/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'=>'[email protected]' 
)); 

但只能使用存檔或主人或瀏覽爲通配符。

+0

所以你有三個資源,但是你想讓通用路由器具有通配符? – carousel

回答

1

根據通配符,您無法定義不同的控制器。你將不得不在控制器中定義它。

Route::get('{page}/gallery/{galleryId}/picture/{pictureId}', array(
    'as'=>'picture', 
    'uses'=>'[email protected]' 
)); 

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採用先入 - >一線>出

0

如果你有您的{???}這可以只是一個正則表達式。

也許這樣的事情{(archive|browse|masters)}

更新:我想在L3上述作品,但L4具有以不同的方式

Route::get('/{variable}', function() 
{ 
    return View::make('view'); 
})->where('masters', 'browse', 'archive'); 
1

做你可以試試這個

Route::get('{type}/gallery/{galleryId}/picture/{pictureId}', array(
    'as'=>'picture', 
    'uses'=>'[email protected]' 
))->where('type', 'masters|browse|archive'); 

PictureController:

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