Slim 3 控制器未连接到数据库容器
Slim 3 controller not connecting to db container
我是 slim 框架的新手,我正在使用路由的匿名函数来访问具有给定路由及其工作的数据库数据,但是如果我尝试将它们传递给控制器,我会收到以下错误消息
Slim Application Error The application could not run because of the
following error:
Details
Type: Error Message: Call to undefined method
Slim\Container::prepare() File:
C:\xampp\htdocs\drivingapp\app\src\Controllers\apiController.php Line:
22 Trace
#0 [internal function]: App\Controllers\apiController->igice(Object(Slim\Http\Request),
Object(Slim\Http\Response), Array)
#1 C:\xampp\htdocs\drivingapp\vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php(41):
call_user_func(Array, Object(Slim\Http\Request),
Object(Slim\Http\Response), Array)
#2 C:\xampp\htdocs\drivingapp\vendor\slim\slim\Slim\Route.php(344): Slim\Handlers\Strategies\RequestResponse->__invoke(Array,
Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#3 C:\xampp\htdocs\drivingapp\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(116):
Slim\Route->__invoke(Object(Slim\Http\Request),
Object(Slim\Http\Response))
#4 C:\xampp\htdocs\drivingapp\vendor\slim\slim\Slim\Route.php(316): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request),
Object(Slim\Http\Response))
#5 C:\xampp\htdocs\drivingapp\vendor\slim\slim\Slim\App.php(438): Slim\Route->run(Object(Slim\Http\Request), Object(Slim\Http\Response))
#6 C:\xampp\htdocs\drivingapp\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(116):
Slim\App->__invoke(Object(Slim\Http\Request),
Object(Slim\Http\Response))
#7 C:\xampp\htdocs\drivingapp\vendor\slim\slim\Slim\App.php(332): Slim\App->callMiddlewareStack(Object(Slim\Http\Request),
Object(Slim\Http\Response))
#8 C:\xampp\htdocs\drivingapp\vendor\slim\slim\Slim\App.php(293): Slim\App->process(Object(Slim\Http\Request),
Object(Slim\Http\Response))
#9 C:\xampp\htdocs\drivingapp\public\index.php(30): Slim\App->run()
#10 {main}
下面是我的apiController.php:
<?php
namespace App\Controllers;
use Slim\Views\Twig;
use Psr\Log\LoggerInterface;
use Slim\Http\Request;
use Slim\Http\Response;
class apiController{
private $db;
public function __construct($db){
$this->db = $db;
}
public function igice($request, $response){
$stmt = $this->db->prepare("SELECT * FROM igice ORDER BY igiceid");
$stmt->execute();
$dowork = $stmt->fetchAll();
return $this->response->withJson($dowork);
}
}
下面是我的 dependencie.php
$container['db'] = function ($c) {
$settings = $c->get('settings')['db'];
$pdo = new PDO("mysql:host=" . $settings['host'] . ";dbname=" . $settings['dbname'],
$settings['user'], $settings['pass']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $pdo;
};
// -----------------------------------------------------------------------------
// Registering Controllers
// -----------------------------------------------------------------------------
$container[App\Action\HomeAction::class] = function ($c) {
return new App\Action\HomeAction($c->get('view'), $c->get('logger'));
};
$container['apiController']=function($c){
return new App\Controllers\apiController($c->get('db'));
};
低于我的 routes.php:
$app->get('/ibice','App\Controllers\apiController:igice');
修改apiController.php
<?php
namespace App\apiController;
use Slim\Views\Twig;
use Psr\Log\LoggerInterface;
use Slim\Http\Request;
use Slim\Http\Response;
use PDO;
final class apiController{
private $db;
public function __construct($host, $dbname, $user, $password){
try {
$this->db = new PDO("mysql:host=" . $host . ";dbname=" . $dbname, $user, $password);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
public function getDb() {
if ($this->db instanceof PDO) {
return $this->db;
}
}
}
然后你应该为每个路由编写函数,我给你一个例子
<?php
namespace App\apiController;
use Slim\Views\Twig;
use Psr\Log\LoggerInterface;
use Slim\Http\Request;
use Slim\Http\Response;
final class Hello
{
private $view;
private $conn;
public function __construct(Twig $view, $db)
{
$this->view = $view;
$this->conn = $db->getDb();
}
public function __invoke(Request $request, Response $response, $args)
{
$this->logger->info("action dispatched");
$stmt = $this->conn->prepare("SELECT * FROM igice ORDER BY igiceid");
$stmt->execute();
$dowork = $stmt->fetchAll();
return $response->withJson($dowork,201);
}
}
修改dependencies.php
$container['apiController']=function($c){
return new App\Controllers\apiController::class($c->get('db'));
};
最好将容器添加到您的 apiController 构造函数,以便轻松访问包括数据库在内的所有容器对象,这样您就可以像这个示例一样轻松更改您的代码:
只需将 __get 魔术方法添加到您的控制器中,您就可以轻松访问控制器中的所有容器对象,只需使用 $this->dependency name ;)
<?php
namespace App\Controllers;
use Slim\Views\Twig;
use Psr\Log\LoggerInterface;
use Slim\Http\Request;
use Slim\Http\Response;
class apiController{
protected $container;
public function __construct($container){
$this->container = $container;
}
public function __get($property)
{
if ($this->container->{$property}) {
return $this->container->{$property};
}
}
public function igice($request, $response){
$stmt = $this->db->prepare("SELECT * FROM igice ORDER BY igiceid");
$stmt->execute();
$dowork = $stmt->fetchAll();
return $this->response->withJson($dowork);
}
}
在您的依赖项中:
$container['apiController']=function($c){
return new App\Controllers\apiController($c);
};
以及路线:
$app->get('/ibice','App\Controllers\apiController:igice');
喜欢你之前的
我是 slim 框架的新手,我正在使用路由的匿名函数来访问具有给定路由及其工作的数据库数据,但是如果我尝试将它们传递给控制器,我会收到以下错误消息
Slim Application Error The application could not run because of the following error:
Details
Type: Error Message: Call to undefined method Slim\Container::prepare() File: C:\xampp\htdocs\drivingapp\app\src\Controllers\apiController.php Line: 22 Trace
#0 [internal function]: App\Controllers\apiController->igice(Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#1 C:\xampp\htdocs\drivingapp\vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php(41): call_user_func(Array, Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#2 C:\xampp\htdocs\drivingapp\vendor\slim\slim\Slim\Route.php(344): Slim\Handlers\Strategies\RequestResponse->__invoke(Array, Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#3 C:\xampp\htdocs\drivingapp\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(116): Slim\Route->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#4 C:\xampp\htdocs\drivingapp\vendor\slim\slim\Slim\Route.php(316): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#5 C:\xampp\htdocs\drivingapp\vendor\slim\slim\Slim\App.php(438): Slim\Route->run(Object(Slim\Http\Request), Object(Slim\Http\Response))
#6 C:\xampp\htdocs\drivingapp\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(116): Slim\App->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#7 C:\xampp\htdocs\drivingapp\vendor\slim\slim\Slim\App.php(332): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#8 C:\xampp\htdocs\drivingapp\vendor\slim\slim\Slim\App.php(293): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#9 C:\xampp\htdocs\drivingapp\public\index.php(30): Slim\App->run()
#10 {main}
下面是我的apiController.php:
<?php
namespace App\Controllers;
use Slim\Views\Twig;
use Psr\Log\LoggerInterface;
use Slim\Http\Request;
use Slim\Http\Response;
class apiController{
private $db;
public function __construct($db){
$this->db = $db;
}
public function igice($request, $response){
$stmt = $this->db->prepare("SELECT * FROM igice ORDER BY igiceid");
$stmt->execute();
$dowork = $stmt->fetchAll();
return $this->response->withJson($dowork);
}
}
下面是我的 dependencie.php
$container['db'] = function ($c) {
$settings = $c->get('settings')['db'];
$pdo = new PDO("mysql:host=" . $settings['host'] . ";dbname=" . $settings['dbname'],
$settings['user'], $settings['pass']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $pdo;
};
// -----------------------------------------------------------------------------
// Registering Controllers
// -----------------------------------------------------------------------------
$container[App\Action\HomeAction::class] = function ($c) {
return new App\Action\HomeAction($c->get('view'), $c->get('logger'));
};
$container['apiController']=function($c){
return new App\Controllers\apiController($c->get('db'));
};
低于我的 routes.php:
$app->get('/ibice','App\Controllers\apiController:igice');
修改apiController.php
<?php
namespace App\apiController;
use Slim\Views\Twig;
use Psr\Log\LoggerInterface;
use Slim\Http\Request;
use Slim\Http\Response;
use PDO;
final class apiController{
private $db;
public function __construct($host, $dbname, $user, $password){
try {
$this->db = new PDO("mysql:host=" . $host . ";dbname=" . $dbname, $user, $password);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
public function getDb() {
if ($this->db instanceof PDO) {
return $this->db;
}
}
}
然后你应该为每个路由编写函数,我给你一个例子
<?php
namespace App\apiController;
use Slim\Views\Twig;
use Psr\Log\LoggerInterface;
use Slim\Http\Request;
use Slim\Http\Response;
final class Hello
{
private $view;
private $conn;
public function __construct(Twig $view, $db)
{
$this->view = $view;
$this->conn = $db->getDb();
}
public function __invoke(Request $request, Response $response, $args)
{
$this->logger->info("action dispatched");
$stmt = $this->conn->prepare("SELECT * FROM igice ORDER BY igiceid");
$stmt->execute();
$dowork = $stmt->fetchAll();
return $response->withJson($dowork,201);
}
}
修改dependencies.php
$container['apiController']=function($c){
return new App\Controllers\apiController::class($c->get('db'));
};
最好将容器添加到您的 apiController 构造函数,以便轻松访问包括数据库在内的所有容器对象,这样您就可以像这个示例一样轻松更改您的代码:
只需将 __get 魔术方法添加到您的控制器中,您就可以轻松访问控制器中的所有容器对象,只需使用 $this->dependency name ;)
<?php
namespace App\Controllers;
use Slim\Views\Twig;
use Psr\Log\LoggerInterface;
use Slim\Http\Request;
use Slim\Http\Response;
class apiController{
protected $container;
public function __construct($container){
$this->container = $container;
}
public function __get($property)
{
if ($this->container->{$property}) {
return $this->container->{$property};
}
}
public function igice($request, $response){
$stmt = $this->db->prepare("SELECT * FROM igice ORDER BY igiceid");
$stmt->execute();
$dowork = $stmt->fetchAll();
return $this->response->withJson($dowork);
}
}
在您的依赖项中:
$container['apiController']=function($c){
return new App\Controllers\apiController($c);
};
以及路线:
$app->get('/ibice','App\Controllers\apiController:igice');
喜欢你之前的