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

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解