简体   繁体   中英

How to remove controller name and action name from URL in Cakephp

I'm adding categories, products (along with category id) in admin panel and i want to show the products list category wise in the simple url as:

localhost/project_folder/[:category_name]

insted of

localhost/project_folder/HomeController/action/[:category_name]

and also i want to show the products deatils in the simple url as:

localhost/project_folder/[:product_id]

insted of

localhost/project_folder/HomeController/action/[:product_id]

I want to show all the details by using HomeController only.

By solution of How to remove action name from url in cakephp? , listing of products by category is working but product details are not working.

I got product list by category_name as below:

Router::connect
    '/:param',
    array('controller' => 'Home', 'action' => 'category'), 
    array('pass' => array('param'))
);

But the details of product is not working if i used below one:

Router::connect(
    '/:param1',
    array('controller' => 'Home', 'action' => 'details'),
    array('pass' => array('param1'))
);

Your router will get confused if you used this configuration (cause they are the same), you have to difference them by defining the nature of 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)
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM