Comments (8)
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.
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.
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.
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.
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.
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.
<?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.
Thank you for the example. This is making more sense to me now.
from slim-skeleton.
Related Issues (20)
- delete
- Error 500 HOT 2
- Compatibility with PHP 8.1 HOT 1
- LICENSE.md
- Controller Middleware HOT 1
- `src/Application/Actions/ActionError` has wrong type `$description` HOT 2
- Guide for Slim skeleton
- .htaccess files
- DataBase in action.php
- Root URL includes unnecessary slash when installed in subfolder, how to remove it? HOT 3
- String interpolation syntax in ViewUserAction is deprecated in PHP 8.2
- Links to docs or tutorials on some basics like adding Twig
- PHP warnings cause the response to be a mess consisting of a partial JSON and an error JSON HOT 1
- I don't want a local server HOT 1
- How to update the skeleton with changes from this repo... HOT 3
- Log levels are now stored as an enum Monolog\Level
- Create a Slim project without the user example HOT 1
- User actions should be in the Domain namespace instead of the Application namespace HOT 1
- PHP version bump HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from slim-skeleton.