我有一个电子商务网站,需要创建一些自定义网址。这些URL将动态生成。结构将是
示例网址:http://website.com/categoryname/product-title
此处类别名称是动态的。它会改变每个产品,它可以是
website.com/buttons/CP1或 website.com/ribbons/red等
在任何情况下,这些网址都应重定向到http://website.com/products/productdetail/product-title
我尝试了以下脚本:
$routes->connect(
'/:type/:slug/*',
['controller' => 'Products','action' => 'productDetail'],
['type' => '\b(?:(?!admin)\w)+\b'],
[
'slug' => '[A-Za-z-]+',
'pass' => [
'slug'
]
]
);
['type' => '\b(?:(?!admin)\w)+\b']
是排除其他控制器。在这种情况下'管理员'。这是有效的,但通过URL(产品标题)传递的参数没有进入控制器功能(productDetail)
任何帮助将不胜感激。
它是固定的。更新了下面的脚本。
$routes->connect(
'/:type/:slug/*',
['controller' => 'Products','action' => 'productDetail'],
[
'type' => '\b(?:(?!admin)\w)+\b',
'pass' => ['slug']
]);
答案 0 :(得分:0)
根据API,connect()
函数只接受3个参数,而您实际上正在传递4.请参阅Cake Routing Router - Connect
我根本没有测试过这个,但我认为你应该像这样重写它:
$routes->connect(
'/:type/:slug/*',
['controller' => 'Products','action' => 'productDetail'],
[
'type' => '\b(?:(?!admin)\w)+\b',
'slug' => '[A-Za-z-]+',
'pass' => ['slug']
]
);