Laravel博客slug路线定义

时间:2017-04-13 05:51:28

标签: laravel

我正在关注博客教程,我发现这个有趣的博客路线用于显示博客文章

http://sweet-blog.herokuapp.com/interesting-articles-wide-pharmaceutical-and-produts

这是迁移文件https://github.com/28harishkumar/blog/blob/master/database/migrations/2015_05_23_133926_posts.php

这是函数https://github.com/28harishkumar/blog/blob/master/app/Http/Controllers/PostController.php#L85

public function show($slug)
{
    $post = Posts::where('slug',$slug)->first();
    if($post)
    {
        if($post->active == false)
            return redirect('/')->withErrors('requested page not found');
        $comments = $post->comments;    
    }
    else 
    {
        return redirect('/')->withErrors('requested page not found');
    }
    return view('posts.show')->withPost($post)->withComments($comments);
}

这是路线

https://github.com/28harishkumar/blog/blob/master/app/Http/routes.php#L62

Route::get('/{slug}',['as' => 'post', 'uses' => 'PostController@show'])->where('slug', '[A-Za-z0-9-_]+');

当我看到这一行时

public function show($slug)
{
    $post = Posts::where('slug',$slug)->first();

是否在不使用$request获取uri细分的情况下为我们获取了slu g?

如果是这样,我们如何处理uri中的多个参数?

1 个答案:

答案 0 :(得分:1)

uri中的多个参数可以是:

Route::get('/{slug}/{other}',['as' => 'post', 'uses' => 'PostController@show'])->where('slug', '[A-Za-z0-9-_]+');

这里我使用other作为这个uri的第二个参数。你可以根据你的要求使用很多参数。 在控制器中:

public function show($slug, $other) {
 // Your code here   
}

希望它有所帮助。