简体   繁体   中英

Zend Framework 2 custom library in my controller

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

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();
    }
}

Looks like your path is wrong.

If init_autoloader.php is in /home/mydir/ and the library is in /home/mydir/vendor/My/library/ the correct value to specify in the autoloader namespaces array is

'My' => __DIR__ . '/vendor/My/library/My'

If you handle it in the Module::getAutoloaderConfig() method instead (which you should, by the way), just prepend the correct sequence of /.. to the path string, after the constant. Your mileage may vary depending on where your Module.php actually is.

Furthermore, contrary to what was stated in the other answer, you can indeed import a whole package with use My\\Helper and then use its classes prepending just the Helper\\ prefix.

create a file autoload_classmap.php in your Application Module or any module and add in it:

<?php
return array(
    'FF' => 'vendor/My/library/My/Helper/ff.php', 
);

then you can use it anywhere in your modules like \\FF::test()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM