Giter Site home page Giter Site logo

Comments (8)

Guyverix avatar Guyverix commented on May 27, 2024

I should clarify, the "$test = $this->get(PDO::class);" is one of the attempts I have made in my Action however since I already see the data inside the object, I moved on to trying to pull it out via the "$this->db->driver" and failing..

Sorry for any confusion.

from slim-skeleton.

l0gicgate avatar l0gicgate commented on May 27, 2024

You need to add PDO::class as a dependency in the constructor of your actions, PHP-DI will inject the dependency in there for you.

<?php
declare(strict_types=1);

namespace App\Application\Actions\User;

use App\Application\Actions\Action;
use App\Domain\User\UserRepository;
use Psr\Log\LoggerInterface;

abstract class MyAction extends Action
{
    protected PDO $db;

    public function __construct(LoggerInterface $logger, PDO $db)
    {
        parent::__construct($logger);
        $this->db = $db;
    }

   protected function action(): Response
   {
       $test = $this->db->doSomething();
   }
}

from slim-skeleton.

Guyverix avatar Guyverix commented on May 27, 2024

Still getting errs that it cannot seem to find the PDO that are defined in the settings.php and dependencies.php files..

"Error while injecting dependencies into App\\Application\\Actions\\Mapping\\ViewAllMappingAction: No entry or class found for 'App\\Application\\Actions\\Mapping\\PDO'"

from slim-skeleton.

Guyverix avatar Guyverix commented on May 27, 2024

This is literally what I have defined in the depnedencies.php. I know full well I am the one causing the problem not understanding the framework well enough yet. Just one heck of a road-block that I cannot seem to get past.

<?php
declare(strict_types=1);

use App\Application\Settings\SettingsInterface;
use App\Application\Settings\Settings;
use DI\ContainerBuilder;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Monolog\Processor\UidProcessor;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

return function (ContainerBuilder $containerBuilder) {
    $containerBuilder->addDefinitions([
        LoggerInterface::class => function (ContainerInterface $c) {
            $settings = $c->get(SettingsInterface::class);

            $loggerSettings = $settings->get('logger');
            $logger = new Logger($loggerSettings['name']);

            $processor = new UidProcessor();
            $logger->pushProcessor($processor);

            $handler = new StreamHandler($loggerSettings['path'], $loggerSettings['level']);
            $logger->pushHandler($handler);

            return $logger;
        },
        PDO::class => function (ContainerInterface $c) {
            $settings = $c->get(SettingsInterface::class);
            $dbSettings = $settings->get('db');
            $host = $dbSettings['host'];
            $dbname = $dbSettings['database'];
            $username = $dbSettings['username'];
            $password = $dbSettings['password'];
            $charset = $dbSettings['charset'];
            $flags = $dbSettings['flags'];
            $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
            return new PDO($dsn, $username, $password);
        },
    ]);
};

from slim-skeleton.

eugene-borovov avatar eugene-borovov commented on May 27, 2024

Still getting errs that it cannot seem to find the PDO that are defined in the settings.php and dependencies.php files..

"Error while injecting dependencies into App\\Application\\Actions\\Mapping\\ViewAllMappingAction: No entry or class found for 'App\\Application\\Actions\\Mapping\\PDO'"

Add a backslash to the PDO class reference:

abstract class MyAction extends Action
{
    protected \PDO $db;

from slim-skeleton.

Guyverix avatar Guyverix commented on May 27, 2024

No change. Still says it cannot find the class. In this framework, is it more reasonable to create a separate PDO file and call it with use instead of trying to make dependencies.php work?

Error while injecting dependencies into App\\Application\\Actions\\Mapping\\ViewAllMappingAction: No entry or class found for 'App\\Application\\Actions\\Mapping\\PDO'

from slim-skeleton.

eugene-borovov avatar eugene-borovov commented on May 27, 2024
<?php
declare(strict_types=1);

namespace App\Application\Actions\User;

use App\Application\Actions\Action;
use App\Domain\User\UserRepository;
use Psr\Log\LoggerInterface;

abstract class MyAction extends Action
{
    protected \PDO $db;

    public function __construct(LoggerInterface $logger, \PDO $db)
    {
        parent::__construct($logger);
        $this->db = $db;
    }

   protected function action(): Response
   {
       $test = $this->db->doSomething();
   }
}

You`ve declared the \App\Application\Actions\Mapping. So PDO without namespace will be \App\Application\Actions\Mapping\PDO. You need \PDO class for $db variable.

from slim-skeleton.

Guyverix avatar Guyverix commented on May 27, 2024

Thank you for the example. This is making more sense to me now.

from slim-skeleton.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.