简体   繁体   English

Silex自动加载我的名称空间

[英]Silex autoload my namespace

The site is using Silex. 该网站正在使用Silex。

Structure look like this: 结构如下所示:

root
  api
    RSYWX
      Book.php
  vendor
  views
  web

In Book.php : Book.php

namespace RSYWX;

class Book {
  public static function sayHello() 
  {
      return "Hello, there!";
  }
}

In composer.json : composer.json

{
  "require": {
    "silex/silex": "~1.1",
    "twig/twig": "^1.23",
    "doctrine/dbal": "~2.2",
    "symfony/var-dumper": "^2.7"
  },
  "autoload": {
    "psr-4": {
        "RSYWX\\": "/api/RSYWX"
      }
  }
}

I ran composer update to update the autoload relevant files, in particular the vendor/composer/autoload_psr4.php : 我运行了composer update来更新自动加载相关文件,尤其是vendor/composer/autoload_psr4.php

return array( 
  ...
  'RSYWX\\' => array('/api/RSYWX'), 
  ...

When I tried to call sayHello from index.php file, the error log has this line: 当我尝试从index.php文件调用sayHello ,错误日志显示以下行:

PHP Fatal error:  Class 'RSYWX\\Book' not found in vagrant/api.rsywx.com/web/index.php on line 32

Any hints to load this class and the static method? 有什么提示可以加载此类和静态方法吗?

Try with: 尝试:

"RSYWX\\": "api/RSYWX"

Because using / on beginning refers to server's root directory and it's probably not what you wanted. 因为在开始使用/指服务器的根目录,可能不是您想要的。 Of course call composer update after changing composer.json file. 当然,在更改composer.json文件后调用composer update

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

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