Wednesday, April 12, 2017

Laravel blog slug route definition

I am following a blog tutorial and i found this interesting blog route used to display a blog post

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

This is the migration file https://github.com/28harishkumar/blog/blob/master/database/migrations/2015_05_23_133926_posts.php

This is the function 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);
}

and this is the route

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-_]+');

When i look at this lines

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

is laravel getting the slug for us without using $request to get the uri segment?

If so, how would we handle multiple parameters in the uri?.



via Geoffrey Isle

Advertisement