iam developing small php framework for personal use.Iam trying to autoload classes with UniversalClassLoaderwich is used in Symfony.But when i try to use some these clases i got error
Fatal error: Class 'Controller' not found in /opt/lampp/htdocs/web/globeapi/Start.php on line 14
Here is Start.php
file code.
require('../libraries/loader/Loader.php');
use Symfony\Component\ClassLoader\UniversalClassLoader;
$auto = require('../config/Auto.php');
$Loader = new UniversalClassLoader();
$Loader->registerNamespaces($auto);
$Loader->register();
Controller::test();
Here is code of Controller class
namespace Libraries\Controller;
class Controller
{
function Controller()
{
}
public static function test()
{
echo 1;
}
}
here is code of Auto.php file wich returns array of classes for autoloading.
return array(
'Libraries\Controller' => '../libraries/controller/Controller.php',
'Libraries\Module' => '../libraries/module/Module.php',
'Libraries\View' => '../libraries/view/View.php',
'Libraries\Sammy' => '../libraries/sammy/Sammy.php',
'Libraries\Routes' => '../config/Routes.php'
);
My answer is using the current version of Symfony (2.2) and the UniversalClassLoader. The general idea is to follow the PSR-0 standard so that you don't have to define a mapping entry for each file. Just by following simple naming and location conventions your classes will be found - neat, isn't it? :-) (note that both directory and file names are case sensitive).
The directory structure (the vendor directory is created by composer)
The composer.json
The content of app.php:
And finally the controller class: