Jump to content

CakePHP - Am I using routing incorrectly?


CrimpJiggler

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 thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.