Showing posts with label Zend Framework. Show all posts
Showing posts with label Zend Framework. Show all posts

Monday, 28 September 2009

Another Zend Framework Error - missing index.phtml

Something that isn't made especially clear is the way views and controllers work together. It hints at the issue in the ZF official tutorial here, but provides no clear message.

OK, the scenario - I've just created a new controller:

<?php

class UserController extends Zend_Controller_Action {

public function indexAction(){
echo "You are in the index of User";
}
?>

Running this in the browser by going to http://localhost/public/user will cause this error:

An error occurred

Application error

Exception information:

Message: script 'user/index.phtml' not found in path (C:\wamp\www\application\views\scripts\)

And it also outputs a stack trace and other stuff.

Evidently, there is a missing file issue here. Believe it or not, you need to create a phtml file corresponding to your controller and action! You have to go to the /applications/views/scripts/ directory and create a new folder called 'user' (or whatever you have called your controller), and you need to create a file in it called index.phtml and an additional phtml file for every action you create in all your controllers.

So lame.

Sunday, 27 September 2009

Zend Framework View not found

I was messing about with a new installation of the Zend Framework on my system after reading through the basic tutorials on the web (the official ZF website tutorial is horrible) and I came across something that didn't work when using Views.

Here is the typical usage as shown in a tutorial on the IBM site (tut 2 [sign in required]):

<?php
//You should have require_once 'Zend/Loader.php'; in your bootstrap (index.php)

Zend::loadClass('Zend_Controller_Action');
Zend::loadClass('Zend_View');

class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$view = new Zend_View();
setScriptPath('views'); //the offending line
echo $view->render('indexIndexView.php');
}
}
?>

This is a typical controller class that makes a View object in order to render the HTML in the indexIndxView.php file to your browser. What should happen is that the contents of indexIndexView.php are displayed on your webpage. What actually happens is you get this error:

An error occurred

Application error

Exception information:

Message: script 'indexIndexView.php' not found in path (views\)

And then it outputs the stack trace and other stuff.

Evidently, this is a path problem - it doesn't know where to look for the files. Believe it or not, Zend didn't make their scripts auto-discover the location of the views folder! The thing is, the people who write the software and these tutorials should really mention these errors and how to get past them because it's things like this that put people off learning frameworks in the first place because the simplest things don't even work. Trying to Google this error is next to useless as it's really difficult to find the solution.

The fix is actually really easy:

$view->setScriptPath(APPLICATION_PATH . '/views/');

The APPLICATION_PATH is a constant set in the bootstrap file that can be used to make sure your files gets found. Why do people negelect to mention such important things?

Anyway, seeing the inadequecy of needing to create a view object and set its script path in every controller method, I decided to just create an instance of the view object in the bootstrap (index.php):

Zend_Loader::loadClass('Zend_View');
$view = new Zend_View();
$view->setScriptPath(APPLICATION_PATH . '/views/');

Then in every controller method I need to render a view in I just write this code (without the silly file naming OC):

global $view;
echo $view->render('indexIndexView.php');