Support the ongoing development of Laravel.io →
posted 10 years ago
Views

Hi, I need to build a website use Laravel 4, there are some static pages, such as "about us", "contact us", "our products" and so on.

The URL should like this:

example.com/about-us

However, I cannot use URL like this:

example.com/page/about-us

My question is how should I write my routes? I know I could write something like this:

Route::get('about-us', array('uses' => 'pageController@aboutUs');

When the website become bigger, there could be lots of static pages, do I have to write a route for each page? What is the best approach? Thanks!!

Last updated 3 years ago.
0

Ok, I might have a solution. I could put my page controller at the bottom of my router.php, if the URL can not match any of other route path, so Laravel 4 know it might be a page route.

Route::get('{page}', array('uses' => 'pageController@getPage');

Any other better solutions? Thanks!

Last updated 3 years ago.
0

hetnieuweweb said:

Don't forget to place the '/' before your url.

Like this:

Route::get('/about-us', array('uses' => 'pageController@aboutUs');

When you make a lot of pages, you have to write every route...

That is actually wrong, you don't need the '/' all, unless the page is info/about-us

Last updated 3 years ago.
0

It would be better if you could make it as dynamic and fetch the data from the database.

So you can create this route

Route

Route::get('{slug}', array('as' => 'pages.show', 'uses' => 'PagesController@show'));

Controller

public function show($slug) {

$modulePage = Module::where('slug', $slug)->get(array('module_name', 'module_content'))->first();

return View::make('modulepages', compact('modulePage')); }

And then show the content to your View

<h3 class="page-header">{{$modulePage->module_name}}</h3> <p>{{$modulePage->module_content}}</p>

Hope this helps.

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

dd123123 dd123123 Joined 23 Sep 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.