Thanks for such a comprehensive reply! Just trying things out, and the first part doesnt seem to work,
I'm trying to get www.domain.com/slug to actually be www.domain.com/artists/view/slug Any ideas? d. On 30 June, 19:17, cricket <zijn.digi...@gmail.com> wrote: > On Wed, Jun 30, 2010 at 6:02 AM, designv...@gmail.com > > <designv...@gmail.com> wrote: > > hi there, > > > I have (imo) a slightly tricky routing issue. > > > I have a store setup with various artists and the artists have their > > own mini stores with custom layout etc... > > > Now I want to route the following; > > >www.domain.com/artist_slug -> /artists/view/artist_slug > > If you mean thatwww.domain.com/artist_slugshould point to: > > controller => artists > view => view > pass => artist_slug > > ... then, yes, that's pretty simple. > > Router::connect( > '/artists/:slug', > array('controller' => 'artists', 'action' => 'view'), > array('slug' => '[-a-z]+', 'pass' => array('slug')) > ); > > If you may have underscores, then use '[_-a-z]+'. If digits also, > '[_-a-z0-9]+'. > > Then, you controller method should look like: > > function view($slug = null) { > > The only issue here is that you must explicitly provide routes for any > other non-admin ArtistsController actions *before* this route. > > > and also: > > >www.domain.com/artist_slug/merch/merch_slug -> /artists/view_merch/ > > artist_slug/merch_slug > > This is a bit trickier because you're starting the URL with a slug. So > you'd need to provide every route for *all* controllers before this > route. You'd need to do this because Cake can figure out a route even > if it's not listed in routes.php. But, because the first part of the > URL is a wildcard, all other routes must then be explicit. > > Question: why do you need a slug for both the artist and merchant? Are > they not the same? Also, you could get around the above issue by > doing: > > Router::connect( > '/merch/:slug', > array('controller' => 'artists', 'action' => 'merch_view'), > array('slug' => '[-a-z]+', 'pass' => array('slug')) > ); > > public function merch_view($slug = null) { > > IOW, you're URL would bewww.domain.com/merch/slug > > > I have tried a ton of things and they all seem to fail - do I need to > > build the routes dynamically from the DB? > > There is a way to to connect routes on the fly by querying the > database. It has the benefit that you wouldn't have to register all > other routes because you'd be assigning a route with the explicit > slug, as opposed to using a regexp. Say that you want the URL to start > with the artist slug, but you don't want to have to register every > other route for your app. At the very end of routes.php: > > App::import('Model', 'Artist'); > $artist = new Artist(); > $slugs = $artist->find( > 'list', > array( > 'fields' => array('Artist.slug'), > 'order' => 'Artist.slug DESC' > ) > ); > Router::connect( > '/:slug', > array('controller' => 'artists', 'action' => 'view'), > array('slug' => implode($slugs, '|'), 'pass' => array('slug')) > ); > > So, your 2nd array would actually be something like: > > 'slug' => 'foo|bar|fred|wilma|barney|betty|etc' > > I got this from an article somewhere for making the PagesController > get it's pages from the database. > > However, there's a better way (AFAICT) that I figured out. Instead of > fetching the slugs from the DB on every single request, cache the > output. > > In bootstrap.php: > > $slugs = Cache::read('artist_slugs'); > > if (empty($slugs)) > { > App::import('Model', 'Artist'); > $artist = new Artist(); > $slugs = $artist->find( > 'list', > array( > 'fields' => array('Artist.slug'), > 'order' => 'Artist.slug DESC' > ) > ); > Cache::write('artist_slugs', $slugs);} > > Configure::write('artist_slugs', $slugs); > > In routes.php: > > Router::connect( > '/:slug', > array('controller' => 'pages', 'action' => 'view'), > array('slug' => implode(Configure::read('artist_slugs'), '|'), 'pass' > => array('slug')) > ); > > So, any time an artist is added or deleted, you'd want to remove the > cache file so that it'll be rebuilt on the next request. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en