• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 114
  • Last Modified:

avoid having 'index' in url when using a variable in cakephp

When you want to have a link like


In Cakephp you have to rewrite it to:


If you don't do this cakephp complains that within the users controller there is no action called 54. Is there no other way around this? Like to tell cakephp that when the second variable in the url is a number (so in this case 54), to refer it to the index function:

public function index($userId = null).

I hope this makes sense
  • 2
1 Solution
Bernard S.Commented:
You might consider doing a mod_rewrite within your htaccess, ie by Apache outside of php itself
ioboxWeb ArchitectCommented:
You should consider start using routes:

Routes and Connected Params

You'll need to create a route that will match your pattern.

http://www.domain.com/users/index/54/John -> This is the default pattern: domain/controller/action/param1/param2/etc...

Open the file /app/Config/routes.php and create at the very top a route that will match your pattern in this way (routes are matched top to bottom)

    array('controller' => 'users', 'action' => 'index'),
         'pass' => array('id', 'slug'),
         'id' => '[0-9]+'

Open in new window

Router::connect in quick:
- Param1: Url to match (/users/:id/:slug),
- Param2: Where the request must be dispatched (controller users, action index)
- Param3: Passed parameters and validation (we pass the id and slug from first param and then validate the param id to make sure it contains only numbers)

Hope it helps.
Bernard S.Commented:
Iobox's answer is highly to the point
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now