我在管理面板中添加了类别,产品(以及类别ID),我希望在简单网址中明确显示产品列表类别:
localhost/project_folder/[:category_name]
insted of
localhost/project_folder/HomeController/action/[:category_name]
我还希望在简单网址中显示产品deatils:
localhost/project_folder/[:product_id]
insted of
localhost/project_folder/HomeController/action/[:product_id]
我想通过仅使用HomeController来显示所有细节。
通过How to remove action name from url in cakephp?的解决方案,按类别列出的产品正在生效,但产品详细信息无效。
我按category_name
获得了产品清单,如下所示:
Router::connect
'/:param',
array('controller' => 'Home', 'action' => 'category'),
array('pass' => array('param'))
);
但如果我使用下面的产品,那么产品的细节就不起作用了:
Router::connect(
'/:param1',
array('controller' => 'Home', 'action' => 'details'),
array('pass' => array('param1'))
);
答案 0 :(得分:0)
如果您使用此配置(因为它们是相同的),您的路由器会感到困惑,您必须通过定义param
Router::connect('/:param', array('controller' => 'Home', 'action' => 'category'), array(
'pass' => array('param'),
'param' => '[a-zA-Z_]{1}[a-zA-Z_0-9]*' // Accepts just words (category names slugged with _ caracter)
));
Router::connect('/:param1', array('controller' => 'Home', 'action' => 'details'), array(
'pass' => array('param1'),
'param1' => '[1-9]{1}[0-9]*' //Accepts just integers (product ids)
));