laravel dynamic url parameters code example

Example: 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

Tags:

Php Example