doulu1867 2013-06-05 15:27
浏览 17
已采纳

我的控制器中的Zend Framework 2自定义库

I'm using ZendSkeletonApplication and I have this directory :

/home/mydir/vendor/My/library/My/Helper

which contains ff.php (respectively, /home/mydir/vendor/My/library/My/Helper/ff.php)

My code in ff.php :

<?php

namespace My\Helper;

class FF {
    public function test() {
        echo "success !";
    }
}

And my controller :

<?php
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

use My\Helper\FF;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        FF::test();
        return new ViewModel();
    }
}

My module.php :

public function getAutoloaderConfig()
{
    return array(
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                'My' => __DIR__ . '/../vendor/My/library/My'
            ),
        ),
    );
}

And I get this error :Fatal error: Class 'My\Helper\FF' not found in /home/mydir/module/Application/src/Application/Controller/IndexController.php on line 13

How resolve ?

Thank you

  • 写回答

3条回答 默认 最新

  • dousong4777 2013-06-05 15:35
    关注

    You should do like this in your module.php or init_autoloader.php

           'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                    'My' => __DIR__ . '/../../vendor/My/library/My',
                ),
            ),
    

    And that will do the job :)

    EDIT : And in your controller

    <?php
    namespace Application\Controller;
    
    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\View\Model\ViewModel;
    
    use My\Helper\FF;
    
    class IndexController extends AbstractActionController
    {
        public function indexAction()
        {
            FF::test();
            return new ViewModel();
        }
    }
    

    or

    <?php
    namespace Application\Controller;
    
    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\View\Model\ViewModel;
    
    class IndexController extends AbstractActionController
    {
        public function indexAction()
        {
            \My\Helper\FF::test();
            return new ViewModel();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号