简体   繁体   中英

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.

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


?>

I think that you includes a wrong path into the *include_path*, include /var/www/html/site/Library not /var/www/html/site/Library/Zend .

Check: https://github.com/zendframework/zf2/blob/master/INSTALL.md

Or paste here your php.ini configuration or the phpinfo() response.

The autoloader is indeed what is needed. If you've got the path to your library included in your php.ini, you can add the following code to get Zend to work as intended:

require_once 'Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));
$loader->register();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM