Bonjour,
Je suis le tuto mais je rencontre une erreur sans parvenir à en détecter la source. Je n'arrive pas à modifier l'url avec le format slug-id.
Elle reste toujours : http://localhost/Cote-Jardin/pages/show/slug:Accueil/id:1
Voici le code que j'ai dans index.ctp (app/view/Pages/index.ctp)
<?php foreach($pages as $k=>$v): $v = current($v); ?>
<?php echo $this->Html->link($v'name'], $v'link']); ?>
<?php endforeach; ?>
Le code de routes.php (app/config/routes.php) :
<?php
/**
* Routes configuration
*
* In this file, you set up routes to your controllers and their actions.
* Routes are very important mechanism that allows you to freely connect
* different URLs to chosen controllers and their actions (functions).
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package app.Config
* @since CakePHP(tm) v 0.2.9
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* Here, we are connecting '/' (base path) to controller called 'Pages',
* its action called 'display', and we pass a param to select the view file
* to use (in this case, /app/View/Pages/home.ctp)...
*/
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/page/:slug-:id', array('controller' => 'pages', 'action' => 'show'), array('pass' => array('id','slug'),'id'=>'[0-9]+','slug'=>'[a-z0-9\-]+'));
/**
* Load all plugin routes. See the CakePlugin documentation on
* how to customize the loading of plugin routes.
*/
CakePlugin::routes();
/**
* Load the CakePHP default routes. Only remove this if you do not want to use
* the built-in default routes.
*/
require CAKE . 'Config' . DS . 'routes.php';
Le code de Post.php (app/model/Post.hph)
<?php
class Post extends AppModel {
public function afterFind($results, $primary = false) {
foreach ($results as $k => $d) {
if(isset($d'Post']'slug']) && isset($d'Post']'id'])) {
$d'Post']'link'] = array (
'controller' => 'pages',
'action' => 'show',
'slug' => $d'Post']'slug'],
'id' => $d'Post']'id']
);
}
$results$k] = $d;
}
return $results;
}
}
Et pour finir mon PagesController :
<?php
/**
* Static content controller.
*
* This file will render views from views/pages/
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package app.Controller
* @since CakePHP(tm) v 0.2.9
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('AppController', 'Controller');
/**
* Static content controller
*
* Override this controller by placing a copy in controllers directory of an application
*
* @package app.Controller
* @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
*/
class PagesController extends AppController {
/**
* This controller does not use a model
*
* @var array
*/
public $uses = array('Post');
public function index() {
$d'pages'] = $this->Post->find('all', array(
'conditions' => array('type' => 'page'
)));
$this->set($d);
}
public function show($id=null,$slug=null) {
}
/**
* Displays a view
*
* @param mixed What page to display
* @return void
* @throws NotFoundException When the view file could not be found
* or MissingViewException in debug mode.
*/
public function display() {
$path = func_get_args();
$count = count($path);
if (!$count) {
return $this->redirect('/');
}
$page = $subpage = $title_for_layout = null;
if (!empty($path[0])) {
$page = $path[0];
}
if (!empty($path[1])) {
$subpage = $path[1];
}
if (!empty($path$count - 1])) {
$title_for_layout = Inflector::humanize($path$count - 1]);
}
$this->set(compact('page', 'subpage', 'title_for_layout'));
try {
$this->render(implode('/', $path));
} catch (MissingViewException $e) {
if (Configure::read('debug')) {
throw $e;
}
throw new NotFoundException();
}
}
}
Merci d'avance pour votre aide.
Cecile
Le problème vient du fais que dans ton Router tu n'autorises que des caractères en minuscules pour la valeur de slug. Or Accueil commence avec une majuscule :
http://localhost/Cote-Jardin/pages/show/slug:Accueil/id:1
Il y a 2 choses que tu peux faire pour résoudre ce problème. Soit tu converti la valeur du slug en lowercase a la volée dans ton afterFind :
<?php
class Post extends AppModel {
public function afterFind($results, $primary = false) {
foreach ($results as $k => $d) {
if(isset($d'Post']'slug']) && isset($d'Post']'id'])) {
$d'Post']'link'] = array (
'controller' => 'pages',
'action' => 'show',
'slug' => strtolower($d'Post']'slug']),
'id' => $d'Post']'id']
);
}
$results$k] = $d;
}
return $results;
}
}
?>
Soit tu autorises aussi les majuscules dans tes routes :
Router::connect(
'/page/:slug-:id',
array('controller' => 'pages', 'action' => 'show'),
array('pass' => array('id','slug'),'id'=>'[0-9]+','slug'=>'[a-zA-Z0-9\-]+')
);