我的申请文件:
<?php // /src/app.php
require_once __DIR__ . '/../lib/vendor/Sensio/silex.phar';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Foo\Bar;
$app = new Silex\Application();
$app['autoloader']->registerNamespace('Foo', __DIR__);
$bar = new Bar();
(...)
我的酒吧课程
<?php /src/Bar.php
namespace Foo;
use Silex\Application;
use Silex\ControllerProviderInterface;
use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\Response;
class Bar implements ControllerProviderInterface { ... }
当我在$bar = new Bar()
中执行app.php
时,出现错误:Fatal error: Class 'Moken\Classname' not found in (...)/src/app.php on line 11
谁能告诉我我做错了什么?
答案 0 :(得分:3)
如果您使用namespace Foo;
,则必须在Foo
目录中找到此课程
每个命名空间部分都是symfony
如果不起作用,则必须显示加载程序在哪里可以找到此类 在symfony2中我用它:
use Symfony\Component\ClassLoader\UniversalClassLoader;
$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
// HERE LOCATED FRAMEWORK SPECIFIED PATHS
// app namespaces
'Foo' => __DIR__ . '/../src',
));
答案 1 :(得分:0)
在您的主要php文件(index.php)中,您必须:
例如(Example \ Controllers是命名空间,XyzControllerProvider是Controller Provider,url是/ my / example):
[...]
// declare the use of your Controller Provider
use Example\Controllers\XyzControllerProvider;
[...]
//after creation of your Application object you must register your namespace;
$app = Application();
$app['autoloader']->registerNamespace('Example', __DIR__.'/src');
[...]
//mount your Controller Provider
$app->mount('/my/example', new Example\Controllers\XyzControllerProvider());
Controller Provider(在src / example / controllers下)将是:
<?php
namespace Example\Controllers;
use Silex\Application;
use Silex\ControllerProviderInterface;
use Silex\ControllerCollection;
class XyzControllerProvider implements ControllerProviderInterface {
public function connect(Application $app) {
$controllers = new ControllerCollection();
$controllers->get('/', function (Application $app) {
return "DONE;"
});
return $controllers;
}
}