Jump to content

CakePHP - Am I using routing incorrectly?


Recommended Posts

I have 5 tables for different kinds of substances (plants, compounds, products, preparations etc.), and I don't make a controller and views for each of these tables, it would make more sense to use a single controller for each of them, so heres what I did, I added this to the routes.php file:

Router::connect('/Substances', array('controller' => 'menus', 'action' => 'display', 'main'));

Router::connect('/Compounds',array('controller' =>  'Substances'));
Router::connect('/Plants',array('controller' => 'Substances'));
Router::connect('/Preparations',array('controller' => 'Substances'));
Router::connect('/Products',array('controller' => 'Substances'));
Router::connect('/Ailments',array('controller' => 'Substances'));

Router::connect('/Plants/:action/*',array('controller' => 'Substances'));
Router::connect('/Preparations/:action/*',array('controller' => 'Substances'));
Router::connect('/Products/:action/*',array('controller' => 'Substances'));
Router::connect('/Ailments/:action/*',array('controller' => 'Substances'));
Router::connect('/Compounds/:action/*',array('controller' =>  'Substances'));

so now they are all directed to a single controller called Substances. In case someone tries to go to /Substances, I routed that to a static, main menu page. In the SubstancesController.php file, I added this:

public $uses = array('Formulation','Compound','Preparation','Ailment','Plant');

and now to find out which table it needs to load the data from, I get the model name from the URL:

$this->modelName = Inflector::classify(explode('/',$this->request->url)[0]); 

 Its up and running, but I'm starting to encounter glitches. I have a feeling I'm doing things the wrong way. I'm hoping some experienced cake programmers here can give me some tips on how they'd handle this kinda scenario. 

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.