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

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

python - Django-easy-pdf: xhtml2pdf reporting reportlab 2.2+ is required, but 3.0 installed -

collision detection - C++ library for mesh to mesh intersection: what is available? -