Cakephp 3无法从插件加载类

时间:2015-04-17 13:59:34

标签: php cakephp logging plugins cakephp-3.0

我写了一个插件来编写数据库中的日志。我的文件夹结构如下所示:

plugins/Logging/src/Log/Engine/DatabaseLog

该课程如下:

  <?php

    namespace Logging\Log\Engine;

    use Cake\Log\Engine\BaseLog;
    use Cake\ORM\TableRegistry;

class DatabaseLog extends BaseLog{

private $Model;

public function __construct(array $config = []){
  parent::__construct($config);
}

public function log($level, $message, array $context = []){

//Laden des Models
if(!$context || !array_key_exists('model', $context)){
   $context['model'] = 'SystemLogs';
}
$this->Model = TableRegistry::get($context['model']);

$log_data = [
  'level' => $level,
  'message' => $message
]; 

$entity = $this->Model->newEntity($log_data);
$this->Model->save($entity);

return true;
 }
}
?>

在我的app.php中:

'Log' => [
  'debug' => [
      'className' => 'Logging.DatabaseLog',
  ]
],

我需要更改以便加载类

由于

3 个答案:

答案 0 :(得分:2)

Dunno,如果有人还需要这个。但为了以防万一。

自从我开始使用蛋糕3以来,我已经遇到过几次。

但是,只要我将此行添加到我的root bootstrap.php

的末尾
Plugin::load('[Name_of_Plugin]', ['bootstrap' => false, 'routes' => true]);

它始终有效。

干杯。

答案 1 :(得分:0)

我的错误消息:

  

致命错误:未捕获的异常&#39; RuntimeException&#39;有消息&#39;无法加载类DatabaseLog&#39;在/Users/patrick/Sites/vz/vendor/cakephp/cakephp/src/Log/LogEngineRegistry.php:57堆栈跟踪:#0 / Users / patrick / Sites / vz / vendor / cakephp / cakephp / src / Core / ObjectRegistry .php(86):Cake \ Log \ LogEngineRegistry-&gt; _throwMissingClassError(&#39; DatabaseLog&#39;,&#39; Logging&#39;)#1 / Users / patrick / Sites / vz / vendor / cakephp / cakephp /src/Log/Log.php(198):Cake \ Core \ ObjectRegistry-&gt; load(&#39; debug&#39;,Array)#2 / Users / patrick / Sites / vz / vendor / cakephp / cakephp / src / Log / Log.php(180):Cake \ Log \ Log :: _ loadConfig()#3 /Users/patrick/Sites/vz/vendor/cakephp/cakephp/src/Log/Log.php(346):Cake \ Log \ Log :: _ init()#4 /Users/patrick/Sites/vz/vendor/cakephp/cakephp/src/Log/Log.php(448):Cake \ Log \ Log :: write(&#39;错误&#39;,&#39; [RuntimeExcepti ...&#39;,数组)#5 /Users/patrick/Sites/vz/vendor/cakephp/cakephp/src/Error/BaseErrorHandler.php(245):蛋糕\ Log \ Log :: error(&#39; [RuntimeExcepti ...&#39;)#6 /Users/patrick/Sites/vz/vendor/cakephp/cakephp/src/Error/BaseErrorHandler.php(156): / Users / patr中的Cake \ Error \ Base第57行的ick / Sites / vz / vendor / cakephp / cakephp / src / Log / LogEngineRegistry.php

如果我正确理解文档,我不需要composer.json。 在我的bootstrap.php中。

bootstrap.php中:

Plugin::load('Logging', ['autoload' => true]);

我还调整了composer.json:

composer.json:

"autoload": {
    "psr-4": {
        "App\\": "src",
        "Logging\\": "./plugins/Logging/src"
    }
},

答案 2 :(得分:-3)

请阅读有关如何从插件中自动加载类的官方文档的部分:

http://book.cakephp.org/3.0/en/plugins.html#autoloading-plugin-classes