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

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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵