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');

Thursday 24 September 2009

Kohana PHP framework - some tips

Recently, I've been working with the Kohana PHP Framework and there are a few hurdles I've come across and gotten past - here are a few tips.

Making your own Controller

The default gateway controller with a fresh install of Kohana is 'welcome'. This means that URIs are written a bit like this:

http://localhost/kohana/welcome/index

But it's not ideal to have 'welcome' in the URI all the time. So, if you want to choose a custom name like 'site', you need to make your own controller...
  1. Copy the welcome controller (welcome.php) in /kohana/applications/controllers/ and paste into the same folder.
  2. Rename the file to site.php
  3. Open the site.php file and change the class declaration to 'class Site_Controller extends Template_Controller'
  4. Now, go to /kohana/system/config/ and copy the file called routes.php to /kohana/application/config/ .
  5. Open up the file and edit like so: $config['_default'] = 'site';
That should do it. Your site will now build its URIs and route through the 'site' controller by default. You should make urls like so:

<?php html::anchor('site/index', 'Home Page'); ?>

Making your own default template

Simply go to /kohana/system/views/kohana/ and copy the template.php file to /kohana/application/views/kohana/. Edit the file as you wish. But, make sure your controller has this line pointing to it: public $template = 'kohana/template';

Making your CSS and Javascript work (get found)

So you make your XHTML template file and are ready to test everything - you stick your CSS in a .css file and your Javascript into a .js file and your images into an 'images' folder and put them in the root (/kohana/). You go to the page through the URI, e.g. http://localhost/kohana/site/index

Huh? What's this? Your CSS, Javascript and image files were not found!

Turns out you need to change the location of your files and add some PHP to your template.
  1. make a folder in root (/kohana/) called 'media'.
  2. make 3 folders called images, css and js inside your media folder and move your images, css and javascript files into their respective folders
  3. Edit your css and js files to point to the right path for the images, e.g: background-image: url(/kohana/media/simages/bg.png); (make sure the first / is there otherwise it won't work
  4. Now edit your template file to include this bit of code in the head section (I called my css file and js file css.css and js.js (imaginative huh?)):
<?php echo html::stylesheet(array('media/css/css'), array('screen')) .
html::script(array('media/js/js'), FALSE); ?>

Now this will put the typical XHTML tags into your page to include outside css and js files:
<link rel="stylesheet" type="text/css" href="/kohana/media/css/css.css" media="screen" />

<script type="text/javascript" src="/kohana/media/js/js.js">

In 1935, one of the founders of modern genetics, JBS Haldane, studied a group of men with the blood disease haemophilia. He speculated that there would be about 150 new mutations in each of us.

Others have since looked at DNA in chimpanzees to try to produce general estimates for humans.

However, next generation sequencing technology has enabled the scientists to produce a far more direct and reliable estimate.

They looked at thousands of genes in the Y chromosomes of two Chinese men. They knew the men were distantly related, having shared a common ancestor who was born in 1805.

By looking at the number of differences between the two men, and the size of the human genome, they were able to come up with an estimate of between 100 and 200 new mutations per person.

Impressively, it seems that Haldane was right all along.

Unimaginable

One of the scientists, Dr Yali Xue from the Wellcome Trust Sanger Institute in Cambridgeshire, said: "The amount of data we generated would have been unimaginable just a few years ago.

"And finding this tiny number of mutations was more difficult than finding an ant's egg in an emperor's rice store."

New mutations can occasionally lead to severe diseases like cancer. It is hoped that the findings may lead to new ways to reduce mutations and provide insights into human evolution.

Joseph Nadeau, from the Case Western Reserve University in the US, who was not involved in this study said: "New mutations are the source of inherited variation, some of which can lead to disease and dysfunction, and some of which determine the nature and pace of evolutionary change.

"These are exciting times," he added.

"We are finally obtaining good reliable estimates of genetic features that are urgently needed to understand who we are genetically."

http://news.bbc.co.uk/1/hi/sci/tech/8227442.stm