I am trying to understand how friendly URLs and "Routes" work in Cake and have few of questions that might be trivial but I can't seem to find an answer in the documentation.
I am familiar with url rewriting from other platforms where I used a similar functionality of mod rewrite to map logical URLs to physical server paths. I understand that Cake does this in two phases, one using apache mod rewrite to pass the rest of the path to cake and the second one by "Routes" to further route the URL in cake internally. Is this correct?
I assume that the major purpose of the Routes is to map URLs to controllers, functions and parameters.
Questions:
1. I've seen a colon (:) used in the manual in routes config (like / blog/:action/* ). What is the special meaning of the colon? It isn't mentioned anywhere. 2. Can I use regular expressions in Routes like on mod rewrite? How? The manual doesn't mention it. 3. Can I still use URL query string parameters using "?" ? Or does cake only use the /controller/action/param/param... convention?
And now to a real example:
Suppose I have a Model called Unit, so I would like to have a view to show multiple units in table, with various filters and sorted parameters, and a view that shows the information on a single unit.
So, I'd like to have the following URLs /units - for a view showing a table of multiple units /units/<unit name> - for a view of the single unit (no, I don't want to use the action in the URL like /units/view/<unit name>, I'd like to keep this one as short and simple as possible)
Now I would like to accomplish 2 additional things:
1. Have parameters passed to the /units/ index view (like filters and sort columns) on the URL. I would normally do this with query string parameters: /units?sort=column&filer=.....&... How do I do this in Cake so it doesn't interfere with the single unit view?
2. I would like to be able to pass multiple units to the index view and show these units in the same multiple units table (with unknown parameter count). Like this /units/unit1+unit2+units3 OR /units/unit1/unit2/unit3/...
How do I define unknown count of parameters in Routes and how do I do this without interfering with the single unit view?
Hope I am making myself clear. Sorry for the long message.
> I understand that Cake does [routing] this in two phases, one > using apache mod rewrite to pass the rest of the path to cake and the > second one by "Routes" to further route the URL in cake internally. Is > this correct?
Yes.
> I assume that the major purpose of the Routes is to map URLs to > controllers, functions and parameters.
Correct.
> 1. I've seen a colon (:) used in the manual in routes config (like / > blog/:action/* ). What is the special meaning of the colon? It isn't > mentioned anywhere.
They are to control the parameters that are passed to the Controller. I think the syntax is a Ruby-ism. First, the general case. If your route is:
/blog/:spam/*
then if the browser requested
/blog/eggs/
then $this->params['spam'] would contain the value 'eggs'. You can have more than one in the route. For example:
/blog/:year/:month/:day/:slug/*
gets you something like the default WordPress blog link structure. There are two 'magic' parameters, "controller" and "action" which, when set, decide which controller or action to call respectively. For example, the route:
/blog/:action/:spam
when called with:
/blog/view/eggs/
will call the "view" action with $this->params['spam'] set to "eggs".
> 2. Can I use regular expressions in Routes like on mod rewrite? How? > The manual doesn't mention it.
Just use regular Perl-compatible regexs.
> 3. Can I still use URL query string parameters using "?" ? Or does > cake only use the /controller/action/param/param... convention?
Cake has a different method of handling query string parameters. My advice is to construct a controller action to display $this->params and see how they are handled.
> Hope I am making myself clear. Sorry for the long message.
Hopefully someone else can help with the rest if the above does not help you solve the problem yourself. Note that the CakePHP source is very readable for a PHP program, so examining the dispatcher code may make sense than any of this.
> So, I'd like to have the following URLs > /units - for a view showing a table of multiple units > /units/<unit name> - for a view of the single unit (no, I don't > want to use the action in the URL like /units/view/<unit name>, I'd > like to keep this one as short and simple as possible)
I wanted to do something similar, but rather than using routes I put a condition in the beforeFilter of a controller. The controller is called galleries, I wanted to keep the actions like index, view, detail, etc. So I could have /galleries/view/<gallery id> but also to have /galleries/<region name>
I think I have better understanding of the Route functionality now.
I am still not sure I understand the full potential of regular expressions in Route. I mean, I see how I can specify a regex in the matching part of the Route->connect() but in mod rewrite, I can use regular expression both in the matching and the substitutions part of the URL (the groups from the match can be used in the rewritten URL, using (...) in the pattern and $N in the substitution), can something like this be done in Route?
I assume the colon is one way to do this, but does colon-param pattern always needs to come surrounded by slashes (/)? What about the asterisk I used in my route, how come it isn't being confused with the meaning of asterisk in regex?
I am also not sure what is the best way to achieve what I described in my original post. One possible solution is to define one big parameter and parse it myself in my code using a delimiter I set.
So I set unit1+unit2+unit3... as one big parameter and separate it in code myself by the plus signs. Then I set a Route rule to detect plus signs before the normal unit view match. I think this will work also for the parameters to the index view but I wonder if this is the right way to do this and whether cake can do it automatically for me?
Thanks.
- barduck
On Feb 28, 5:54 pm, Chris Lamb <c...@chris-lamb.co.uk> wrote:
> > I understand that Cake does [routing] this in two phases, one > > using apache mod rewrite to pass the rest of the path to cake and the > > second one by "Routes" to further route the URL in cake internally. Is > > this correct?
> Yes.
> > I assume that the major purpose of the Routes is to map URLs to > > controllers, functions and parameters.
> Correct.
> > 1. I've seen a colon (:) used in the manual in routes config (like / > > blog/:action/* ). What is the special meaning of the colon? It isn't > > mentioned anywhere.
> They are to control the parameters that are passed to the Controller. > I think the syntax is a Ruby-ism. First, the general case. If your > route is:
> /blog/:spam/*
> then if the browser requested
> /blog/eggs/
> then $this->params['spam'] would contain the value 'eggs'. You can have > more than one in the route. For example:
> /blog/:year/:month/:day/:slug/*
> gets you something like the default WordPress blog link structure. > There are two 'magic' parameters, "controller" and "action" which, when > set, decide which controller or action to call respectively. For > example, the route:
> /blog/:action/:spam
> when called with:
> /blog/view/eggs/
> will call the "view" action with $this->params['spam'] set to "eggs".
> > 2. Can I use regular expressions in Routes like on mod rewrite? How? > > The manual doesn't mention it.
> Just use regular Perl-compatible regexs.
> > 3. Can I still use URL query string parameters using "?" ? Or does > > cake only use the /controller/action/param/param... convention?
> Cake has a different method of handling query string parameters. My > advice is to construct a controller action to display $this->params and > see how they are handled.
> > Hope I am making myself clear. Sorry for the long message.
> Hopefully someone else can help with the rest if the above does not > help you solve the problem yourself. Note that the CakePHP source is > very readable for a PHP program, so examining the dispatcher code may > make sense than any of this.
> Best wishes,
> -- > Chris Lamb, Leamington Spa, UK GPG: 0x634F9A20
> I think I have better understanding of the Route functionality now.
> I am still not sure I understand the full potential of regular > expressions in Route. I mean, I see how I can specify a regex in the > matching part of the Route->connect() but in mod rewrite, I can use > regular expression both in the matching and the substitutions part of > the URL (the groups from the match can be used in the rewritten URL, > using (...) in the pattern and $N in the substitution), can something > like this be done in Route?
> I assume the colon is one way to do this, but does colon-param pattern > always needs to come surrounded by slashes (/)? What about the > asterisk I used in my route, how come it isn't being confused with the > meaning of asterisk in regex?
> I am also not sure what is the best way to achieve what I described in > my original post. One possible solution is to define one big parameter > and parse it myself in my code using a delimiter I set.
> So I set unit1+unit2+unit3... as one big parameter and separate it in > code myself by the plus signs. Then I set a Route rule to detect plus > signs before the normal unit view match. I think this will work also > for the parameters to the index view but I wonder if this is the right > way to do this and whether cake can do it automatically for me?
> Thanks.
> - barduck
> On Feb 28, 5:54 pm, Chris Lamb <c...@chris-lamb.co.uk> wrote:
> > > I understand that Cake does [routing] this in two phases, one > > > using apache mod rewrite to pass the rest of the path to cake and the > > > second one by "Routes" to further route the URL in cake internally. Is > > > this correct?
> > Yes.
> > > I assume that the major purpose of the Routes is to map URLs to > > > controllers, functions and parameters.
> > Correct.
> > > 1. I've seen a colon (:) used in the manual in routes config (like / > > > blog/:action/* ). What is the special meaning of the colon? It isn't > > > mentioned anywhere.
> > They are to control the parameters that are passed to the Controller. > > I think the syntax is a Ruby-ism. First, the general case. If your > > route is:
> > /blog/:spam/*
> > then if the browser requested
> > /blog/eggs/
> > then $this->params['spam'] would contain the value 'eggs'. You can have > > more than one in the route. For example:
> > /blog/:year/:month/:day/:slug/*
> > gets you something like the default WordPress blog link structure. > > There are two 'magic' parameters, "controller" and "action" which, when > > set, decide which controller or action to call respectively. For > > example, the route:
> > /blog/:action/:spam
> > when called with:
> > /blog/view/eggs/
> > will call the "view" action with $this->params['spam'] set to "eggs".
> > > 2. Can I use regular expressions in Routes like on mod rewrite? How? > > > The manual doesn't mention it.
> > Just use regular Perl-compatible regexs.
> > > 3. Can I still use URL query string parameters using "?" ? Or does > > > cake only use the /controller/action/param/param... convention?
> > Cake has a different method of handling query string parameters. My > > advice is to construct a controller action to display $this->params and > > see how they are handled.
> > > Hope I am making myself clear. Sorry for the long message.
> > Hopefully someone else can help with the rest if the above does not > > help you solve the problem yourself. Note that the CakePHP source is > > very readable for a PHP program, so examining the dispatcher code may > > make sense than any of this.
> > Best wishes,
> > -- > > Chris Lamb, Leamington Spa, UK GPG: 0x634F9A20
For sort and filter you can certainly use query string parameters. Have you tried that? I don't think you will any problem there.
For the problem of passing multiple units you don't need to use a special separator, cakephp will pass anything matched by * as additional arguments to your controller function. For example if you have $Route->connect('/units/*', array('controller' => 'units', 'action' =>'view'));
you can use
function view() { $sort = $_GET['sort']; $units = func_get_args(); ...
On Mar 4, 4:27 pm, "barduck" <zevel+goo...@klunsk.com> wrote:
> Surely someone can offer some additional insights on this.
> Thanks.
> On Mar 1, 10:44 am, "barduck" wrote:
> > Thanks for the reply.
> > I think I have better understanding of the Route functionality now.
> > I am still not sure I understand the full potential of regular > > expressions in Route. I mean, I see how I can specify a regex in the > > matching part of the Route->connect() but in mod rewrite, I can use > > regular expression both in the matching and the substitutions part of > > the URL (the groups from the match can be used in the rewritten URL, > > using (...) in the pattern and $N in the substitution), can something > > like this be done in Route?
> > I assume the colon is one way to do this, but does colon-param pattern > > always needs to come surrounded by slashes (/)? What about the > > asterisk I used in my route, how come it isn't being confused with the > > meaning of asterisk in regex?
> > I am also not sure what is the best way to achieve what I described in > > my original post. One possible solution is to define one big parameter > > and parse it myself in my code using a delimiter I set.
> > So I set unit1+unit2+unit3... as one big parameter and separate it in > > code myself by the plus signs. Then I set a Route rule to detect plus > > signs before the normal unit view match. I think this will work also > > for the parameters to the index view but I wonder if this is the right > > way to do this and whether cake can do it automatically for me?
> > Thanks.
> > - barduck
> > On Feb 28, 5:54 pm, Chris Lamb <c...@chris-lamb.co.uk> wrote:
> > > > I understand that Cake does [routing] this in two phases, one > > > > using apache mod rewrite to pass the rest of the path to cake and the > > > > second one by "Routes" to further route the URL in cake internally. Is > > > > this correct?
> > > Yes.
> > > > I assume that the major purpose of the Routes is to map URLs to > > > > controllers, functions and parameters.
> > > Correct.
> > > > 1. I've seen a colon (:) used in the manual in routes config (like / > > > > blog/:action/* ). What is the special meaning of the colon? It isn't > > > > mentioned anywhere.
> > > They are to control the parameters that are passed to the Controller. > > > I think the syntax is a Ruby-ism. First, the general case. If your > > > route is:
> > > /blog/:spam/*
> > > then if the browser requested
> > > /blog/eggs/
> > > then $this->params['spam'] would contain the value 'eggs'. You can have > > > more than one in the route. For example:
> > > /blog/:year/:month/:day/:slug/*
> > > gets you something like the default WordPress blog link structure. > > > There are two 'magic' parameters, "controller" and "action" which, when > > > set, decide which controller or action to call respectively. For > > > example, the route:
> > > /blog/:action/:spam
> > > when called with:
> > > /blog/view/eggs/
> > > will call the "view" action with $this->params['spam'] set to "eggs".
> > > > 2. Can I use regular expressions in Routes like on mod rewrite? How? > > > > The manual doesn't mention it.
> > > Just use regular Perl-compatible regexs.
> > > > 3. Can I still use URL query string parameters using "?" ? Or does > > > > cake only use the /controller/action/param/param... convention?
> > > Cake has a different method of handling query string parameters. My > > > advice is to construct a controller action to display $this->params and > > > see how they are handled.
> > > > Hope I am making myself clear. Sorry for the long message.
> > > Hopefully someone else can help with the rest if the above does not > > > help you solve the problem yourself. Note that the CakePHP source is > > > very readable for a PHP program, so examining the dispatcher code may > > > make sense than any of this.
> > > Best wishes,
> > > -- > > > Chris Lamb, Leamington Spa, UK GPG: 0x634F9A20