本文介绍了CakePHP中的动态路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为小型CMS设定动态路径。有正确的方法怎么办呢?我在某处创建了这个soliution,但老实说,我不满意。 CMS有其他内容类型,所以定义这对于每个模型不适合我。

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']));
}

感谢任何帮助!

推荐答案

无需执行任何复杂操作:)

No need to do anything complex :)

在routes.php:

In routes.php:

Router::connect('/produkty/*', array('controller'=>'products', 'action'=>'view'));

在products_controller.php中:

In products_controller.php:

function view($url = null) {
    $product = $this->Product->find('first', array('conditions'=>array('Product.url' => $url)));
    ...
}

这篇关于CakePHP中的动态路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:41