如何从其他类访问 Slim 容器?
How to access to Slim container from other classess?
假设在我的 dependencies.php
文件中我设置了这个容器:
<?php
$container = $app->getContainer();
$container['db'] = function($config)
{
$db = $config['settings']['db'];
$pdo = new PDO("mysql:host=" . $db['host'] . ";port=" . $db['port'] .
";dbname=" . $db['dbname'] . ";charset=" . $db['charset'], $db['user'], $db['pass']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $pdo;
};
我可以在我的 route
中使用这个 container
,例如 table.php
:
<?php
use Slim\Http\Request;
use Slim\Http\Response;
$app->get('/table/get_table', function (Request $request, Response $response, array $args)
{
$sql = $this->db->prepare("SELECT * FROM some_table");
$sql->execute();
$result = $sql->fetchAll();
return $response->withJson($result);
});
这基本上是默认用法,对吧?也就是说,我怎样才能使用其他 classes 的 db
容器?假设我创建了一个名为 TableUtility
的 class 并导入到 table.php
:
class TableUtility
{
function GetTableFromDb()
{
$sql = $this->db->prepare("SELECT * FROM some_table");
$sql->execute();
$result = $sql->fetchAll();
return $response->withJson($result);
}
}
如您所见,我将 PDO
的逻辑移到了 TableUtility
的 GetTableFromDb
中,我如何从这个 class 访问 db
容器?
table.php
中的用法为:
<?php
use Slim\Http\Request;
use Slim\Http\Response;
$app->get('/table/get_table', function (Request $request, Response $response, array $args)
{
$tableUtility = new TableUtility();
return $response->withJson($tableUtility->GetTableFromDb());
});
其实我进了 TableUtility
:
Call to a member function prepare() on null
您所说的 container
的全称是 Dependency Injection Container
。它应该包含对象的依赖关系。将此容器传递给对象被认为是不好的做法。相反,您应该只传递该对象所需的依赖项,在您的情况下是将 db
传递给 $tableUtility
。这通常通过在构造对象时传递依赖项或使用 setter 方法来使用。在您的情况下,您可以像这样重构代码:
class TableUtility
{
function __construct($db) {
$this->db = $db;
}
}
现在,在 TableUtility
class 的任何方法中,您都可以使用 $this->db
访问 db 对象,但是您需要将 db 传递给 class 构造函数创建一个新对象。所以你还需要这样做:
$app->get('/table/get_table', function (Request $request, Response $response, array $args)
{
$tableUtility = new TableUtility($this->db);
// rest of the code
});
假设在我的 dependencies.php
文件中我设置了这个容器:
<?php
$container = $app->getContainer();
$container['db'] = function($config)
{
$db = $config['settings']['db'];
$pdo = new PDO("mysql:host=" . $db['host'] . ";port=" . $db['port'] .
";dbname=" . $db['dbname'] . ";charset=" . $db['charset'], $db['user'], $db['pass']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $pdo;
};
我可以在我的 route
中使用这个 container
,例如 table.php
:
<?php
use Slim\Http\Request;
use Slim\Http\Response;
$app->get('/table/get_table', function (Request $request, Response $response, array $args)
{
$sql = $this->db->prepare("SELECT * FROM some_table");
$sql->execute();
$result = $sql->fetchAll();
return $response->withJson($result);
});
这基本上是默认用法,对吧?也就是说,我怎样才能使用其他 classes 的 db
容器?假设我创建了一个名为 TableUtility
的 class 并导入到 table.php
:
class TableUtility
{
function GetTableFromDb()
{
$sql = $this->db->prepare("SELECT * FROM some_table");
$sql->execute();
$result = $sql->fetchAll();
return $response->withJson($result);
}
}
如您所见,我将 PDO
的逻辑移到了 TableUtility
的 GetTableFromDb
中,我如何从这个 class 访问 db
容器?
table.php
中的用法为:
<?php
use Slim\Http\Request;
use Slim\Http\Response;
$app->get('/table/get_table', function (Request $request, Response $response, array $args)
{
$tableUtility = new TableUtility();
return $response->withJson($tableUtility->GetTableFromDb());
});
其实我进了 TableUtility
:
Call to a member function prepare() on null
您所说的 container
的全称是 Dependency Injection Container
。它应该包含对象的依赖关系。将此容器传递给对象被认为是不好的做法。相反,您应该只传递该对象所需的依赖项,在您的情况下是将 db
传递给 $tableUtility
。这通常通过在构造对象时传递依赖项或使用 setter 方法来使用。在您的情况下,您可以像这样重构代码:
class TableUtility
{
function __construct($db) {
$this->db = $db;
}
}
现在,在 TableUtility
class 的任何方法中,您都可以使用 $this->db
访问 db 对象,但是您需要将 db 传递给 class 构造函数创建一个新对象。所以你还需要这样做:
$app->get('/table/get_table', function (Request $request, Response $response, array $args)
{
$tableUtility = new TableUtility($this->db);
// rest of the code
});