Will magic routes for plugins ever exist again? Why just the index() method? Baffling. There are tens if not twenties of plugin junkies out there and we have no voice.

Apsontonk on 3/7/10

@Apsontonk what are you talking about? giving more Router control within your plugin ? or are you talking about /plugin/ pointing to /plugin/controller/index ?

I think Mark’s solution could be used to search for a routes file in your plugins and implement whatever routes you define there. You just need a little creativity. Though it being a default feature sure would be nice.

Angel S. Moreno on 3/7/10

Apsontonk: I doubt the old magic plugin routes will return. They were a source of pain and numerous problems. Of course you can still define routes in your application to provide compatible routes. As for not having a voice, CakePHP is an open source project. Your ‘voice’ in a project is entirely dependent on you and your willingness to get involved. Sorry that you don’t agree with the decisions made, but perhaps if you were more involved a different solution could have been reached.

mark story on 8/7/10

Hey. Thanks for this post.
Do you have a good idea, how I can use your SlugRoute class fot a two layer navigation?

/:mainslug
/:mainslug/:subslug

Bests
Mathias

Mathias on 9/7/10

Hi mark, i am trying to work with subdomains using a custum route class. I am having problems with the link building…

Could you take a look to the code?

#config/routes.php
App::import(‘Lib’, ‘SubdomainRoute’);
define(‘MYDOMAIN’, ‘domain.com.ar’);

$ops=array(‘routeClass’ => ‘SubdomainRoute’);
Router::connect(’/:subdomain/’, array(‘controller’=>‘pages’,‘action’ => ‘display’,‘home’),$ops);
Router::connect(’/:subdomain/:controller’, array(‘action’ => ‘index’),$ops);
Router::connect(’/:subdomain/:controller/:action/*’,array(),$ops);

  1. libs/subdomain_route.php
    class SubdomainRoute extends CakeRoute { function parse($url) { if(defined(‘MYDOMAIN’)){ $subdomain = substr( env(“HTTP_HOST”), 0, strpos(env(“HTTP_HOST”), “.”.MYDOMAIN) ); $url=’/’.$subdomain.$url; }

return parent::parse($url); }

function _writeUrl($params) { $params[‘subdomain’]=isset($params[‘subdomain’])? strtolower($params[‘subdomain’]):‘www’;

$out=parent::_writeUrl($params);

if(!defined(‘MYDOMAIN’)) return $out;

$out=substr($out,strpos($out, ‘/’)+1); $out=substr($out,strpos($out, ‘/’));

return ‘http://’.$params[‘subdomain’].’.’.MYDOMAIN.$out; }
}

when i access to some url like;
http://sub.domain.com/
or
http://sub.domain.com/pages/display/test

everything its okey

but when i build a link using;
echo $this->Html->link(‘test’,array(‘controller’=>‘pages’,‘action’=>‘display’,‘home’,‘subdomain’=>‘www’));

it outputs this url; /app_path/http:/www.domain.com

any ideas?

Eugenio on 11/8/10

@Erik

I passed the id by assigning $params[‘post_id’] in my route class, then adding the optional ‘pass’ paramater in Router::Connect().

Some code will hopefully make more sense – here are the important bits:

$post = ClassRegistry::init(‘Post’)->find(‘first’, array( ‘fields’ => array(‘id’), ‘conditions’ => array(‘Post.slug’ => $params[‘slug’]), ‘recursive’ => -1
));
if ($post) { $params[‘post_id’] = $post[‘Post’][‘id’]; return $params;
}

———

Router::connect(’/:slug/*’, array(‘controller’ => ‘posts’, ‘action’ => ‘view’), array(‘routeClass’ => ‘SlugRoute’, ‘pass’ => array(‘post_id’))
);

Not sure if this is the best way, but it works for me :)

Mark P 4 weeks, 1 day ago

Erik, MarkP

Why not set the ‘pass’ paramater inside parse() directly?

$fpost = $Post->find(‘first’, array( ‘conditions’ => array(‘Post.slug’ => $params[‘slug’]), ‘recursive’ => -1
));

if ($count) { $params[‘pass’][] = $count[‘Post’][‘id’]; return $params;
}

Gabriel 3 weeks, 2 days ago

I have a question about dynamic routing. I’m fairly new to CakePHP, so setting up routing is a little confusing to me.

Background:
I have two tables, ‘webpages’ and ‘webpage_categs’. Here are the pertinent fields of each table:

[webpages]-> (‘id’, ‘url’, ‘webpage_categ_id’, ‘page_html);
[webpage_categs] -> (‘id’, ‘url’, ‘name’)

As you may have guessed, these two tables are for an admin back-end which allows users to add/edit/delete their own static html pages. Each html page is linked to a category with the field ‘webpage_categ_id’.

My main controller is named ‘webpages_controller’, and thus by default you could view a web page by going to ‘/webpages/view/6’ (where 6 is the ‘id’ of the webpage).

Ok, onto my question… Instead of having all of my url’s appear like this: ‘/webpages/view/6’ I’d like them to appear like this: ‘/category/page’ where category=webpage_categs.url and page=webpages.url. So my question is, can I do this dynamically so I don’t have to define a route for each page?

If anyone could help me out or provide suggestions, I’d be very grateful!

Thanks in advance – Ryan

Ryan 2 weeks, 2 days ago

1 2 next >