Wednesday, May 9, 2012

Re: Little help with routes and links

First off, I suggest you add slug columns to both your tables. You
might eventually have color names like "rosy red" which should be
sluggified. And I wouldn't count on the product names always being a
single word.

So, this is one way to do what you want:

Router::connect(
'/items/:color/:slug',
array(
'controller' => 'products',
'action' => 'view'
),
array(
'color' => implode('|', Cache::read('product_colors')),
'slug' => '[a-z0-9]+',
'pass' => array('slug', 'color')
)
);

bootstrap.php:

$colors = Cache::read('product_colors');

if (empty($colors))
{
App::import('Model', 'Color');
$Color = new Color();
$colors = $Color->find(
'list',
array(
'fields' => array('Color.slug')
)
);
Cache::write('product_colors', $colors);
}

controller:

public function view($slug = null, $color = null) { ... }

That should work fine but if you're using Cake 2.x you'd probably be
better off creating a route class by extending CakeRoute.

On Tue, May 8, 2012 at 8:26 AM, Confused Dev <confused.devel@gmail.com> wrote:
> Hello.
>
> I need help with defining some unusual routes.
>
> Here are my 2 tables Colors and Products:
>
> mysql> select * from colors;
> +----+------+
> | id | name |
> +----+------+
> |  1 | blue |
> +----+------+
>
> mysql> select * from products;
> +----+------+----------+
> | id | name | color_id |
> +----+------+----------+
> |  1 | bowl |        1 |
> +----+------+----------+
>
>
> Normally Cake will produce links like this:
> http://example.com/products/view/1
>
> I'd like to have links like this:
> http://example.com/items/blue/bowl/1
>
> I don't even know where to begin to make it produce automatic links
> like that. Any help would be greatly appreciated.
>
> --
> Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
> Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
>
>
> To unsubscribe from this group, send email to
> cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate