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

LadyDeathKZN's avatar

Disable Url if page is not published

Good day, I have finally managed to get a somewhat stable CMS working, though I don't know how to disable the url in the front end if the page is in draft.

web.php //Cms Page Route::resource('/commander/pages', 'Admin\CmsController'); //Front End Cms Page Route::match(['post', 'get'], '/{url}', 'Admin\CmsController@frontPage');

Admin\CmsController.php public function frontPage($url){ $cmspage = CmsPage::where('url', $url)->first(); return view('frontend.frontpage', compact('cmspage')); }

I have no idea how to do this, I have tried ::where('status', 'published)->get('url');

And yes I am a newbie with this so please DO laugh haha, any advice and guidance welcome.

0 likes
3 replies
Screenbeetle's avatar
Level 15

You may be overthinking this.

One simple option is to have a condition in your PageController's show method (or CmsController's frontPage method as you have it) to check the draft status and have different return statements depending

Firstly its worth noting you can use a url slug as a unique identifier in your page (or cmspage) model. You just need a unique slug field in your pages table and then add this to your model:

public function getRouteKeyName()
{
    return 'slug';
}

at this point Laravel route model binding will get the model from the slug instead of id.

 // you have this as frontPage($url) but it seems like a show method
public function show(Page $page)
{ 
    if($page->status == 'draft'){
        return redirect('somewhere');
    }

    return view('frontend.frontpage', compact('page')); 
} 
LadyDeathKZN's avatar

Thank you so much, I had completely forgotten about the slug function inside the model!! Life saver :D

Please or to participate in this conversation.