Wednesday, April 17, 2013

Re: Confused about routing

Hi,
You need to organize a database in different way to achieve what you want.

Create the following table:

nodes - to save the name or category of products, content types, full path as a slug.

id
parent_id
lft
rght
title
path
published (0,1)
type
....

closure - to save deep linking association. This gives you the ability to create an unlimited number of subcategories.


id
ancestor
descendant

posts - to save product info
images - to save product images


Relations:

Nodes self HABTM via Closures
Nodes hasOne Post
Nodes hasMany Images
...


Routnig for exmple:

localhost/my-categories
localhost/my-categories/sub-categories
localhost/my-categories/sub-categories/2  (pagination)
localhost/my-categories/sub-categories/product-name

Router::connect('/:path/*', array('admin'=>false,'controller' => 'nodes', 'action' => 'show'),array('pass'=>array('path','page'),'page' =>'[0-9]+'));

In nodes controller:

public function show(){

$path = func_get_args();
//debug($path);
$last_arg = end(array_values($path));

if (is_numeric($last_arg)) {
$page = $last_arg;
//debug($page);
array_pop($path);
$path = implode("/", $path);
$this -> request -> params['named']['page'] = $last_arg;
} else {
$path = implode("/", $path);
}
           //debug($path);
 $check_post = $this -> Node -> find('first', array('conditions' => array('Node.path' => $path), 'fields' => array('Node.id', 'Node.type', 'Node.title', ), 'recursive' => -1));
//$this -> Node -> id = $check_post['Post']['id'];
//debug($check_post);
if (!$check_post['Post']['id']) {
throw new NotFoundException(__('Invalid post'));
}
if ($check_post['Node']['type'] == 0) {
//if category , find results via paginate joins
}
else
{
findByPath, and render views
}

}


this would be a short explanation., maybe there is a better and simpler solution.

Nikola






--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: