0

I'm migrating application from ZF1 to ZF2. I have a controller depends on third party lib 'Solarium'.

namespace Stock\Controller;
class BaseController extends AbstractActionController
{
    protected function indexAction()
    {
        require_once('Solarium/Autoloader.php');
        Solarium_Autoloader::register();

The 'Solarium' exists under 'vendor', and in 'init_autoloader.php' I have:

set_include_path(implode(PATH_SEPARATOR, array(
    realpath('vendor')
)));

But, when I viewing the page, there's an error:

Fatal error: Class 'Stock\Controller\Solarium_Autoloader' not found in ...

I tried to add trace in 'StandardAutoloader.php' and found StandardAutoloader.autoload('Stock\Controller\Solarium_Autoloader') is called on runtime.

I want to know how this happens and how to fix it. Thanks.

user1633272
  • 2,007
  • 5
  • 25
  • 48
  • possible duplicate of http://stackoverflow.com/questions/1566879/adding-3rd-party-library-to-zendframework – Stoyan Dimov Apr 14 '13 at 15:03
  • Why don't you use composer? http://wiki.solarium-project.org/index.php/V3:Installation#Getting_Solarium – Aydin Hassan Apr 14 '13 at 15:06
  • Zend Framework 2 is very different with ZF1. So this thread is new problem. – user1633272 Apr 14 '13 at 15:57
  • I just enabled solarium in composer, and changed code to "$client = new Solarium\Client($config);". But there is still the similar error: "Fatal error: Class 'Stock\Controller\Solarium\Client' not found in". – user1633272 Apr 14 '13 at 16:15
  • @user1633272 You are missing a backslash. Try this: `$client = new \Solarium\Client($config);`. – ba0708 Apr 14 '13 at 16:21

1 Answers1

3

As Aydin Hassan wrote in his comment, the easiest way to make this work is by using Composer. First, edit your composer.json file within your project's root directory to look something like this:

"require": {
    "php": ">=5.3.3",
    "zendframework/zendframework": "2.*",
    "solarium/solarium": ">=2.4.0"
}

If you are using the Zend Skeleton Application, then you will also have Composer itself in your project's root directory (composer.phar). In that case, you can do like this:

cd /path/to/project && php composer.phar install solarium/solarium

Or

cd /path/to/project && php composer.phar install

Otherwise just have Composer available everywhere in your command line. By doing like above, Composer will take care of autoloading for you. Within your controller, you should then not worry about including the file as this happens automatically for you with spl_autoload_register. You just have to use the namespace. You can use either of these two approaches:

namespace Stock\Controller;

use Solarium\Autoloader;

class BaseController extends AbstractActionController
{
    protected function indexAction()
    {
        Autoloader::register();
    }
}

Or

namespace Stock\Controller;

class BaseController extends AbstractActionController
{
    protected function indexAction()
    {
        \Solarium\Autoloader::register();
    }
}
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
ba0708
  • 10,180
  • 13
  • 67
  • 99
  • The first approach you mentioned above doesn't work on my environment. Error is "Fatal error: Class 'Stock\Controller\Autoloader' not found in...". The second approach works fine. – user1633272 Apr 15 '13 at 12:20
  • @user1633272 Sorry about that. I cannot remember exactly how to go about it that way as I usually go with the last mentioned approach. Perhaps try `use Solarium\Autoloader` instead. – ba0708 Apr 15 '13 at 15:38