Josh R
Josh R

Reputation: 1295

How to create custom urls in cakephp

I have created a cakephp app. I have urls looks like

www.mysite.com/products/search/hardware

It loads fine. But I want urls that looks like

www.mysite.com/hardware

Can this is achieved by setting route connect

I appreciate any help.

Thanks.

Upvotes: 1

Views: 1664

Answers (3)

damusnet
damusnet

Reputation: 4398

You could add something like

 Router::connect('/hardware',
                  array('controller' => 'products', 'action' => 'search'),
                  array('pass' => array('search'), 'search' => 'hardware'));

in your routes.php file, but then you would have to do it for each searchable item.

The problem you will be facing if you want something automatic, is that you need a way to differentiate your searchable products from every other model you have. So maybe you should settle for another type of URL like

 www.mysite.com/products/hardware

or

 www.mysite.com/s/hardware

and use the appropriate routes accordingly.

Upvotes: 1

JJJ
JJJ

Reputation: 33163

Yes, you can use a route.

Router::connect(
    '/hardware',
    array('controller' => 'products', 'action' => 'search', 'hardware')
);

For a more general solution (any category name routing to products/search) see http://book.cakephp.org/view/948/Defining-Routes

Upvotes: 3

woodscreative
woodscreative

Reputation: 1031

How about creating hardware_controller in your controllers folder.

Upvotes: 0

Related Questions