Vahid Alimohamadi
Vahid Alimohamadi

Reputation: 5868

Multiple slug types with cakePHP

I'm working on a CMS writing from base by my own. I implemented it's content section with Node and NodeType logic. ( every Node belongs to a NodeType) There's Slug field in both Nodes and NodeTypes tables and there's i wrote this two routes in router:

Router::connect('/:typeslug',array('controller' => 'nodetypes', 'action' => 'view'),array('pass'=>array('typeslug')));
Router::connect('/:typeslug/:nodeslug',array('controller' => 'nodes', 'action' => 'view'),array('pass'=>array('typeslug','nodeslug')));

it will achieve to contents like this : http://domain.ext/article/my-custom-article
First question : Is this a right and normal method?
Second question : What is the solution to use complex slugging like wordpress? ( e.g using slugs like archived dates like : http://mydomain.ext/2013/01/01/article/....
and more important of all is ability to switch between slug types in administration section.

Thanks for guides

Upvotes: 1

Views: 605

Answers (1)

Sam Delaney
Sam Delaney

Reputation: 1305

Question 1

Is this a right and normal method?

Yup that's fine, take a look at the PagesController that gets bundled in with a CakePHP app (documentation).

Question 2

What is the solution to use complex slugging like wordpress? ( e.g using slugs like archived dates like : http://mydomain.ext/2013/01/01/article/....

In this particular case you would have to setup additional routes in your routes.php file.

For example:

Router::connect(
    '/article/:year/:month/:day/*', array('controller' => 'articles')
);

Any requests for http://mydomain.ext/article/2013/01/01 would get routed to the following action in your articles controller:

public function index($year, $month, $day){
    ...
}

Observe that I reversed your URL a little because it avoids ambiguous requests for controllers which don't exist.

Question 3

...and more important of all is ability to switch between slug types in administration section.

What you can do in this instance is switch on and off routing depending on another configuration value. You would have to setup a configuration file specifically for your application but you would be able to modify it at run time:

Routing.php:

// load your routing configuration 
Configure::load('application_config', 'default');
// setup your routes
if(empty(Configure::read('routing_1')){
    // default routing
    ...
}else{
    // routing 1
    ...
}

AdministrationController.php:

public function someAction(){
    // persist configuration to file
    Configure::dump('application_config', 'default', array('routing_1' => true));
}

For more information, take a look at the Reading and Writing Configuration Files section on the documentation.

I hope this points you in the right direction.

Upvotes: 1

Related Questions