Web Images Videos Maps News Shopping Google Mail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Please help me understand URL and Routes in Cake
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
barduck  
View profile   Translate to Translated (View Original)
(1 user)  More options 28 Feb 2007, 09:37
From: "barduck" <zevel+goo...@klunsk.com>
Date: Wed, 28 Feb 2007 09:37:51 -0000
Local: Wed 28 Feb 2007 09:37
Subject: Please help me understand URL and Routes in Cake

Hi,

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)

I defined the following Routes:

$Route->connect('/units', array('controller' => 'units', 'action' =>
'index'));
$Route->connect('/units/*', array('controller' => 'units', 'action' =>
'view'));

So far everything works fine.

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.

Thanks in advance!

- barduck


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Lamb  
View profile   Translate to Translated (View Original)
 More options 28 Feb 2007, 15:54
From: Chris Lamb <ch...@chris-lamb.co.uk>
Date: Wed, 28 Feb 2007 15:54:36 +0000
Local: Wed 28 Feb 2007 15:54
Subject: Re: Please help me understand URL and Routes in Cake

> 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

  signature.asc
< 1K Download

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John  
View profile   Translate to Translated (View Original)
 More options 28 Feb 2007, 20:13
From: "John" <johnsv...@googlemail.com>
Date: Wed, 28 Feb 2007 12:13:58 -0800
Local: Wed 28 Feb 2007 20:13
Subject: Re: Please help me understand URL and Routes in Cake

> 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>

Anyway I've got the following in my controller

var $regions = array('london', 'south_west', 'north', 'south_east',
'central', 'wales', 'scotland', 'northern_ireland');

function beforeFilter()
{
if(in_array($this->action, $this->regions))
  {
     $this->region();
     exit;
  }

}

So if the is a URL like /galleries/london it will route to the the
action region() in the controller.

It works well, but I was wondering if there are better solutions?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
barduck  
View profile   Translate to Translated (View Original)
 More options 1 Mar 2007, 08:44
From: "barduck" <zevel+goo...@klunsk.com>
Date: Thu, 01 Mar 2007 00:44:25 -0800
Local: Thurs 1 Mar 2007 08:44
Subject: Re: Please help me understand URL and Routes in Cake
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:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
barduck  
View profile   Translate to Translated (View Original)
 More options 4 Mar 2007, 19:27
From: "barduck" <zevel+goo...@klunsk.com>
Date: Sun, 04 Mar 2007 11:27:33 -0800
Local: Sun 4 Mar 2007 19:27
Subject: Re: Please help me understand URL and Routes in Cake

No one?

Surely someone can offer some additional insights on this.

Thanks.

On Mar 1, 10:44 am, "barduck"  wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bernardo  
View profile   Translate to Translated (View Original)
 More options 4 Mar 2007, 21:26
From: "bernardo" <auxs...@gmail.com>
Date: Sun, 04 Mar 2007 21:26:16 -0000
Local: Sun 4 Mar 2007 21:26
Subject: Re: Please help me understand URL and Routes in Cake
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:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google