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.
Related
I am relatively new to Zend as I am working with Apigility.
When I try to include classes from external files, which are part of another MVC project, files are read, but creating instances returns an error.
If I use an include/require function class_exists return true, but still I can't create objects, while using one of the autoloading procedures from Zend_Loader gives the error "class *** not found" (class_exists return false in this case) and prints on screen the entire content of the file.
It's not due to my MVC, because I got the same problem including a class created on the fly.
And it isn't a match problem.
Any ideas?
EDIT - As I am trying to load class User from function getServiceConfig() in Module.php of an Apigility application named 'Example'
// Zend\Loader\StandardAutoloader namespace is stated at the beginning and it is working
$loader = new StandardAutoloader();
$loader->registerPrefix('local', '/home/mylocal/class/local');
$loader->autoload('local_User');
$loader->check = class_exists('User'); // states true and is listed in get_declared_classes()
return new User(); // returns the error: Class 'Example\User' not found in /home/mylocal/apigility/module/Example/src/Example/Module.php on line *
The same code works correctly if used in the index.php file of Apigility public folder where there is no interference from Example namespace.
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 need a fast way to test a code, please I know is not the best practice, but is there a way to load a class on a symfony controller without having to add namespaces, proxy class and autoloader stuff?
I tried:
public function singlepagestatsAction($id, $pid, $group) {
include '/var/www/web/welcomestuff/scripts/ga/gapi.class.php';
$ga = new gapi('email','password');
Inside my controller, but when i execute it; Fatal error: Class 'Done\PunctisBundle\Controller\gapi' not found in /var/www/src/Done/PunctisBundle/Controller/BrandController.php on line 630
You should add \ before class name
$ga = new \gapi('email','password');
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;
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