0
votes

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?.

1
separate the parameters using / like /{$slug}/{$param} - linktoahref
is laravel getting the uri segment slug? - user7851085
Yes the $slug parameter would contain the slug in the url, plus you need to pass an default value to the $slug variable laravel.com/docs/5.4/routing#route-parameters - linktoahref
In your /{$slug}/{$param} how would i access param? - user7851085
public function show($slug = null, $param = null) { // you'll be able to access the $param variable in here - linktoahref

1 Answers

1
votes

multiple parameters in the uri can be as:

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

Here I used other as a second parameter to this uri.You can use many parameters as per your requirements. In Controller:

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

Hope it helps.