简体   繁体   English

如何使用 Silex 自动加载器注册命名空间

[英]How do you register a namespace with Silex autoloader

I'm experimenting with creating an extension with the Silex php micro framework for user authentication but I can't seem to get the autoloader to work.我正在尝试使用Silex php 微框架创建扩展以进行用户身份验证,但我似乎无法让自动加载器工作。 Can anyone shed any light?任何人都可以解释一下吗?

I have a directory structure like this (truncated)我有这样的目录结构(截断)

usertest
|_lib
| |_silex.phar
| |_MyNamespace
|   |_UserExtension.php
|   |_User.php
|_www
  |_index.php

The pertinent bits of index.php, which serves as the bootstrap and the front controller look like this: index.php 的相关位,用作引导程序,前面的 controller 如下所示:

require '../lib/silex.phar';

use Silex\Application;
use MyNamespace\UserExtension;

$app = new Application();
$app['autoloader']->registerNamespace( 'MyNamespace', '../lib' );
$app->register( new UserExtension() );

The class I'm trying to load looks similar this:我尝试加载的 class 看起来与此类似:

namespace MyNamespace;

use Silex\Application;
use Silex\ExtensionInterface;

class UserExtension implements ExtensionInterface {
    public function register( Application $app ) {
        $app['user'] = $app->share( function() use( $app ) {
            return new User();
        });
    }
}

All pretty straight forward except it throws this error:除了抛出这个错误之外,一切都非常简单:

Fatal error: Class 'MyNamespace\UserExtension' not found in /home/meouw/Projects/php/usertest/www/index.php on line 8

I have dabbled with symfony2 and have successfully followed the instructions for setting up the universal class loader , but in this instance I am stumped.我已经涉足 symfony2 并成功地按照说明设置通用 class 加载程序,但在这种情况下我很难过。 Am I missing something?我错过了什么吗? Any help would be appreciated.任何帮助,将不胜感激。

In recent versions of Silex the autoloader is deprecated and you should register all your namespaces through the composer.json file which imo is a nicer solution because you are centralizing your autoloading definitions.在最近的 Silex 版本中,自动加载器已被弃用,您应该通过composer.json文件注册所有命名空间,imo 是一个更好的解决方案,因为您正在集中您的自动加载定义。

Example:例子:

{
    "require": {
        "silex/silex": "1.0.*@dev"
    },
    "autoload": {
        "psr-0": {
            "MyNameSpace": "src/"
        }
    }
}

In fact when you try to access the autoloader in any recent version of Silex the following RuntimeException is thrown:事实上,当您尝试在任何最新版本的 Silex 中访问自动加载器时,都会引发以下 RuntimeException:

You tried to access the autoloader service.您试图访问自动装载机服务。 The autoloader has been removed from Silex.自动装载机已从 Silex 中移除。 It is recommended that you use Composer to manage your dependencies and handle your autoloading.建议您使用 Composer 来管理您的依赖项并处理您的自动加载。 See http://getcomposer.org for more information.有关详细信息,请参阅http://getcomposer.org

Deprecated - As of 2014-10-21 PSR-0 has been marked as deprecated.
PSR-4 is now recommended as an alternative

That is why you should use PSR-4 syntax in composer.json这就是为什么你应该在 composer.json 中使用 PSR-4 语法

{
  "require": {
      "silex/silex": "1.0.*@dev",
  },
  "autoload": {
      "psr-4": {
          "Vendor\\Namespace\\": "/path"
      }
  }
}

I'd use我会用

$app['autoloader']->registerNamespace('MyNamespace', __DIR__.'/../lib');

To register namespaces, just call registerNamespaces() like this:要注册命名空间,只需像这样调用registerNamespaces()

$app = new Silex\Application();

$app['autoloader']->registerNamespaces(array(
    'Symfony'          => __DIR__.'/../vendor/',
    'Panda'            => __DIR__.'/../vendor/SilexDiscountServiceProvider/src',
    'Knp'              => __DIR__.'/../vendor/KnpSilexExtensions/',
    // ...
));

Both adding appropriate statement to the autoload section of composer.json and registering namespaces directly calling registerNamespace was not working for me, until I executed composer update in the projects folder.composer.jsonautoload部分添加适当的语句和直接调用registerNamespace注册命名空间对我不起作用,直到我在项目文件夹中执行composer update

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM