Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic urls in laravel?

Tags:

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?

like image 271
Hailwood Avatar asked Dec 13 '12 12:12

Hailwood


People also ask

What are dynamic URLs?

A dynamic URL is a page address that results from the search of a database-driven web site. Dynamic URLs are the URLs that are generated by a server or a content management system and are not easy to remember. These are created because the server or CMS doesn't know what to call each page.

What is dynamic URL routing?

Dynamic Routing: It is the process of getting dynamic data(variable names) in the URL and then using it. Variable Rules: Variable sections can be added to a URL by marking sections with <variable_name>.

What is @param in laravel?

The required parameters are the parameters that we pass in the URL. Sometimes you want to capture some segments of the URI then this can be done by passing the parameters to the URL. For example, you want to capture the user id from the URL.


1 Answers

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')); 
like image 82
Dee S Avatar answered Sep 18 '22 20:09

Dee S