Cakephp 3 Failed to load class from plugin

I wrote a plugin to write logs to a database. My folder structure looks like this:

plugins/Logging/src/Log/Engine/DatabaseLog

      

The class looks like this:

  <?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;
 }
}
?>

      

In my app.php:

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

      

what do i need to change for the class to load

thank

+3


source to share


3 answers


I don't know if anyone else needs it. But just in case.

I ran into this a few times since I started using cake 3.

However, if I add this line to the end of my bootstrap.php root



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

      

It always works.

Greetings.

+2


source


my error message:

Fatal error: Throw a "RuntimeException" with the message "Failed to load DatabaseLog class" in /Users/patrick/Sites/vz/vendor/cakephp/cakephp/src/Log/LogEngineRegistry.php:57 Stack trace: # 0 / Users / patrick / Sites / vz / vendor / cakephp / cakephp / src / Core / ObjectRegistry.php (86): Cake \ Log \ LogEngineRegistry → _ throwMissingClassError ('DatabaseLog', 'Logging') # 1 / Users / patrick / Sites / vz /vendor/cakephp/cakephp/src/Log/Log.php(198): Cake \ Core \ ObjectRegistry-> load ('debug', 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 ('error ',' [RuntimeExcepti ... ', Array) # 5 / Users / patrick / Sites / vz / vendor / cakephp / cakephp / src / Error / BaseErrorHandler.php (245): Cake \ Log \ Log :: error ( error) '[RuntimeExcepti ...') # 6 / Users / patrick / Websites / vz / vendor / cakephp / cakephp / src / Error / BaseError Handler.php (156): Cake \ Error \ Base in / Users / patrick / Sites / vz / vendor / cakephp / cakephp / src / Log / LogEngineRegistry.php on line 57

If I understand the documentation correctly, I don't need the .json composer. in my bootstrap.php.

bootstrap.php:



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

      

I also adjusted the composer .json:

composer.json:

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

      

0


source


Please see the official documentation section on how to automatically load classes from plugins:

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

-3


source







All Articles