php - How to make an action for missing actions? -
i new cakephp.
i working on controller so:
class designerscontroller extends appcontroller { var $name = 'designers'; function index() { $data = $this->designer->find('all'); $this->set('designers', $data); } function belle_etoile() { $this->show_designer("belle etoile"); } function ritani() { $this->show_designer("ritani"); } function swarovski() { $this->show_designer("swarovski"); } function verragio() { $this->show_designer("verragio"); } private function show_designer($designer) { $this->layout = 'first'; $data = $this->designer->find('first', array('conditions' => array('designer.name' => $designer))); $this->set('data', $data); $this->render('show_designer'); } } as can see many of "actions" shortcuts show_designer/param action param name of shortcut action.
every 1 of these actions "designer" in database. don't want have make url designers/show_designer/ritani, rather designers/ritani.
this works, problem is:
i have create bunch of redundant functions every designer, , if new designer gets added, won't work until add function it.
i rather have function/action runs if action requested missing, , has action requested parameter
so if request url designers/stardust, since stardust not defined action call catch_all action stardust parameter.
so instead of bunch of redundant functions have this:
function catch_all($action) { $this->show_designer($action) } is there anyway this?
use routing instead
// add app/config/routes.php router::connect('/designer/*', array('controller' => 'designers', 'action' => 'designer')); in controller
// , remove actions 'belle_etoile', 'swarovski' etc // change `show_designer` `public designer` class designerscontroller extends appcontroller { var $name = 'designers'; function designer($name) { $this->layout = 'first'; $data = $this->designer->find('first', array('conditions' => array('designer.name' => $name))); if(!empty($data)) { $this->set('data', $data); $this->render('show_designer'); } else { $this->redirect('index'); } } }
Comments
Post a Comment