0

Here is my app structure:

/application
/config
/library
  /Foo
    /Controler.php
/module
  /User
    /config
    /src
      /Bar
        /Controler
          /BarController.php
/public
/vendor
/init_autoloader.php

The Controler.php file...

namespace Foo_Controller;

use Zend\Mvc\Controller\AbstractRestfulController;

class Foo_Controller extends AbstractRestfulController {
  protected $foo;

  public function getFoo()
  {
    return "foooo";
  }

  function __construct() 
  {
    parent::__construct();
    $foo = $this->getFoo();
  }
}

The BarController.php...

namespace Bar\Controler;

use Zend\Mvc\Controller\AbstractRestfulController;
use Foo_Controller\Foo_Controller;
use Zend\View\Model\JsonModel;

class BarController extends Foo_Controller {
.
..
....
}

Added the path /library folder in the init_autoloader.php

  $loader = include 'vendor/autoload.php';
  $zf2Path = 'vendor/zendframework/zendframework/library';
  $loader->add('Zend', $zf2Path);
  $loader->add('Julia', 'library'); // added the library folder

  if (!class_exists('Zend\Loader\AutoloaderFactory')) {
    throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or     define a ZF2_PATH environment variable.');
}

I get an error 500 with the following:
PHP Fatal error: Class 'Foo_Controller\Foo_Controller' not found in /application/module/Bar/src/Bar/Controller/BarController.php on line #

I really don't know what to do now. I have been searching the internet for some time now for the correct way to extend a controller class in Zend Frazmework 2, but i can't seem to grasp it!

What am i doing wrong in the app? Thank you

1 Answer 1

2

I would suggest you set this out slightly differently. Would really make more sense to create your own custom module which you can load into any project with directory structure like:

/zf2-MyCustomModule /src /MyCustomModule /Controller /Abstract /MyAbstractController.php

namespace for MyAbstractController.php would be - MyCustomModule\Controller\Abstract

If it is specific to the project then why not just add

/Abstract /MyAbstractController.php

to the User Module Controller dir.

but seems like what you have done is pretty much right you would just need to update namespace in Foo_Controller.php to:

namespace Julia\Foo\Controller;

not

namespace Foo_Controller;

Though I have never used the method you are using so am not 100% sure.

I would add a new local config to /config/autoload/

like /config/autoload/namespaces.global.php

Zend\Loader\AutoloaderFactory::factory(array(
     'Zend\Loader\StandardAutoloader' => array(
         'namespaces' => array(
             'Julia' => __DIR__ . '/Library',
         ),
     )
));

then your namespace should still be Julia\Foo\Controller;

Sign up to request clarification or add additional context in comments.

1 Comment

Yea changing the namespace should work. I've extended controllers in ZF2 with no issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.