2013-07-27 28 views
1

我遇到了CakePHP初學者的博客教程的問題。當我嘗試打開site.com/posts/index頁面時,我收到一個錯誤,指出我需要創建PostsController.php文件,但是我已經有了PostsController.php。蛋糕PHP博客教程錯誤PostsController Mising

我使用PHP蛋糕2.3.8版本

這是錯誤我得到

Error: PostsController could not be found. 

Error: Create the class PostsController below in file: app\Controller\PostsController.php 

我已經按照本教程的信,並正確地把目錄中的文件。

這是我創建的PostsController.php文件。

<? 
class PostsController extends AppController { 

    public $helpers = array('Html', 'Form'); 



    public function index() { 
     $this->set('posts', $this->Post->find('all')); 
    } 

    public function view($id = null) { 
     if (!$id) { 
      throw new NotFoundException(__('Invalid post')); 
     } 

     $post = $this->Post->findById($id); 
     if (!$post) { 
      throw new NotFoundException(__('Invalid post')); 
     } 
     $this->set('post', $post); 
    } 
} 
?> 

這裏是post.php中的文件(模型)

<? 
class Post extends AppModel { 
} 
?> 

這裏是index.ctp文件

<!-- File: /app/View/Posts/index.ctp --> 

<h1>Blog posts</h1> 
<table> 
    <tr> 
     <th>Id</th> 
     <th>Title</th> 
     <th>Created</th> 
    </tr> 

    <!-- Here is where we loop through our $posts array, printing out post info --> 

    <?php foreach ($posts as $post): ?> 
    <tr> 
     <td><?php echo $post['Post']['id']; ?></td> 
     <td> 
      <?php echo $this->Html->link($post['Post']['title'], 
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?> 
     </td> 
     <td><?php echo $post['Post']['created']; ?></td> 
    </tr> 
    <?php endforeach; ?> 
    <?php unset($post); ?> 
</table> 

我已經用Google搜索周圍的解決方案,發現很多人都有同樣的問題,但是他們的解決方案不適合我的情況,或者他們自己解決問題,也沒有發佈解決方案。

請幫我...

UPDATE

的問題已經得到解決,這是<?php標記,失蹤了。

回答

4

的問題是因爲你使用的<?代替<?php打開你的PHP。您可以使用完整標籤打開php或使用php.ini啓用短標籤。

It's been recommended not to use the short tag "short cut" and instead to use the full tag combination. With the wide spread use of XML and use of these tags by other languages, the server can become easily confused and end up parsing the wrong code in the wrong context. Also short tags may not be supported on the target server

+1

CakePHP只是PHP - 它沒有它自己的短開標籤設置。 +1,因爲它似乎可能是錯誤(儘管沒有提到錯誤消息上方輸出中的文件內容)。 – AD7six

+0

我完全同意你@ AD7six。 「CakePHP只是PHP--它沒有它自己的短打開標籤設置」。我編輯了我的答案。 – Konsole

+0

補給它,爲什麼我沒有想到這一點?我會盡力並儘快回傳結果。 – akiortagem

0

Here is the PostController.php file I created.

控制器文件的名稱是PostsController.php--由於找不到類而發生錯誤。

+0

我的問題輸入了錯誤的名字,我目前使用的一個是PostsController.php – akiortagem

+0

你需要這樣做:'錯誤:在文件中創建以下類PostsController:應用程序\控制器\ PostsController.php'無論你怎麼想 - 你創建的文件都在錯誤的地方。如果你能看到錯誤消息您的帖子上方控制器的內容 - 的問題是使用短的PHP標籤(一個壞主意) - 替換'<'和' AD7six