Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic routing in CakePHP

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!

like image 800
s7anley Avatar asked May 24 '26 07:05

s7anley


1 Answers

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)));
    ...
}
like image 174
Tyler Avatar answered May 26 '26 21:05

Tyler