2017-04-19 38 views
-1

我想两条只使用两条路线和获取(mp3,视频,专辑艺术家)。Laravel:54使用同一路线的一条路线

这里是我的尝试:(一组 “/”)

Route::get("mp3s", "[email protected]"); 
    Route::get("mp3s/mp3/{slug}", "[email protected]"); 

    Route::get("videos", "[email protected]"); 
    Route::get("videos/video/{slug}", "[email protected]"); 

    Route::get("albums", "[email protected]"); 
    Route::get("albums/album/{slug}", "[email protected]"); 

    Route::get("artists", "[email protected]"); 
    Route::get("artists/artist/{slug}", "[email protected]"); 

我已经所著一个foreach,但:

  1. 我认为这是eachpage一种不好的做法。

  2. 我不能得到(MP3或专辑,..)在这里:

foreach (pts as $pt) { 
    Route::get($pt.'s', '[email protected]'); 
    Route::get(("{$pt}s/$pt/{slug}"), "[email protected]"); 
    } 

回答

1

,你使用的是相同的控制器 - 这被认为是一个贫穷的实践 - ,你可以处理你的参数如下:

Route::group("media", function() { 
    Route::get("{category}", "[email protected]"); 
    Route::get("{category}/{type}/{slug}", "[email protected]"); 
}); 

并在你的方法中,调用这些变量为:

public function singlePost($category, $type, $slug) 
.... 

+0

是的,但我怎么能在这个控制器中获得“视频”? –

+0

结帐我的回答更新 – hassan

+0

似乎没问题!但是,在这个URL localhost:8000/admin/post/new' 404 :((( –

0

您可以使用单一的路线,以及,

Route::get("{category}/{type?}/{slug?}", "[email protected]"); 

在您发表控制器

public function singlePost($category, $type = null, $slug = null) { 


} 

通过这种方式可以实现,方法重载,使用switch语句检索结果,如果只有类别出现,则显示归档,如果类别和类型出现,则显示该类型的数据列表e,如果所有三个都来检索单个帖子。