Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting out with Zend framework 2, class not found errors

I'm trying to get started with a simple LAMP site however can't seem to get the Zend framework to be picked up by my local Apache instance. I started on XAMPP on Windows and have since tried a Centos 6 VM with manual Apache/PHP install, but still get the same error on both which is below. The phpinfo() works fine, as does the rest of the site.

Fatal error: Class 'Zend\Log\Logger' not found in /var/www/html/site/public/test.php on line 20

My website code is a fairly simple test to invoke a Zend framework logger which is as follows

use Zend\Log\Logger;
use Zend\Log\Writer;
echo "<p>Hello world</p>";
echo $_POST["VIN"]; 
phpinfo();
$logger = new Zend\Log\Logger;
$writter = new Zend\Log\Writer\Stream('php://output');
$logger->addWriter($writer);
?>

My Apache linux httpd.conf is

<VirtualHost *:80>

DocumentRoot /var/www/html/site/public
<Directory /var/www/html/site/public>              
    DirectoryIndex test.php 
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

My Zend framework resides in /var/www/html/site/library/Zend, and I've added /var/www/html/site/library to the php.ini include as well.

like image 217
LJT Avatar asked Nov 13 '22 13:11

LJT


1 Answers

For anyone else hitting this error you need to utilise an autoloader, something that's not mentioned on the Zend framework getting started wiki.

<?php 
use Zend\Loader\StandardAutoloader;
use Zend\Log\Logger;
use Zend\Log\Writer;
require_once dirname((__DIR__)).'\library\Zend\Loader\StandardAutoloader.php';
$loader = new StandardAutoloader(array('autoregister_zf' => true));
$loader->register();


echo "<p>Hello world</p>";
echo $_POST["VIN"]; 
phpinfo();

$logger = new Zend\Log\Logger;
$writer = new Zend\Log\Writer\Stream('php://output');
$logger->addWriter($writer);


?>
like image 107
LJT Avatar answered Nov 15 '22 04:11

LJT