Dynamic urls in laravel?

18,937

Solution 1

For Laravel 4 do this

Route::get('{slug}', function($slug) {
    $page = Page::where('slug', '=', $slug)->first();

    if ( is_null($page) )
        // use either one of the two lines below. I prefer the second now
        // return Event::first('404');
        App::abort(404);

    return View::make('pages.show', array('page' => $page));
});

// for controllers and views
Route::get('{page}', array('as' => 'pages.show', 'uses' => 'PageController@show'));

Solution 2

You could use the route wildcards for the job, you can start with an (:any) and if you need multiple url segments add an optional (:all?), then identify the page from the slug.

For example:

Route::get('(:any)', function($slug) {
    $page = Page::where_slug($slug)->first();

    if ( is_null($page) )
        return Event::first('404');

    return View::make('page')->with($page);
});

Solution 3

Very similar to Charles' answer, but in the controller:

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
Share:
18,937

Related videos on Youtube

Hailwood
Author by

Hailwood

I could tell you all about me... but I'd prefer to let my work do the talking for me!

Updated on June 28, 2022

Comments

  • Hailwood
    Hailwood about 2 years

    I am looking at switching to laravel for my next project.

    My next project is probably going to be a small site with a few static pages, a blog and a projects manager and will be using controllers not routes.

    What I am curious about is how I can manage dynamic routes in Laravel.

    Basically, I want to build in an admin section so I can easily create the static pages on the fly, and the static pages will have SEO focussed urls, e.g. http://domain.com/when-it-started I do not want to have to create a new controller or route manually for each page.

    So I am wondering what the cleanest way is to handle this.

    essentially all static pages are going to share the same view, just a few variables to change.

    The dynamic routing should work with the controllers not instead of.

    E.g. if we have a controller about with a function staff then this should be loaded via http://domain.com/about/staff

    but we dont have the function players, so a call to http://domain.com/about/players should check the database to see if a dynamic route exists and matches. If it does display that, otherwise show the 404 page. Likewise for a non-existant controller. (e.g. there would not be a when-it-started controller!)

    The chosen answer doesn't seem to work in Laravel 4. Any help with that?

    • Xavi López
      Xavi López over 11 years
      @anujarora Arbitrarily emphasizing words of questions/answers with bold for "better highlights" isn't considered a good edit. Actually, it can be considered actively harmful. Please, take a look at Is editing posts to only introduce bold emphasis acceptable?.
    • anuj arora
      anuj arora over 11 years
      i am not making extra efforts for emphasis. I have highlighted only words that should catch the notice and helps in better understanding.
    • psubsee2003
      psubsee2003 over 11 years
      @anujarora but the point that Xavi is making is that type of editing is considered unacceptable by most members the community. fixing grammer and spelling is good, but adding "emphasis" is not something that is thought to add to the post. Please go to the link that Xavi provided to discuss
    • anuj arora
      anuj arora over 11 years
      ok fine. I will read the whole question and then judge that it needs some editing or not. And also edit only the spelling mistakes and codes not highlighted.
  • Павел Иванов
    Павел Иванов about 10 years
    how it works? Page is model? what is requirments for views/controllers?
  • Dee S
    Dee S about 10 years
    Yes Page is a model, updated to show codes for using controllers and views. Hope that helps.