doutongwei4380 2014-04-14 22:21
浏览 212
已采纳

Phalcon自动加载器找不到的类

I'm trying to inject a third-party authentication library into my Phalcon application. The file exists at /lib/Foo_Bar_AuthProvider.php:

<?php

namespace Foo\Bar;

class AuthProvider
{
    public function authenticate()
    {
        return true;
    }
}

I register this directory with the Phalcon autoloader in my bootstrapper, located at /public/index.php, and add it to the DI:

<?php

try {

    //Register an autoloader
    $loader = new \Phalcon\Loader();
    $loader->registerDirs(array(
        '../app/controllers/',
        '../app/models/',
        '../lib/'
    ))->register();

    //Create a DI
    $di = new \Phalcon\DI\FactoryDefault();

    $di->set('authProvider', function() {
        return new \Foo\Bar\AuthProvider();
    });

    // ...
}

Then I try to use this component in /app/controllers/AccountController.php:

<?php

class AccountController extends \Phalcon\Mvc\Controller
{
    public function loginAction()
    {
        if (!$this->request->isPost()) {
            return;
        }

        $success = $this->authProvider->authenticate();

        if (!$success) {
            $this->flash->error('Authentication failed.');
            return;
        }

        $this->flash->success('Authentication succeeded. Welcome!');

        return $this->dispatcher->forward(array('controller' => 'index', 'action' => 'index'));
    }
}

This throws an exception:

Fatal error: Class 'Foo\Bar\AuthProvider' not found in /public/index.php on line 44

I am pretty new at using PHP namespaces so I'm probably missing something obvious, but I haven't been able to figure it out. I tried adding a backslash before the namespace declaration in Foo_Bar_AuthProvider.php like this:

namespace \Foo\Bar;

This didn't change anything. I also tried removing this backslash from the bootstrapper:

$di->set('authProvider', function() {
    return new Foo\Bar\AuthProvider();
});

No dice here either. Finally, I tried adding the use statement to AccountController.php:

use \Foo\Bar;

Also:

use Foo\Bar;

I believe the purpose of the autoloader is to avoid such things, but adding the use statements didn't work anyways.

  • 写回答

1条回答 默认 最新

  • duanquanzhi5560 2014-04-14 23:16
    关注

    Read about the PSR-0 standard. Phalcon applies most of their conventions and others PHP Frameworks as well.

    From my understanding the _ is only meaningful in the class name, elsewhere underscores are literal. For example...

    A class name Foo_Bar_AuthProvider means: in each registered dir search for the Foo/Bar path then check for the AuthProvider.php file. AFAIK, that's useful if you need the file to be under the Foo/Bar path but not necessarily in the Foo/Bar namespace.

    Id recommend you to use the "namespace=path" approach that PSR-0 describes. Try to rename Foo_Bar_AuthProvider.php to AuthProvider.php then put this file into /lib/Foo/Bar. With this done you'll be able to register this lib like this:

    $di->set('authProvider', 'Foo\Bar\AuthProvider');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。
  • ¥15 SQL Server analysis services 服务安装失败