2017-04-04 79 views
0

我安裝了CakePHP 2.9.7,我正在通過參考(https://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html)來做博客教程。Cakephp博客教程 - 添加一個圖層

創建一個Post模型

class Post extends AppModel { 
} 

創建郵政控制器

class PostsController extends AppController { 
    public $helpers = array('Html', 'Form'); 

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

創建後Views.I內部創建的應用程序/查看folder.And帖子夾i更新database.php中也使其可以連接到mysql數據庫。

<!-- 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> 

但是,當我在當地的CakePHP /應用程序錯誤運行說 錯誤:AppController中::索引視圖()沒有found.Confirm您所創建的文件:應用程序/ index.ctp在一個以下路徑: cakephp/app/View/App/index.ctp.stuck解決這個問題,因爲我是cakephp的新手。

+1

你的鏈接應該是'cakephp/posts' – tarikul05

+5

Offtopic:你真的不應該用CakePHP 2.x啓動任何新項目,3.x是要走的路! – ndm

+0

我寫的和博客教程中的一樣。並且該文件也在博客教程中指定的路徑中。 index.ctp僅在app/View/Posts/index.ctp中。但錯誤表示確認您已在以下路徑之一中創建文件:App/index.ctp:cakephp/app/View/App/index.ctp。 – Manasa

回答

1

好像你正在請求錯誤的URL。如果您要求http://your-domain.de/app Cake將嘗試查找AppControllerindex的操作和查看。

您應該通過請求http://your-domain.de/postshttp://your-domain.de/posts/index來獲得您想要的控制器和視圖。