On Wed, Feb 4, 2009 at 11:51 AM, Dwayne <[email protected]> wrote:
>
> I've been searching for a way to implement nested routes in cakephp.
> I've seen a few threads on this (for example
> http://groups.google.com/group/cake-php/browse_thread/thread/abd4ffcb3eb363a7/e33566a2a8182b46?lnk=gst&q=nested+routes#e33566a2a8182b46)
> but they've all been at least a few years old so I don't know if
> there's still no way to do this using a default install of cake. My
> suspicion is that there isn't, but it doesn't hurt to ask.
>
> I want this behaviour for a multi-site CMS I'm working on at the
> moment. What I'd like to be able to do is have URLs of the form /
> page_1/page_2/page_3/ where the number of pages is arbitrary, but
> page_2 is a child of page_1 and page_3 is a child of page_2, etc.
> Basically simulating how things would work with static pages in
> hierarchically organized folders. This is complicated by the fact that
> to enable multiple sites managed together the URL might be of the
> form /site/page_1/page_2/page_3/ and that I'd also like to be able to
> access specific actions on a page (so /site/page_1/page_2/page_3/
> category/1 might show a specific category on page_3).

I did something similar. It wasn't multi-site, though. I created a
Content model with this method:

function findIdByPath($path, $lang)
{
        $parts = explode('/', $path);
        $slug = array_pop($parts);
        
        $sql = 'SELECT c.id FROM contents c ';
        $num_parents = count($parts);
        
        for ($i = 1; $i <= $num_parents; $i++)
        {
                $parent = array_pop($parts);
                $sql .= "LEFT JOIN contents p${i} ON p${i}.name_${lang} = 
'${parent}' ";
        }
        
        $sql .= "WHERE c.slug_${lang} = '${slug}'";

        return Set::extract($this->query($sql), '{n}.c.id');
}

The Content model uses TreeBehavior to deal with the hierachy. I
originally thought to give the table a field for the entire
path/route, but ended up simply storing the slug for each page. Thus,
I pop that off the end of the path array and each successive part is
the slug for a parent.

 The controller called it like:

function display()
{
        if (!func_num_args()) $this->redirect('/');
        $path = func_get_args();

        $id = $this->Content->findIdByPath($path, $this->lang);

I had some issues with the fact that the site has 3 languages. This is
why the line above is fetching just the ID at this point. There is
much more after that  to deal with getting the correct content,
language-wise, as well as fetching links to alternative content (eg.
offer links to Spanish & French content when there is no English
version) for a given route.

Looking at it now I see that it could all really use some refactoring.

As for having actions in the path, I can't help you there. That
doesn't seem like something that should be mixed in with this sort of
database-served static content. It should have its own controller. Or
maybe I misunderstood.

> At this point I think that to enable this I'll have to put in a route
> that catches everything and then split it up myself. I'd rather not do
> that, but if I have to I can live with it. The problem I have is I'm
> not sure what the best way would be to efficiently determine what page
> I'm actually looking at. The pages are set up using the Tree behavior,
> so I can get out a breadcrumb to a page pretty quickly once I know the
> page, but what I need to do is the reverse. I have no idea how to do
> that efficiently, especially since the path is embedded between site
> and action sections in the URL, which aren't easily separated out.
>
> Any advice on this would be appreciated. Even just a link to a good
> resource explaining how cakephp routing and/or the Tree behavior works
> (I'm a bit hazy on the both).
>
> >
>

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

Reply via email to