I'm trying to set dynamic routes for small CMS. Is there proper way how to do it? I founded somewhere this soliution, but honestly I'm not satisfied with it. CMS have other content types so define this for every model does't seem right to me.
$productsModel = ClassRegistry::init('Product');
$products = $productsModel->find('all');
foreach($products as $product){
Router::connect('/produkty/:id/'.$product['Product']['url'], array('controller' => 'products', 'action' => 'view',$product['Product']['id']));
}
Thanks for any help!
No need to do anything complex :)
In routes.php:
Router::connect('/produkty/*', array('controller'=>'products', 'action'=>'view'));
In products_controller.php:
function view($url = null) {
$product = $this->Product->find('first', array('conditions'=>array('Product.url' => $url)));
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With