Phalcon登录失败(GetDI)

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (309)

我在IIS上使用PHP,Phalcon框架。我有一个登录控制器,我正在使用,但我似乎无法让它工作。

我有一个表单动作张贴到signin/doSignin。这是SigninController.php的一个片段:

public function doSigninAction(){
    //$this->view->disable();
    $user = User::findFirst([
          'conditions' => 'email = :email: AND password = :password:'
        , 'bind' => [
              'email' => $this->request->getPost('email')
            , 'password' => $this->request->getPost('password')
        ]
    ]);
    if ($user){
        echo 1;
        return;
    }
    echo 2;

当我运行这段代码时,结果是一个空白页,它只报告:

Call to undefined method or service 'getDI'

某些地方没有为Phalcon的内部结构做好适当的排列,但我不知道我需要检查什么。当我将上述代码更改为此时,将得到视图的适当呈现,为用户打印1:

    $user = new User()/*::findFirst([
          'conditions' => 'email = :email: AND password = :password:'
        , 'bind' => [
              'email' => $this->request->getPost('email')
            , 'password' => $this->request->getPost('password')
        ]
    ]);*/;
    if ($user){
        echo 1;
        return;
    }
    echo 2;

我的引导程序如下:

<?php
try {
set_include_path('c:/workspace/GIIAnalytics/app/views');

//Autoloader
$loader = new \Phalcon\Loader();
$loader->registerDirs([
      '../app/controllers/'
    , '../app/models/'
    , '../app/config/'
]);
/** For MSSQL connections */
$loader->registerNamespaces([
      "Twm\Db\Adapter\Pdo"  =>  "../app/library/db/adapter/"
    , "Twm\Db\Dialect"      =>  "../app/library/db/dialect/"
]);
$loader->register();

//Dependancy Injection
$di = new \Phalcon\DI\FactoryDefault();

//Config
$configFile = __DIR__ . '/../app/config/config.json';
$config = json_decode ( file_get_contents ( $configFile ) );
$di->setShared('config',$config);

//MSSQL Database connection
$di->set("db", function() use ($di) {
    //Database info
    /** For MSSQL connections */
    $mc = $di->getDI()->getShared('config')['db'];
    $db = new Twm\Db\Adapter\Pdo\Mssql($mc);
    return $db;
});

//View
$di->set('view', function(){
    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir('../app/views');
    /*
    $view->registerEngines([
        '.volt' => '\Phalcon\Mvc\View\Engine\Volt'
    ]);
    */
    return $view;
});

// Router
$di->set('router',function(){
    $router = new \Phalcon\Mvc\Router();
    $router->mount(new Routes());
    return $router;
});

// Session
$di->setShared('session', function() {
    $session = new \Phalcon\Session\Adapter\Files();
    $session->start();
    return $session;
});

//Flash Data (temp data)
$di->set('flash', function() {
    $flash = new \Phalcon\Flash\Session([
          'error'   =>  'alert alert-danger'
        , 'success' =>  'alert alert-success'
        , 'notice'  =>  'alert alert-info'
        , 'warning' =>  'alert alert-warning'
    ]);
    return $flash;
});

//Metadata
$di['modelsMetadata'] = function() {
    $metaData = new \Phalcon\Mvc\Model\MetaData\Memory([
          'lifetime' => 86400
        , 'prefix' => 'metaData'
    ]);
    return $metaData;
};

// custom dispatcher overrides the default
$di->set('dispatcher', function() use ($di) {
    $eventsManager = $di->getShared('eventsManager');

    // Custom ACL class
    $permission = new Permission();

    // Listen for events from the $permission class
    $eventsManager->attach('dispatch', $permission);

    $dispatcher = new \Phalcon\Mvc\Dispatcher();
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
});

//Deploy the app
$app = new \Phalcon\Mvc\Application($di);
echo $app->handle()->getContent();

} catch(\Phalcon\Exception $e) {
    echo $e->getmessage();
}
?>

如何解决依赖问题?

提问于
用户回答回答于

发现了问题。Phalcon不适合为无效函数调用提供行。它位于我的db构造函数中,在这里我正在执行$di->getDI,它(连同配置读取器)被更改为这个代码段:

//MSSQL Database connection
$di->set("db", function() use ($di) {
    //Database info
    /** For MSSQL connections */
    $mc = (array) $di->getShared('config')->db;
    $db = new Twm\Db\Adapter\Pdo\Mssql($mc);
    return $db;
});

扫码关注云+社区

领取腾讯云代金券