Bonsoir, J'ai un petit problème dans le tutoriel de CakePHP : jour 1. Lorsque nous devons afficher le tableau des Posts, j'ai deux erreurs et je ne sais pas réellement pourquoi. En plus, j'ai aussi deux erreurs qui me suivent depuis le tout début. J'ai pris la version 2.2.1 de Cake php, une version stable. [code]Notice (8): Use of undefined constant Post - assumed 'Post' [CORE\Cake\Routing\Dispatcher.php, line 242] Code return false; } return $reflection->newInstance($request, $response); Context $request = object(CakeRequest) {} $response = object(CakeResponse) {} $ctrlClass = 'PagesController' $reflection = object(ReflectionClass) {} ReflectionClass::newInstance() - [internal], line ?? Dispatcher::_getController() - CORE\Cake\Routing\Dispatcher.php, line 242 Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 152 [main] - APP\webroot\index.php, line 92 [/code] [code]Strict (2048): Declaration of Post::afterFind() should be compatible with Model::afterFind($results, $primary = false) [APP\Model\Post.php, line 2] Code if (file_exists($file)) { self::_map($file, $className, $plugin); return include $file; Context $className = 'Post' $parts = array( (int) 0 => 'Model' ) $plugin = null $package = 'Model' $file = 'C:\wamp\www\site_cakephp\app\Model\Post.php' $paths = array( (int) 0 => 'C:\wamp\www\site_cakephp\app\Model\', (int) 1 => 'C:\wamp\www\site_cakephp\app\Lib\Model\', (int) 2 => 'C:\wamp\www\site_cakephp\app\Model\', (int) 3 => 'C:\wamp\www\site_cakephp\lib\Cake\Model\' ) $appLibs = 'C:\wamp\www\site_cakephp\app\Lib\' $normalizedClassName = 'Post' $path = 'C:\wamp\www\site_cakephp\app\Model\' include - APP\Model\Post.php, line 2 App::load() - CORE\Cake\Core\App.php, line 560 spl_autoload_call - [internal], line ?? class_exists - [internal], line ?? ClassRegistry::init() - CORE\Cake\Utility\ClassRegistry.php, line 136 Controller::loadModel() - CORE\Cake\Controller\Controller.php, line 734 Controller::__isset() - CORE\Cake\Controller\Controller.php, line 366 PaginatorComponent::_getObject() - CORE\Cake\Controller\Component\PaginatorComponent.php, line 239 PaginatorComponent::paginate() - CORE\Cake\Controller\Component\PaginatorComponent.php, line 118 Controller::paginate() - CORE\Cake\Controller\Controller.php, line 1082 PagesController::admin_index() - APP\Controller\PagesController.php, line 41 ReflectionMethod::invokeArgs() - [internal], line ?? Controller::invokeAction() - CORE\Cake\Controller\Controller.php, line 485 Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 186 Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 161 [main] - APP\webroot\index.php, line 92[/code] [code]Notice (8): Undefined variable: pages [APP\View\Pages\admin_index.ctp, line 12] Code ob_start(); include $this->__viewFile; Context $viewFile = 'C:\wamp\www\site_cakephp\app\View\Pages\admin_index.ctp' $dataForView = array() include - APP\View\Pages\admin_index.ctp, line 12 View::_evaluate() - CORE\Cake\View\View.php, line 920 View::_render() - CORE\Cake\View\View.php, line 883 View::render() - CORE\Cake\View\View.php, line 475 Controller::render() - CORE\Cake\Controller\Controller.php, line 957 Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 193 Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 161 [main] - APP\webroot\index.php, line 92[/code] [code]Warning (2): Invalid argument supplied for foreach() [APP\View\Pages\admin_index.ctp, line 12] Code ob_start(); include $this->__viewFile; Context $viewFile = 'C:\wamp\www\site_cakephp\app\View\Pages\admin_index.ctp' $dataForView = array() include - APP\View\Pages\admin_index.ctp, line 12 View::_evaluate() - CORE\Cake\View\View.php, line 920 View::_render() - CORE\Cake\View\View.php, line 883 View::render() - CORE\Cake\View\View.php, line 475 Controller::render() - CORE\Cake\Controller\Controller.php, line 957 Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 193 Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 161 [main] - APP\webroot\index.php, line 92 [/code] **Voici les URL(afin de se situé):** http://ramz.no-ip.org/site\_cakephp/admin/pages https://dl.dropbox.com/u/90872635/Cake\_php/Cake\_1.PNG** http://ramz.no-ip.org/site\_cakephp/pages https://dl.dropbox.com/u/90872635/Cake\_php/Cake\_2.PNG** **AppController.php** [code]<?php //App::uses('Controller', 'Controller');
class AppController extends Controller {
function beforeFilter(){
if(isset($this->request->params['prefix']) && $this->request->params['prefix'] == 'admin'){ $this->layout = 'admin'; } } } [/code] \*\*PagesController.php\*\* [code]<?php //App::uses('AppController', 'Controller');
class PagesController extends AppController {
public $uses = array(Post);
/**
* RequestAction, permet d'avoir la liste des contenus pour le menu
**/
function menu(){
$pages = $this->Post->find('all', array( 'conditions' => array('type' => 'page', 'online' => 1), 'fields' => array('id', 'slug', 'name' ))); return $pages; } /\*\* \* Permet d'afficher une page \*\*/ function show($id = null, $slug = null){ if(!$id) throw new NotFoundException(''); $page = $this->Post->find('first', array( 'conditions' => array('id' => $id) )); if(empty($page)) throw new NotFoundException(''); if($slug != $page['Post']['slug']) $this->redirect($page['Post']['link'], 301); $d['page'] = current($page); $this->set($d); } /\*\* \* \*\*/ function admin\_index(){ $this->paginate = array('Post' => array('limit' => 20)); $d['pages'] = $this->paginate('Post', array('type' => 'page')); $this->set[$d]; } } [/code] \*\*Post.php\*\* [code]<?php class Post extends AppModel {
public function afterFind($data){
foreach ($data as $k => $d) { if(isset($d['Post']['slug']) && isset($d['Post']['id'])){ $d['Post']['link'] = array( 'controller' => 'pages', 'action' => 'show', 'id' => $d['Post']['id'], 'slug' => $d['Post']['slug'] ); } $data[$k] = $d; } return($data); } } ?>[/code] \*\*admin.ctp\*\* [code]
<title>
<?php echo $title_for_layout; ?>
</title><link rel="stylesheet/less" href="<?php%20echo%20%24this->Html->url('/css/bootstrap.less');%20?>"><?php echo $this->Html->script('less'); ?> <?php echo $scripts_for_layout; ?>
Administration
- <?php echo $this->Html->Link("Pages", array('action'=>'index', 'controller' => 'pages')); ?>
<?php echo $content_for_layout; ?>
<?php echo $this->element('sql_dump'); ?> [/code] **default.ctp** [code] <title>
<?php echo $title_for_layout; ?>
</title><link rel="stylesheet/less" href="<?php%20echo%20%24this->Html->url('/css/bootstrap.less');%20?>"><?php echo $this->Html->script('less'); ?> <?php echo $scripts_for_layout; ?>
Titre<?php echo $this->element('menu'); ?>
<?php echo $content_for_layout; ?>
<?php echo $this->element('sql_dump'); ?> [/code] admin_index.php [code]
Gérer les pages
| ID |
Nom |
En ligne |
Action |
| <?php foreach($pages as $k => $v): $v = current($v); ?> |
<?php echo $v['id']; ?> |
<?php echo $v['name']; ?> |
<?php echo $v['online']=='0'?'<span class="label label-important">Hors ligne':'En ligne'; ?> |
|
<?php endforeach; ?>
<?php echo $this->Paginator->numbers();?> [/code] **show.php** [code]<?php $this->set('title_for_layout', $page['name']); ?>
<?php echo $page['name']; ?>
<?php echo $page['content']; ?>[/code] Merci à vous et excellent tutoriel Grafikart, ça me permet de progresser même si mes bases en PHP sont très faibles (orienté objet). Cordialement, Ramz.