I'm just getting started with Zend Framework. I decided to try it for my website translations.
I set the include path as follow:
set_include_path('.:/usr/share/php:/usr/share/php/Zend/library');
And then straight from the docs this doesn't work:
use Zend\I18n\Translator\Translator;
$translator = new Translator();
Of course, I haven't loaded the Zend\I18n\Translator\Translator class anywhere, so I get:
Fatal error: Class 'Zend\I18n\Translator\Translator' not found in /path/file.php on line X
I see there are several things in the Zend/Loader directory, but I can't figure out how to use them and I don't even know if this is what I should be looking at. How do I load the Zend\I18n\Translator\Translator class?
Using the auto loader, you can do:
require_once 'Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader(array(
'fallback_autoloader' => true,
));
$loader->register();
use Zend\I18n\Translator\Translator;
Related
I'm trying to learn the basics of Slim 3 and I have difficulties trying to figure out the proper way to organise my custom code, esp. custom classes. For instance, I want to create a custom error handler:
<?php
namespace App\Handlers;
// [...]
final class Error extends \Slim\Handlers\Error
{
// [...]
}
... but the documentation I've checked hasn't revealed under which path to save the class definition or how to configure the framework so it can be found in my index.php entry point:
<?php
require __DIR__ . '/../vendor/autoload.php';
// [...]
$app = new \Slim\App(['settings' => ['displayErrorDetails' => true]]);
$container = $app->getContainer();
$container['errorHandler'] = function ($c) {
return new App\Handlers\Error($c['Logger']);
};
Fatal error: Class 'App\Handlers\Error' not found
I'd appreciate any hint.
You're problem is not related to the framework at all.
Slim doesn't tell you where to keep your custom code because it's a matter of your free choice.
Your error:
Fatal error: Class 'App\Handlers\Error' not found
is not generated by Slim, but PHP itself. You need to add an autoloader for your code to let PHP know where to find appropriate classes.
I can see that you utilize Composer, therefore it's the best option to configure composer.json to create autoloader for your code.
I'm trying to use Universal Class Loader functionality from Phalcon into my project, however I can't get it working.
Here is how i've implemented into my app (using registerClasses).
index.php:
//...
$loader->registerClasses(
array(
"Commons" => "library/classes/CommonsClass.php"
)
);
$loader->register();
sampleController.php:
public function doAction()
{
$cc = new Commons();
}
And when I execute the controller, it throws me this exception:
Fatal error: Class 'Commons' not found in C:\the\path\to\phalcon_app\app\controllers\SomeController.php on line 63
The Phalcon Documentation just say that you need to register a class and call it in your funcion. There is something I've missed?
Ps.: The library folder is not registered anywhere (don't know if its needed), and it is in the same path as controllers, views, etc (/app/).
You should probably check your directory structure.
given:
mah_app/app/config/loader.php
mah_app/library/classes/Commons.php
I would expect this to work:
// loader.php
$loader->registerClasses(
array(
"Commons" => __DIR__ . "/../../library/classes/Commons.php"
)
)->register();
// works with $loader->registerDirs() for sure
Also, I would also suggest using namespaces and/or matching file name with class name.
Had the same problem. I commented out the namespace declaration inside the class and it worked. Version 2.0.3.
I'm using Zend Framework 2 to do some work, here I need to cleanup user's input, which is in Html. I've used php tidy extension [see this] before in plain php without the use of namespace and autoloading, and that code goes just fine.
while this time, I wrote a view helper in zf2, which is like:
<?php
namespace Blog\View\Helper;
use Zend\View\Helper\AbstractHelper;
class TidyHtml extends AbstractHelper
{
public function __invoke($userHtml){
$config = array(
'indent' => true,
'output-xhtml' => true,
'wrap' => 200);
$userHtml = '<div id="wrapper">'.$userHtml.'</div>';
$tidy = new tidy();
$tidy->parseString($userHtml,$config,'utf8');
$tidy->cleanRepair();
$dom = new DOMDocument();
$dom->loadHTML($tidy);
$node = $dom->getElementsById("wrapper");
$newdoc = new DOMDocument();
$cloned = $node->item(0)->cloneNode(TRUE);
$newdoc->appendChild($newdoc->importNode($cloned,TRUE));
return $newdoc->saveHTML();
}
}
I want to create an instance of tidy in line 14, but an error raised:
PHP Fatal error: Class 'Blog\\View\\Helper\\tidy' not found in /var/www/zf2-tutorial
/module/Blog/src/Blog/View/Helper/TidyHtml.php on line 14
aparently php treat tidy as some user-defined class and can't find its declaration.
maybe the autoloading function in zf2 have some influence on it. and I'm sure tidy2.0 extension is properly installed on my machine.
The problem here is caused by the fact that you are in namespace Blog\View\Helper as declared in your code, and since you call the class tidy without its fully qualified name, PHP supposes you actually want Blog\View\Helper\tidy.
Simply do $tidy = new \tidy(); and you should be fine, notice the prepended slash.
You might also want to read more about namespaces in PHP: http://php.net/manual/en/language.namespaces.php
Also, you might need to do use the fully qualified class name for all the other classes from the root namespace, and in general from namespaces other than the current one, or issue the proper use statement, as explained here: http://php.net/manual/en/language.namespaces.importing.php
You're wise suggested to read about namespaces
You're inside the namespace Blog\View\Helper. This means, that when you create a new tidy(); it is looking for Blog\View\Helper\tidy. If a class is within the global namespace you'd need to call new \tidy(), same with DOMDocument.
if a class is within a different namespace, you need to either use it like:
use Some\Name\Space\Tidy;
new Tidy();
or do it like
new \Some\Name\Space\Tidy();
Short version
Where does the Client class look for the DispatchableInterface interface?
class Client implements Libraries\Stdlib\DispatchableInterface
I'm trying to add the ZF2 libraries to a CodeIgniter installation. The whole Zend folder sits in:
/Users/stef/Sites/site_name/application/libraries/
In my CI controller I have
public function run()
{
$CI =& get_instance();
$CI->load->library('Zend');
$CI->load->library('Zend/Http/Client');
$client = new Client($url);
//Do stuff
}
When I access the run() method I get a fatal error:
Fatal error: Interface 'Zend\Stdlib\DispatchableInterface' not found in /Users/stef/Sites/site_name/application/libraries/Zend/Http/Client.php on line 27
In libraries/Zend.php (a file I added, not part of the ZF) I have
function __construct($class = NULL)
{
// include path for Zend Framework
// alter it accordingly if you have put the 'Zend' folder elsewhere
ini_set('include_path',ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries/Zend/');
}
It doesn't seem to matter what I set the include_path to be, even when I put bogus values the fatal error stays the same. So it seems that the loading of the interface DispatchableInterface doesn't make use of the include_path.
How can I get Client.php to "find" the interface, which it tries to do so here:
class Client implements Libraries\Stdlib\DispatchableInterface
Least difficult solution is probably:
set_include_path(get_include_path() . PATH_SEPARATOR . '/Users/stef/Sites/site_name/application/libraries/');
require_once 'Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));
$loader->register();
Then skip the CI->load part. And just use new Zend\Http\Client. Also make sure that the dispatchable interface is in /Users/stef/Sites/site_name/application/libraries/Zend/Stdlib/DispatchableInterface.php
Update:
I noticed just now:
class Client implements Libraries\Stdlib\DispatchableInterface
What class is this? Is it Zend's class? I guess it's some class of yours. And you're adding wrongly namespaced interface. IMO it should be \Zend\Stdlib\DispatchableInterface
I'm trying to set up Doctrine 2 to play with ZF 1.11 for a while. I have managed to resolve all errors except one:
PHP Fatal error: Class 'Doctrine\ORM\Configuration' not found in C:\development\porjects\application\Bootstrap.php on line 258
Below is the _iniDoctrine() function in the Bootstrap.php file up to the line 258 referred to by the error message:
protected function _initDoctrine()
{
$this->bootstrap('autoload');
require_once('Doctrine/Common/ClassLoader.php');
// Create the doctrine autoloader and remove it from the spl autoload stack (it adds itself)
require_once 'Doctrine/Common/ClassLoader.php';
$doctrineAutoloader = array(new \Doctrine\Common\ClassLoader(), 'loadClass');
spl_autoload_unregister($doctrineAutoloader);
$autoloader = Zend_Loader_Autoloader::getInstance();
// Push the doctrine autoloader to load for the Doctrine\ namespace
$autoloader->pushAutoloader($doctrineAutoloader, 'doctrine');
$classLoader = new \Doctrine\Common\ClassLoader('Entities', realpath(__DIR__ . '/models/'), 'loadClass');
$autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Entities');
$classLoader = new \Doctrine\Common\ClassLoader('Symfony', realpath(__DIR__ . '/../library/Doctrine/'), 'loadClass');
$autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Symfony');
$doctrineConfig = $this->getOption('doctrine');
$config = new \Doctrine\ORM\Configuration();
Apparently, the application fails to 'see' the Configuration class (and file). If I manually require the class before the class is instantiated. Another Class/file appears 'unseen'. A 'manual' fix definitely would not do.
The Doctrine folder which contains these files is on my include path. I have scoured the web for ideas. What please would you suggest? Thanks
I achieve to make ZF 1.x & Doctrine 2 working great together using this application resource it is available on github and well documented.
Hope that helps
If you are looking to integrate Doctrine 2 + Zend Framework you might want to use the 'glue' provided by one of the Doctrine developers (Guilherme Blanco)
https://github.com/guilhermeblanco/ZendFramework1-Doctrine2
If you haven't seen the presentation by Ralph Schindler and Guilherme Blanco, there is one on Slideshare.
http://www.slideshare.net/ralphschindler/zend-framework-1-doctrine-2-6177485
Also, Ralph has a nice example application on Github.
https://github.com/ralphschindler/NOLASnowball/tree/doctrine2-managed-crud#
There's also a great screencast at ZendCasts on how to implement Guilerme's adapter. Look for Unit Testing Doctrine 2 Entities.