On Jun 18, 3:01 am, piyush agarwal <[email protected]> wrote: > Hi, > > I am just creating a url i.e. > /items/browse/5/food > > here : controller -> items, action -> browse, id => 5, > category_name => food > > id & category_name are parameters . > > I want to display this url as > > items/shop/food > > here : action browse will be changed as shop , id will not display > then after category_name will be shown. > > please provide me solution. I have tried lot of ways to do this but > not getting success. > help me please.
It's unclear what your models are here. Do you have both a Category and Item Model? If so, is the ID (5) the Item.id or Category.id? Whichever it is, what you want to do is use "slugs" in the URL. To have Cake create the slugs automatically, download SluggableBehavior from here: http://cake-syrup.sourceforge.net/ingredients/sluggable-behavior/ See the tutorial for more info: http://bakery.cakephp.org/articles/view/slug-behavior If "food" is a Category, not Item, I would do this: Router::connect( '/shop/:category_slug', array( 'controller' => 'categories', 'action' => 'view' ), array( 'category_slug' => '[-a-z]+', 'pass' => array('category_slug') ) ); Router::connect( '/shop/:category_slug/:item_slug', array( 'controller' => 'items', 'action' => 'view' ), array( 'category_slug' => '[-a-z]+', 'item_slug' => '[-a-z]+', 'pass' => array('category_slug', 'item_slug') ) ); So, for each of these routes, the slugs are all lowercase letters or hyphens ("food", "something-else", etc.). If you might have numbers in the category or item names, change the regular expression to: [-a-z0-9]+ Category model: var $actsAs = array( 'Sluggable' => array( 'translation' => 'utf-8', 'separator' => '-', 'label' => array('name'), 'length' => 128, 'overwrite' => true ) ); public function fetch($slug) { return $this->find( 'all', array( 'conditions' => array( 'Category.slug' => $slug ) ) ); } Item model: var $actsAs = array( 'Sluggable' => array( 'translation' => 'utf-8', 'separator' => '-', 'label' => array('name'), 'length' => 128, 'overwrite' => true ) ); public function fetch($slug) { return $this->find( 'all', array( 'conditions' => array( 'Item.slug' => $slug ), 'contain' => array( 'Category' ) ) ); } CategoriesController: public function view($category_slug = null) { if (empty($category_slug)) { $this->redirect(array('action' => 'index')); } $this->set( 'data', $this->Category->fetch($category_slug) ); } ItemsController: public function view($category_slug, $item_slug) { if (empty($item_slug)) { $this->redirect( array( 'controller' => 'categories', 'action' => 'view', 'category_slug' => $category_slug ) ); } $this->set( 'data', $this->Item->fetch($item_slug) ); } Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
