Réécriture de l'url pose probème

Par cafreunion, il y a 11 ans


Bonjour,
j'ai suivi le tutoriel cration d'un site avec cakephp, j'ai un probleme au niveau des url des catégorie
ci-dessous mon postController

<?php App::uses('AppController', 'Controller'); class PostsController extends AppController { public $helpers = array('Text','Form','Html', 'Session', 'Date'); public $components = array('RequestHandler'); /** public $cacheAction= array( 'index'=>'2 DAY', 'show'=>'2 DAY' ); **/ public $uses = array('Post'); function menu(){ $posts = $this->Post->find('all',array( 'conditions' => array('type' =>'post', 'online'=>1), 'fields' =>array('id','slug','name') )); return $pages; } /** * permet d'afficher la home des news **/ function index(){ $this->Post->contain('Tag'); $d['posts'] = $this->Paginate('Post',array('type'=>'post', 'online'=>1,'created<= NOW()')); $this->set($d); } function tag($name){ $this->loadModel('PostTag'); $this->PostTag->recursive = 0; $this->PostTag->contain('Tag','Post'); $posts = $this->Paginate('PostTag',array( 'Tag.name' => $name, 'Post.type' => 'post', 'Post.online' => 1, 'Post.created <= NOW()' )); $post_ids = Set::Combine($posts,'{n}.PostTag.post_id','{n}.PostTag.post_id'); $this->Post->contain('Tag'); $d['posts'] = $this->Post->find('all',array( 'conditions'=>array('id'=>$post_ids) )); $this->set($d); $this->render('index'); } function category($category){ $this->loadModel('Category'); $this->Post->contain('Tag'); $cat = $this->Category->find('first',array( 'conditions' => array('slug' => $category) )); if(empty($cat)) throw new NotFoundException('Aucun categorie ne correspond a cet ID'); $d['posts'] = $this->Paginate('Post',array('type'=>'post','online'=>1,'category_id'=> $cat['Category']['id'])); $this->set($d); $this->render('index'); } function show($id = null, $slug = null){ if(!$id) throw new NotFoundException('Aucun article ne correspond a cet ID'); $post = $this->Post->find('first',array( 'conditions' => array('Post.id' => $id), 'recursive' => 0 )); if(empty($post)) throw new NotFoundException('Aucun article ne correspond a cet ID'); if($slug != $post['Post']['slug']) $this->redirect($post['Post']['link'],301); $d['post'] = $post; $this->set($d); } function feed(){ if( $this->RequestHandler->isRss() ){ $d['posts'] = $this->Post->find('all', array('limit' => 20, 'conditions'=>array('type'=>'post') )); return $this->set($d); } } /** * parie avec les prefix **/ function admin_index(){ $this->paginate = array('Post'=> array( 'limit'=>20 )); $d['posts'] = $this->Paginate('Post',array('type'=>'post','online'>=0)); $this->set($d); } function admin_edit($id = null){ if($this->request->is('put') || $this->request->is('post')){ $this->Post->set($this->request->data); if($this->Post->save()){ $this->Session->setFlash("Le contenu a bien été modifié","notif"); $this->redirect(array('action'=>'index')); } } if($id){ $this->Post->id = $id; $this->request->data = $this->Post->read(); }else{ $this->request->data = $this->Post->getDraft('post'); } $d['categories'] = $this->Post->Category->find('list'); $this->Post->PostTag->contain('Tag'); $d['tags'] = $this->Post->PostTag->find('all',array( 'conditions' => array('PostTag.post_id' => $id) )); $this->set($d); } function admin_delete($id){ $this->Session->setFlash("L'article a bien été supprimer",'notif'); $this->Post->delete($id); $this->redirect($this->referer()); } function admin_delTag($id){ $this->Post->PostTag->delete($id); $this->redirect($this->referer()); } }

Le model category

<?php class Category extends AppModel { public function afterfind($data){ foreach ($data as $k => $d){ if(isset($d['Category']['slug']) && isset($d['Category']['id'])){ $d['Category']['link'] = array( 'controller' => 'posts', 'action' =>'category', 'slug' => $d['Category']['slug'] ); } $data[$k] = $d; } return $data; } public function beforeSave(){ if(empty($this->data['Category']['slug']) && isset($this->data['Category']['slug']) && !empty($this->data['Category']['name'])) $this->data['Category']['slug'] = strtolower(Inflector::slug($this->data['Category']['name'],'-')); return true; } } ?>

la réécriture de l'url dans le fichier route

/** CATEGORY **/ Router::connect('/categorie/:slug',array('controller'=>'posts','action'=>'category'),array('pass'=>array('slug'),'slug'=>'[a-z0-9\-]+'));

Lorsque je clique sur le lien dans ma sidebar je dois arriver sur une page avec tous les articles de la catégorie or ici j'ai le message d'erreur suivant :

Notice (8): Undefined variable: category [APP\Controller\PostsController.php, line 52]

J'ai tournée le probleme dans tous les sens et j'arrive pas a résoudre le probleme
ci-dessous ma sidebar

<?php echo $this->Html->link('Ajout d\'image',array('controller'=>'galleries','action'=>'show'));?> <div class="page-header"> <div class="span4"> <b>Les catégories</b> <?php $categories = $this->requestAction(array('controller'=>'categories', 'action'=>'sidebar','admin'=>false)); ?> <?php foreach ($categories as $k => $v): $v = current($v);?> <li><?php echo $this->Html->link($v['name'].' ('.$v['post_count'].')',$v['link']);?></li> <?php endforeach;?> <br/><br/> <b>Page disponible</b> <?php $pages = $this->requestAction(array('controller'=>'pages', 'action'=>'menu','admin'=>false)); ?> <?php foreach ($pages as $k=>$v): $v = current($v);?> <li><?php echo $this->Html->link($v['name'],$v['link']);?></li> <?php endforeach;?> </div> </div>

Ci-dessous les lignes concernées par l'erreur

Notice (8): Undefined variable: category [APP\Controller\PostsController.php, line 52] Code $this->Post->contain('Tag'); $cat = $this->Category->find('first',array( 'conditions' => array('slug' => $category) PostsController::category() - APP\Controller\PostsController.php, line 52 ReflectionMethod::invokeArgs() - [internal], line ?? Controller::invokeAction() - CORE\Cake\Controller\Controller.php, line 473 Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 107 Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 89 [main] - APP\webroot\index.php, line 96

Erreur apparaissant sur ma page

Aucun categorie ne correspond a cet ID Error: The requested address '/luckdown/posts/category/slug:Programmation?url=%2Fposts%2Fcategory%2Fslug%3AProgrammation' was not found on this server. Stack Trace #0 [internal function]: PostsController->category() #1 C:\wamp\www\luckdown\lib\Cake\Controller\Controller.php(473): ReflectionMethod->invokeArgs(Object(PostsController), Array) #2 C:\wamp\www\luckdown\lib\Cake\Routing\Dispatcher.php(107): Controller->invokeAction(Object(CakeRequest)) #3 C:\wamp\www\luckdown\lib\Cake\Routing\Dispatcher.php(89): Dispatcher->_invoke(Object(PostsController), Object(CakeRequest), Object(CakeResponse)) #4 C:\wamp\www\luckdown\app\webroot\index.php(96): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse)) #5 {main}

Je vous remerci de votre aide
Cafreunion

Aucune réponse