我是作曲家和自动装弹机的新手.我想我也缺乏文件组织策略.
我正在尝试建立一个关于slimframework的新项目.
我有一些Slim课程.但我不能在我的项目中自动加载它们.
/
> composer.json
> composer.phar
>供应商
>配置
> someapiparams.php
> database.php
> cache.php
> general.php
>公众
> index.php
>模特
>图书馆
> Foo
>苗条
> Config.php
> Cache.php
/composer.json:
"autoload": {
"psr-0": {
"Foo": "libraries/"
}
}
/libraries/Foo/Slim/Config.php:
<?php
class Config {
/**
* Loads a file based on $key param under ROOT . "/config",
* if not already loaded.
* Then returns an array.
*/
public static function get($key) {}
}
/libraries/Foo/Slim/Cache.php:
<?php
class Cache{
/**
* Initialize a caching engine defined in config file if not already done.
* Then runs corrensponding engine methods for getting and setting.
*/
public static function init() {
$config = Config::get("cache");
// initialize driver.
}
public static function __get($key) {}
public static function __set($key, $value, $params) {}
}
/public/index.php:
require ROOT."/vendor/autoload.php";
$app = new Slim\Slim();
var_dump(Config::get("database")); exit;
//var_dump(Foo\Slim\Config::get("database")); exit;
//var_dump(Slim\Config::get("database")); exit;
错误是找不到Config类.
最佳答案 你忘记了:
namespace Foo/Slim;
在/libraries/Foo/Slim/Cache.php的顶部(或者可能已经为代码示例剪了它).
如果添加命名空间不能解决问题,则应使用调试器逐步执行代码,并在尝试加载类并失败时准确查看Composer自动加载器正在搜索的文件.