0

Is it possible to identify a controller and pagename in the same url

Router::connect('/:controller/'); Router::connect('/:pagename/', array('controller' => 'home','action'=>'index'));

www.example.com/controller so that the controller goes to the :controller/index

www.example.com/pagename so that the page goes to home/index

Saneesh B
  • 572
  • 4
  • 13

2 Answers2

0

Its really confusing what really you wants. If a url ends with :controller like www.example.com/posts its generally map index action. Now if the url ends with pagename, means action, like www.example.com/mypage, you can map that as-

Router::connect('/mypage', array('controller' => 'homes', 'action' => 'index'));

So when a user browse www.example.com/mypage, it will map to HomesController and index action.

Fazal Rasel
  • 4,446
  • 2
  • 20
  • 31
0

Try following code for pagename:

Router::connect('/:slug', array('controller' => 'home', 'action' => 'index'));

UPDATE 1

If you want to treat www.example.com/user as same as www.example.com/users then you need to add following, because CakePHP controller name is plural as per CakePHP naming convention:

Router::connect('/user/:action/*', array('controller' => 'users'));

UPDATE 2

Router::connect(':slug') overwrite all of default routing. So you need to use custom routing class as like as :

App::uses('SlugRoute', 'Routing/Route');

Router::connect(
    '/:slug',
    array('controller' => 'home', 'action' => 'index'),
    array('routeClass' => 'SlugRoute')
);
monsur.hoq
  • 1,135
  • 16
  • 25
  • but for this to happen the URL will be www.example.com/Controller/ for a controller www.example.com/Pagename for a page but i want the URL to be same www.example.com/Controller and www.example.com/Pagename and still be handled correctly from my all trials i dont think this can happen since there is no way to distinguish between "Controller" and "page" for the same URL – Saneesh B Apr 06 '14 at 07:16