Page 1 of 1

Beginner's ZF question

Posted: Sun Feb 01, 2009 12:17 am
by Theory?
Got me a new book for learnin' me some Zend Framework action. I've seen this bootstrap done in a thousand programs, the one time I try and run it, it doesn't work. This is copied near verbatim from the book. Three files:

index.php (located in the root directory)

Code: Select all

 
<?php 
    
    //Set up the environment
    error_reporting(E_ALL|E_STRICT);                 
    ini_set('display_errors', true);                  
    date_default_timezone_set('America/New_York');  
    
    //Set the app path
    $rootDir = dirname(dirname(__FILE__)); 
    set_include_path($rootDir . '/library'                
            . PATH_SEPARATOR . get_include_path());  
    
    //Get the loader and load the classes
    require_once 'Zend/Loader.php'; 
    Zend_Loader::loadClass('Zend_Debug'); 
    Zend_Loader::loadClass('Zend_Controller_Front'); 
    
    //Get the Front Controller 
    $frontController = Zend_Controller_Front::getInstance();  
    $frontController->setControllerDirectory('../application/controllers'); 
    
    //Run the <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>!
    $frontController->dispatch();
 
IndexController.php (located in application/controllers/)

Code: Select all

 
<?php
 
class IndexController extends Zend_Controller_Action {
 
    public function indexAction() {
    
        $this->view->assign('title', 'Hello World!');
    }
    
}
 
And finally, the view file, index.phtml (located in application/views/scripts/index/)

Code: Select all

 
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
 
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    
    <title>
        <?php echo $this->escape($this->title);?>
    </title>
    
</head>
 
<body>
 
    <h1><?php echo $this->escape($this->title);?></h1>
    
</body>
</html>
 
I get this lovely error both on my live server and my testing machine (except the live server doesnt use mac directories).


Warning: require_once(Zend/Loader.php) [function.require-once]: failed to open stream: No such file or directory in /Users/Wedgewheel/Sites/zfTest/index.php on line 11

Fatal error: require_once() [function.require]: Failed opening required 'Zend/Loader.php' (include_path='/Users/Wedgewheel/Sites/library:.:/usr/local/php5/lib/php') in /Users/Wedgewheel/Sites/zfTest/index.php on line 11


WHY DOES GOD HATE ME?!?!

Re: Beginner's ZF question

Posted: Sun Feb 01, 2009 2:11 am
by jmut
Well, obviously your path to Zend library is wrong. Apart of this..this 30min startup is best thing you can do as a beginner I suppose.
http://framework.zend.com/docs/quickstart

Re: Beginner's ZF question

Posted: Sun Feb 01, 2009 9:41 am
by Theory?
I use their path and I get the same error. When I change

Code: Select all

$rootDir = dirname(dirname(__FILE__));
to simply

Code: Select all

$rootDir = dirname(__FILE__);
I get this error:

Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in /home/tinwbar1/public_html/zfTest/library/Zend/Controller/Dispatcher/Standard.php:241 Stack trace: #0 /home/tinwbar1/public_html/zfTest/library/Zend/Controller/Front.php(934): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 /home/tinwbar1/public_html/zfTest/index.php(22): Zend_Controller_Front->dispatch() #2 {main} thrown in /home/tinwbar1/public_html/zfTest/library/Zend/Controller/Dispatcher/Standard.php on line 241

Re: Beginner's ZF question

Posted: Sun Feb 01, 2009 9:49 am
by Eran
You didn't indicate your directory layout, but the way Zend recommends doing it is to put the index.php inside a publicly accessible directly (the document root directory) and the rest of the application outside of the document root. something like:

Code: Select all

/application  -application files
/library -library files, including Zend's
/html/index.php -bootstrap file
If the change you made worked, that means you didn't follow that guideline, rather placing index.php directly under the /library folder.
The error you are seeing now shows that the library is loaded, and you are missing an error controller - which is reached by default in the current controller/action can not be found (you probably have not created those either). If you would follow the zend tutorial all the way to the end you will see it covers all those things I mentioned.

Re: Beginner's ZF question

Posted: Sun Feb 01, 2009 10:43 am
by Theory?
Well I'll believe that, I just find it maddening that I copied this directly from a book and it doesn't work. Later in the book they abstract the bootstrapping to a separate class, but for this intro tutorial they do it this way for demonstration. I can't even find a problem with this code in the book's errata page on the website.

Re: Beginner's ZF question

Posted: Sun Feb 01, 2009 10:54 am
by Eran
What book would that be? I know only one official tutorial - the quickstart guide on the Zend Framework site
http://framework.zend.com/docs/quickstart

Re: Beginner's ZF question

Posted: Sun Feb 01, 2009 3:58 pm
by Theory?
It's Zend Framework in Action. I've read the quick start at the ZF site, I was just trying to follow along with the book for the sake of drilling it into my head. The book provides a directory structure, which as far as I've seen in most tutorials is the official recommended spec from Zend and it would seem that all my files are right and in the right place, I just can't understand why it's not running :oops:

Re: Beginner's ZF question

Posted: Mon Feb 02, 2009 1:46 am
by jmut
I think it's about time you tell us what this book is. Just scratch that and read the quickstart.
Here reading on choosing directory layout. It's personal decision. ZF is highly configurable.
http://framework.zend.com/wiki/display/ ... ory+Layout

Re: Beginner's ZF question

Posted: Mon Feb 02, 2009 9:21 am
by Theory?
I...I just did :(

It's Zend Framework in Action

Re: Beginner's ZF question

Posted: Mon Feb 02, 2009 9:24 am
by Theory?
So I dropped the .htaccess and the index.php (with the bootstrap in it) into the public folder and everything works like a charm. I just find it really odd that the book would specify doing it a different way. Either way, I'm really sorry if I angered anyone. Thanks so much for your help, seriously.