dongou3286 2015-04-07 11:57
浏览 256
已采纳

获得异常:在Phalcon的依赖注入容器中找不到服务'db'

Even after setting "db" in services.php I'm getting above exception.

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

I'm trying to fetch users from my table like this,

$user = Users::findFirst(1); 

That time, it is giving

    Service 'db' wasn't found in the dependency injection container in Phalcon

:::::::::::::::::::::::::::::::::::Update::::::::::::::::::::::::::::::::

Here I'm loading my services.php file.

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

++++++++++++++++++++++++++++ Update 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();
    });
  • 写回答

2条回答 默认 最新

  • donglu9743 2015-09-22 23:37
    关注

    I encountered this issue before and the fix was the following:
    I had two instances of:
    $di = new FactoryDefault();
    I had one in my index.php and one in my services.php file.
    I removed the one in my index.php file, and it got rid of the error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?