Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Seth27's avatar

Laravel - How to generate Page dynamically

Hi All,

I am a bit new in Laravel and I researched on google for couple of hours to get an example of to be able for the users on my website to create a new page / Edit and delete the pages. I want also these pages appearing on the menu, so I think I should store my url for each page entered by the users but I do not really know how I can handle it properly.

Anyone faced this and have a good example of it ? Something very simple ? I create a menu Dynamically it is adding the menu and managing the position of each items but for the url I need to manage pages and to create them and to manage the rout on the web.php but dynamically.

I hope it is enough clear and you will be able to help me.

Thanks a lot in advance.

0 likes
8 replies
Cronix's avatar

For each page (assume you have a "pages" table, with a "Page" model), I'd have a slug in the database that would then be the url. You'd only need a single route to handle all pages. Well, that depends on what you're doing, but it would be something like:

// this route should be very last in your routes file
Route::get('/{page}', 'PagesController@show');  
use App\Page;

class PagesController extends Controller {

    // Tell laravel to retrieve the model by the $slug instead of the default $id
    public function getRouteKeyName()
    {
        return 'slug';
    }

    // Show the requested page
    public show(Page $page)
    {
        return view('pages', compact('page'));
    }
}

Most pages have a title. You could just use str_slug($title) to create a slug from the title. You'd also want to verify that the slug doesn't already exist in the db when creating an article. https://laravel.com/docs/5.5/helpers#method-str-slug

Your database would look something like:

pages
-id
-slug
-title
-body
-etc

Then you'd just

http://yoursite.com/some-page-slug
http:://yoursite.com/some-other-page

This might not be 100% correct code as I just typed it off the top of my head, but should help get you there.

6 likes
Seth27's avatar

Thanks a lot for your quick answer !

Other question how do you manage the view part to be fully dynamic ?

Cronix's avatar

Define fully dynamic?

<title>{{ $page->title }}</title>

<div class="main-content">{{ nl2br($page->body) }}</div>
Seth27's avatar

Hi,

I am facing this issue: No query results for model [App\Page].

I think is due to the model function, anyone have an idea how to do it ? I create a CRUD also to create each pages with title / body / slug. Now I am trying to see if I generate the pages properly and to access via the url I added for each page... But I am stuck :(

Seth27's avatar

Apologies ! My bad I added a "/" on the slug and that's why it was not working .... It's working perfectly the body change for each page !

I just have to create a rich text editor for the body to add html / pictures etc... to the pages to make it look better ! :)

Thanks a lot for your help again !

simondavies's avatar

@CRONIX - Nice solution, but how would you then take it further regards to child pages?

So /{page}/{child-page?}

The route model binding would be on the first variable so would need to create your own ???

Old post but still relevant these days

Looking at present there is a way to use the single route param, but still get the url request.

$this->get('/{page}', 'PageController@index')->where('page', '.*');

The important part is where('page', '.*') where it then allows the forward slash in the url.

So if we then do.

sitename.com/parent/child

sitename.com/parent/child/child

So the controller would then get

parent/child or parent/child/child

you could then replace the forward slash to a '-' so

'parent-child-child'

then do the search on the slug named that???

Just thoughts

Cronix's avatar

@simondavies

The route model binding would be on the first variable so would need to create your own ???

I'm not sure what you mean by that. The route model binding will take place on any parameter(s) where you typehint the class in the controller method, ie this route

/{page}/{childpage?}

going to a show method

show(Page $page, ChildPage $childpage)

would eager load both $page and $childpage objects since Page and ChildPage classes were both typehinted.

show($page, ChildPage $childpage)

would only load the ChildPage object as $childpage, with $page being whatever literal value was sent in the url since the Page class wasn't typehinted.

Also, in the case of a child-parent relationship (subpage belonging to page), you'd really just need to use route model binding on the child, and load the parent relationship to get the parent. You wouldn't need to use route model binding to retrieve both models (although you sure can)

simondavies's avatar

@CRONIX - yeah! i would have done the typehinted both method as the second one would never get the page details if it was just a parent sent, so you could do it say in the controller like..


/{page}/{childpage?}


show($page, $childpage ==null) {

 if($childpage){
   $page = Page::details($childpage);
} else {
    $page = Page::details($page);
}

The other method using the route where clause i mentioned could reduce it though my thoughts


visit: /film/star-wars

$this->get('/{page}', 'PageController@index')->where('page', '.*');

show($page){

    $page = Page::where('page_identifier',$page); //// so where page_identifier = film/star-wars

}

Thanks for the reply, just whats best practice :-)

Please or to participate in this conversation.