问题
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'
);
回答1:
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)
app.php
composer.json
src
App
Libraries
Controller
Controller.php
vendor
symfony
class-loader
Symfony
Component
ClassLoader
The composer.json
{
"require": {
"symfony/class-loader": "2.2.*"
}
}
The content of app.php:
require_once 'vendor/symfony/class-loader/Symfony/Component/ClassLoader/UniversalClassLoader.php';
use Symfony\Component\ClassLoader\UniversalClassLoader;
$loader = new UniversalClassLoader();
$loader->registerNamespace('App', 'src');
$loader->register();
\App\Libraries\Controller\Controller::test();
And finally the controller class:
//src/App/Libraries/Controller/Controller.php
namespace App\Libraries\Controller;
class Controller
{
public static function test()
{
echo 1;
}
}
来源:https://stackoverflow.com/questions/16505455/symfony-classloader-wont-load