Skip to content

Instantly share code, notes, and snippets.

@INDIAN2020
Forked from angelmartz/gist:8241201
Created October 16, 2016 23:48
Show Gist options
  • Save INDIAN2020/423fd466a962a3352afb077a65bec901 to your computer and use it in GitHub Desktop.
Save INDIAN2020/423fd466a962a3352afb077a65bec901 to your computer and use it in GitHub Desktop.
Slug Laravel
<?php
// Route:
// {id} required
// {slug} optional
Route::get('blog/{id}/{slug?}', array('as' => 'blog.show', 'uses' => 'PostsController@show'));
// PostsController@show
public function show($id, $slug = '')
{
$post = $this->post->findOrFail($id);
// If slug from url doesn't exist or doesn't match the posts slug
// then redirect to the correct url
if ( $post->slug !== $slug ) {
return Redirect::route('blog.show', array('id' => $post->id, 'slug' => $post->slug), 301);
}
return View::make('posts.show', compact('post'));
}
- See more at: http://laravelsnippets.com/snippets/slug-in-url-but-still-changeable-later-on#sthash.AI8S2adr.dpuf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment