On Sat, Jul 11, 2009 at 6:05 AM, ojonam<[email protected]> wrote:
>
> Hello all,
>
> I am trying to code an app for a music festival. There are websites
> for a few years, which all follow the following convention :
>
> www.mysite.com/year (for example, this year's website should link to
> www.mysite.com/2009)
>
> As the models and controllers for all years are more or less the same
> (only the views change), I thought that I should use the routing
> mechanism to get to the right controller :
>
> basically, www.mysite.com/2009/bands/view/1 should actually reroute to
> www.mysite.com/bands/view/1.
>
> Here is the routing rule I wrote :
>
> Router::connect('/:year/:controller/:action/:thing',
>      array('thing' => null),
>      array('year' => '200[6-9]','thing' => '[0-9]+')
>  );
>
> The problem is that if I try getting to www.mysite.com/2009/bands/view/1,
> I am told that the id is null. What am I doing wrong here ?

Why 'thing'? Why not 'id'?

In any case, does your view action have a $thing ($id) param (ie.
function view($thing) )? If so, you need to pass that from the route:

Router::connect(
        '/:year/:controller/:action/:thing',
        array('thing' => null),
        array(
                'year' => '200[6-9]',
                'thing' => '[0-9]+',
                'pass' => array('thing')
        )
);

You'd still need to get year from $this->params, though, unless you
also passed that. I also think your regexp for year is a bit
limiting--you'll need to change that next year. Better to use
something a bit more forward-looking: '20[0-9]{2}'

If I didn't have too many controllers, I'd be tempted to create
explicit routes for them:

Router::connect(
        '/:year/bands',
        array(
                'controller' => 'bands',
                'action' => 'index'
        ),
        array(
                'year' => '20[0-9]{2}',
                'id' => '[0-9]+',
                'pass' => array(
                        'year',
                        'id'
                )
        )
);

Router::connect(
        '/:year/bands/view/:id',
        array(
                'controller' => 'bands',
                'action' => 'view'
        ),
        array(
                'year' => '20[0-9]{2}',
                'id' => '[0-9]+',
                'pass' => array(
                        'year',
                        'id'
                )
        )
);

... etc. Even better would be to create slugs for your bands:

Router::connect(
        '/:year/bands/view/:slug',
        array(
                'controller' => 'bands',
                'action' => 'view'
        ),
        array(
                'year' => '20[0-9]{2}',
                'slug' => '[-_a-z0-9]+',
                'pass' => array(
                        'year',
                        'slug'
                )
        )
);

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