Let's back up. You mentioned 2 different URLs in the 1st msg:
/games/view/123/Starcraft
/games/view/Starcraft
The route only has a placeholder for title:
'/games/view/:title'
That means that the 1st URL will never be matched. Additionally, the
params include id, which isn't in the URL that will match.
If you want to be able to use both URL forms, create 2 routes:
Router::connect(
'/games/view/:title',
array(
'controller' => 'games',
'action' => 'viewByTitle'
),
array(
'pass' => array('title'),
'title' => '[a-zA-Z0-9_-]+'
)
);
Router::connect(
'/games/view/:id/:title',
array(
'controller' => 'games',
'action' => 'view'
),
array(
'pass' => array('id', 'title'),
'id' => '[0-9]+',
'title' => '[a-zA-Z0-9_-]+'
)
);
controller:
function viewByTtle($title = null)
{
//...
$this->render('view');
}
function view($id = null, $title = null)
{
// ...
}
However, you have to ask yourself why you want both title and id in
the URL, since either is sufficient to find the row in the DB. What
I've been doing is to use id solely for edit and delte actions, which
are likely to be admin actions, anyway. Otherwise, I use a slug. If I
have the following route:
Router::connect(
'/games/view/:slug',
array('controller' => 'games', 'action' => 'viewBySlug'),
array(
'pass' => array('slug'),
'slug' => '[a-z0-9_-]+'
)
);
... I can create my links like so:
echo $html->link(
$game['Game']['title'],
array(
'controller' => 'games',
'action' => 'viewBySlug',
'slug' => $game['Game']['slug']
),
array('title' => 'check it out')
);
controller:
function view($slug = null)
{
if (is_numeric($slug))
{
// treat it as an ID
}
else
{
// treat it as a slug
}
...
}
In fact, I generally remove the 'view' part of the route:
Router::connect(
'/games/:slug',
...
);
So the link could be "/games/Starcraft". But doing so means that I
have to take care to cover the routes for any other actions.
You should really think about using a slug instead of title, anyway.
The latter might contain all sorts of funkiness that could become a
huge pain when passed in the URL and, thus, as a param.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---