出现异常:在 Phalcon 的依赖注入容器中找不到服务 'db'
getting exception : Service 'db' wasn't found in the dependency injection container in Phalcon
即使在 services.php 中设置 "db" 之后,我也能正常工作。
services.php
$di->set('db', function() use ($config) {
$dbclass = 'Phalcon\Db\Adapter\Pdo\' . $config->database->adapter;
return new $dbclass(array(
"host" => $config->database->host,
"username" => $config->database->username,
"password" => $config->database->password,
"dbname" => $config->database->name
));
});
我正在尝试像这样从我的 table 中获取用户,
$user = Users::findFirst(1);
那个时候,就是给
Service 'db' wasn't found in the dependency injection container in Phalcon
::::::::::::::::::::::::::::::::::::更新:::::::::: ::::::::::::::::::::::::
我正在加载我的 services.php 文件。
public/index.php
error_reporting(E_ALL);
use Phalcon\Mvc\Application;
use Phalcon\Config\Adapter\Ini as ConfigIni;
try {
define('APP_PATH', realpath('..') . '/');
/**
* Read the configuration
*/
$config = new ConfigIni(APP_PATH . 'app/config/config.ini');
require APP_PATH . 'app/config/loader.php';
require APP_PATH . 'app/config/services.php';
$application = new Application($di);
echo $application->handle()->getContent();
} catch (Exception $e){
echo $e->getMessage();
}
++++++++++++++++++++++++++++++更新2 +++++++++++++++ ++++++++++++++++++++++
loader.php
$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerDirs(
array(
APP_PATH . $config->application->controllersDir,
APP_PATH . $config->application->pluginsDir,
APP_PATH . $config->application->libraryDir,
APP_PATH . $config->application->modelsDir,
APP_PATH . $config->application->formsDir,
)
)->register();
services.php
use Phalcon\Mvc\View;
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaData;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Flash\Session as FlashSession;
use Phalcon\Events\Manager as EventsManager;
/**
* The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
*/
$di = new FactoryDefault();
/**
* We register the events manager
*/
$di->set('dispatcher', function() use ($di) {
$dispatcher = new Phalcon\Mvc\Dispatcher();
return $dispatcher;
});
/**
* The URL component is used to generate all kind of urls in the application
*/
$di->set('url', function() use ($config){
$url = new UrlProvider();
$url->setBaseUri($config->application->baseUri);
return $url;
});
$di->set('view', function() use ($config) {
$view = new View();
$view->setViewsDir(APP_PATH . $config->application->viewsDir);
$view->registerEngines(array(
".volt" => 'volt'
));
return $view;
});
/**
* Setting up volt
*/
$di->set('volt', function($view, $di) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array(
"compiledPath" => APP_PATH . "cache/volt/"
));
$compiler = $volt->getCompiler();
$compiler->addFunction('is_a', 'is_a');
return $volt;
}, true);
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->set('db', function() use ($config) {
$dbclass = 'Phalcon\Db\Adapter\Pdo\' . $config->database->adapter;
return new $dbclass(array(
"host" => $config->database->host,
"username" => $config->database->username,
"password" => $config->database->password,
"dbname" => $config->database->name
));
});
/**
* If the configuration specify the use of metadata adapter use it or use memory otherwise
*/
$di->set('modelsMetadata', function() {
return new MetaData();
});
/**
* Start the session the first time some component request the session service
*/
$di->set('session', function() {
$session = new SessionAdapter();
$session->start();
return $session;
});
/**
* Register the flash service with custom CSS classes
*/
$di->set('flash', function(){
return new FlashSession(array(
'error' => 'alert alert-danger',
'success' => 'alert alert-success',
'notice' => 'alert alert-info',
));
});
/**
* Register a user component
*/
$di->set('elements', function(){
return new Elements();
});
我之前遇到过这个问题,解决方法如下:
我有两个实例:
$di = new FactoryDefault();
我的 index.php 中有一个,services.php 文件中有一个。
我删除了 index.php 文件中的那个,它消除了错误。
更改对我有效的所有 $di 调用的顺序
我在 Phalcon CLI 应用程序中遇到了同样的错误。我通过将 DbAdapter 的命名空间导入到 cli.php
来解决这个问题
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
并添加以下代码
$di['db'] = function() {
return new DbAdapter(array(
"host" => YOUR_HOST,
"username" => YOUR_USERNAME,
"password" => YOUR_PASSWORD,
"dbname" => YOUR_DB_NAME
));
};
之前
$console->setDI($di);
即使在 services.php 中设置 "db" 之后,我也能正常工作。
services.php
$di->set('db', function() use ($config) {
$dbclass = 'Phalcon\Db\Adapter\Pdo\' . $config->database->adapter;
return new $dbclass(array(
"host" => $config->database->host,
"username" => $config->database->username,
"password" => $config->database->password,
"dbname" => $config->database->name
));
});
我正在尝试像这样从我的 table 中获取用户,
$user = Users::findFirst(1);
那个时候,就是给
Service 'db' wasn't found in the dependency injection container in Phalcon
::::::::::::::::::::::::::::::::::::更新:::::::::: ::::::::::::::::::::::::
我正在加载我的 services.php 文件。
public/index.php
error_reporting(E_ALL);
use Phalcon\Mvc\Application;
use Phalcon\Config\Adapter\Ini as ConfigIni;
try {
define('APP_PATH', realpath('..') . '/');
/**
* Read the configuration
*/
$config = new ConfigIni(APP_PATH . 'app/config/config.ini');
require APP_PATH . 'app/config/loader.php';
require APP_PATH . 'app/config/services.php';
$application = new Application($di);
echo $application->handle()->getContent();
} catch (Exception $e){
echo $e->getMessage();
}
++++++++++++++++++++++++++++++更新2 +++++++++++++++ ++++++++++++++++++++++
loader.php
$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerDirs(
array(
APP_PATH . $config->application->controllersDir,
APP_PATH . $config->application->pluginsDir,
APP_PATH . $config->application->libraryDir,
APP_PATH . $config->application->modelsDir,
APP_PATH . $config->application->formsDir,
)
)->register();
services.php
use Phalcon\Mvc\View;
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaData;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Flash\Session as FlashSession;
use Phalcon\Events\Manager as EventsManager;
/**
* The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
*/
$di = new FactoryDefault();
/**
* We register the events manager
*/
$di->set('dispatcher', function() use ($di) {
$dispatcher = new Phalcon\Mvc\Dispatcher();
return $dispatcher;
});
/**
* The URL component is used to generate all kind of urls in the application
*/
$di->set('url', function() use ($config){
$url = new UrlProvider();
$url->setBaseUri($config->application->baseUri);
return $url;
});
$di->set('view', function() use ($config) {
$view = new View();
$view->setViewsDir(APP_PATH . $config->application->viewsDir);
$view->registerEngines(array(
".volt" => 'volt'
));
return $view;
});
/**
* Setting up volt
*/
$di->set('volt', function($view, $di) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array(
"compiledPath" => APP_PATH . "cache/volt/"
));
$compiler = $volt->getCompiler();
$compiler->addFunction('is_a', 'is_a');
return $volt;
}, true);
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->set('db', function() use ($config) {
$dbclass = 'Phalcon\Db\Adapter\Pdo\' . $config->database->adapter;
return new $dbclass(array(
"host" => $config->database->host,
"username" => $config->database->username,
"password" => $config->database->password,
"dbname" => $config->database->name
));
});
/**
* If the configuration specify the use of metadata adapter use it or use memory otherwise
*/
$di->set('modelsMetadata', function() {
return new MetaData();
});
/**
* Start the session the first time some component request the session service
*/
$di->set('session', function() {
$session = new SessionAdapter();
$session->start();
return $session;
});
/**
* Register the flash service with custom CSS classes
*/
$di->set('flash', function(){
return new FlashSession(array(
'error' => 'alert alert-danger',
'success' => 'alert alert-success',
'notice' => 'alert alert-info',
));
});
/**
* Register a user component
*/
$di->set('elements', function(){
return new Elements();
});
我之前遇到过这个问题,解决方法如下:
我有两个实例:
$di = new FactoryDefault();
我的 index.php 中有一个,services.php 文件中有一个。
我删除了 index.php 文件中的那个,它消除了错误。
更改对我有效的所有 $di 调用的顺序
我在 Phalcon CLI 应用程序中遇到了同样的错误。我通过将 DbAdapter 的命名空间导入到 cli.php
来解决这个问题use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
并添加以下代码
$di['db'] = function() {
return new DbAdapter(array(
"host" => YOUR_HOST,
"username" => YOUR_USERNAME,
"password" => YOUR_PASSWORD,
"dbname" => YOUR_DB_NAME
));
};
之前
$console->setDI($di);