Change directories app/View/Pages/Posts/ to app/View/Posts/
Each controller has own dir with views files.
سؤال
I am trying to get my hands on cakephp. So I started off with the cakephp demo Blog Application. I followed the first few steps of the tutorial and when I trying to load the posts controller it says
Missing View
Error: The view for PostsController::index() was not found.
Error: Confirm you have created the file: T:\Project Folders\NetBeans\cakeBlog\app\View\Posts\index.ctp
I searched a lot about this both in stackoverflow , cakephp forum and even in googlegroups but none of the solutions seems to work from me. Most of the solutions posted are as follows:
Check if mod_rewrite is enabled -- Yes I have it enabled.
Check if you named the index.ctp as index.ctp not index.cpt , Index.ctp or any other variant. -- Yes I have placed the index as follows app/views/Posts/index.ctp (using netbeans wizard)
Use tags instead of php short tags-- I am using the conventional Tag
Development Environment
Web Server - WAMP
I created an alias named cakeblog
and pointed it to cakephp_folder/app/webroot/
cakeblog.conf
Alias /cakeblog/ "T:\Project Folders\NetBeans\cakeBlog\app\webroot/"
<Directory "T:\Project Folders\NetBeans\cakeBlog\app\webroot/">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
</Directory>
app/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cakeblog
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I also loaded the localization and the debugkit Plugin. I simply placed the exacted folders into app/plugin and added the following in bootstrap.php
bootstrap.php
CakePlugin::load('DebugKit'); //Loads a single plugin named DebugKit
CakePlugin::load('Localized');
core.php
Configure::write('debug', 1);
I am able to launch the application and I am seeing the welcome page well and fine. I have attached an snapshot of it in startup.png
Now , let me paste the code, which I simply copy pasted from the tutorial:
app/model/post.php
<?php
class Post extends AppModel {
//put your code here
}
?>
app/controller/PostsController.php
<?php
class PostsController extends AppController {
//put your code here
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);
}
}
?>
app/view/Pages/Posts/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>
Kindly check the attachment output.png for the snapshot of the output I am receiving for http://localhost/cakeblog/posts
:
المحلول
Change directories app/View/Pages/Posts/ to app/View/Posts/
Each controller has own dir with views files.
نصائح أخرى
View files are stored in /app/View/, in a folder named after the controller that uses the files, and named after the action it corresponds to. In your example, the view file for the Post controller’s “index()” action, would normally be found in /app/View/Posts/index.ctp.