php - Laravel 4 - Route to the controller with an optional parameter -


how can make routing optional parameter not raise error if there no parameter provided ?

my app/routes.php looks this:

route::get('/{slug}', 'pagecontroller@page'); 

and app/controllers/pagecontroller.php:

class pagecontroller extends basecontroller {      public $layout = 'templates.default.tpl';      public function page( $slug = 'front' )     {         return view::make('pages.'.$slug);     }  } 

so if go www.websiteurl.com/ , without parameter, should arrive default front page. instead i'am getting error

symfony \ component \ httpkernel \ exception \ notfoundhttpexception 

how can tell laravel4, make routing 2nd variable optional ?

you should define route like:

route::get('/{slug?}', 'pagecontroller@page'); 

questionmark tells laravel parameter optional. more on docs


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -