0

I'm trying to integrate PHP namespaces into an existing Zend Framework project (v1.12). When I add namespacing at the top of a working controller, it doesn't work anymore and the application throws an Invalid controller class error. Here's my controller definition :

namespace MyProject\Controller;

use MyProject\Controller\MyRestController;

class MyFooController extends MyRestController
{
}

and the init method within the Bootstrap.php:

protected function _initAutoload()
{
    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->registerNamespace('MyProject');
    return $autoloader;
}

2 Answers 2

0

Just a guess (have not used ZF for quite some time): Zend will not accept any class as a controller, just those extended from the framework's base controller class. As you don't extend from the frameworks base controller class you see the error.

If that is the reason, take care you initially extended from the base framework controller class or you implemented the needed interface.

namespace MyProject\Controller;

class MyRestController extendes Zend_Framework_Base_Controller_Class_Name_Here
{
    ...

p.s. the use MyProject\Controller\MyRestController; looks superfluous as that class is in that namespace already. Let's review your code:

namespace MyProject\Controller;

This sets the namespace of the file. That means, non-FQCN will resolve into it. For example:

new MyRestController();

Resolves to the following FQCN:

new MyProject\Controller\MyRestController

Which - oha! - is exactly what you wrote in use:

use MyProject\Controller\MyRestController;

Which means, that this use clause is superfluous, the extend in:

class MyFooController extends MyRestController

Would go to it anyway at first. Because it's the same namespace.

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

9 Comments

The MyRestController (an abstract class) actually extends \Zend_Rest_Controller. @hakre, as for your PS, I don't get it when you say it looks superfluous.
I extended that part. However contrary to your believ, Zend is sure it doesn't. So probably you have a problem to discuss with your zend framework and argue it into accepting that class ;) - Go in there with a debugger and take a look why the exception is thrown anyway. I suggest the use of Xdebug. That normally gives you the cause nearly in the minute.
@davidloubere you found out what was the cause?
I think Zend is unable to load MyFooController because it doesn't directly extends \Zend_Rest_Controller but firstly extends MyRestController.
@davidloubere: Are you sure? I would double check that because that should not be an issue. Or asked the other way round: What happens if you directly extend? (If that is your issue, you really should report that as a bug because it's a heavy flaw).
|
0

I am facing similar problem now. For me this looks like that Zend cannot properly resolve namespaced controller name. So when I put for example IndexController into namespace \Basic\Controller, it will be not loaded because Zend want to load \IndexController class, which does not exist.

I am thinking about extending standard zend router class, which has method getControllerName.

Then I can set this in bootstrap by:

$router = new \My\Namespaced\Router();

$front = Zend_Controller_Front::getInstance();
$front->setRouter($router);

I didn't tried that code yet but this should work.

Comments

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.