Answers for "dynamic url laravel"

PHP
0

dynamic url laravel

public function showBySlug($slug) {
    $post = Post::where('slug','=',$slug)->first();
    // would use app/posts/show.blade.php
    return View::make('posts.show')->with(array(  
        'post' => $post,
    ));
}
Then you can route it like this:

Route::get('post/{slug}', 'PostsController@showBySlug')
    ->where('slug', '[\-_A-Za-z]+');`
...which has the added bonus of allowing you an easy way to link straight to the slug routes on an index page, for example:

@foreach ($posts as $post)
    <h2>{{ HTML::link(
        action('PostsController@showBySlug', array($post->slug)),
        $post->title
    )}}</h2>
@endforeach
Posted by: Guest on March-01-2021

Browse Popular Code Answers by Language