Slim 3控制器未连接到db容器

时间:2017-02-06 07:26:23

标签: php pdo slim

我是一个瘦身框架的新手,我正在使用路由的匿名函数来访问具有给定路径及其工作的数据库数据但是如果我尝试在控制器中传递它们我会得到错误

  

超薄应用程序错误应用程序无法运行,因为   以下错误:

     

详细

     

类型:错误消息:调用未定义的方法   Slim \ Container :: prepare()文件:   C:\ xampp \ htdocs \ drivingapp \ app \ src \ Controllers \ apiController.php行:   22追踪

     

#0 [内部函数]:App \ Controllers \ apiController-> igice(Object(Slim \ Http \ Request),   对象(Slim \ Http \ Response),数组)

     

#1 C:\ xampp \ htdocs \ drivingapp \ vendor \ slim \ slim \ Slim \ Handlers \ Strategies \ RequestResponse.php(41):   call_user_func(Array,Object(Slim \ Http \ Request),   对象(Slim \ Http \ Response),数组)

     

#2 C:\ xampp \ htdocs \ drivingapp \ vendor \ slim \ slim \ Slim \ Route.php(344):Slim \ Handlers \ Strategies \ RequestResponse-> __ invoke(Array,   对象(Slim \ Http \ Request),Object(Slim \ Http \ Response),Array)

     

#3 C:\ xampp \ htdocs \ drivingapp \ vendor \ slim \ slim \ Slim \ MiddlewareAwareTrait.php(116):   超薄\基于路由> __调用(对象(超薄\ HTTP \请求),   对象(超薄\ HTTP \响应))

     

#4 C:\ xampp \ htdocs \ drivingapp \ vendor \ slim \ slim \ Slim \ Route.php(316):Slim \ Route-> callMiddlewareStack(Object(Slim \ Http \ Request),   对象(超薄\ HTTP \响应))

     

#5 C:\ xampp \ htdocs \ drivingapp \ vendor \ slim \ slim \ Slim \ App.php(438):Slim \ Route-> run(Object(Slim \ Http \ Request),Object(Slim \ HTTP \响应))

     

#6 C:\ xampp \ htdocs \ drivingapp \ vendor \ slim \ slim \ Slim \ MiddlewareAwareTrait.php(116):   超薄\ APP-> __调用(对象(超薄\ HTTP \请求),   对象(超薄\ HTTP \响应))

     

#7 C:\ xampp \ htdocs \ drivingapp \ vendor \ slim \ slim \ Slim \ App.php(332):Slim \ App-> callMiddlewareStack(Object(Slim \ Http \ Request),   对象(超薄\ HTTP \响应))

     

#8 C:\ xampp \ htdocs \ drivingapp \ vendor \ slim \ slim \ Slim \ App.php(293):Slim \ App-> process(Object(Slim \ Http \ Request),   对象(超薄\ HTTP \响应))

     

#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);
}
}

吼叫,这是我的我的dependdencie.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');

2 个答案:

答案 0 :(得分:1)

最好将容器添加到apiController构造函数中,以便轻松访问包括db在内的所有容器对象,这样您就可以像这样轻松地更改代码:

只需将__get魔术方法添加到您的控制器,您就可以使用$ this-&gt;依赖名称轻松访问控制器中的所有容器对象;)

<?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');

喜欢你之前

答案 1 :(得分:0)

修改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'));
};