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;
}
hakre
  • 193,403
  • 52
  • 435
  • 836
David
  • 2,603
  • 4
  • 18
  • 28

2 Answers2

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • 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. – David Dec 24 '12 at 19:35
  • 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. – hakre Dec 24 '12 at 19:40
  • @davidloubere you found out what was the cause? – hakre Dec 24 '12 at 20:27
  • I think Zend is unable to load `MyFooController` because it doesn't directly extends `\Zend_Rest_Controller` but firstly extends `MyRestController`. – David Dec 24 '12 at 21:24
  • @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). – hakre Dec 24 '12 at 21:25
  • I've just tried two things: (1) a single `TestController` extending `Zend_Rest_Controller` with no namespace => OK (2) the same test controller extending `\Zend_Rest_Controller` with `namespace MyProject\Controller;` on top => `Invalid controller class ("TestController")` – David Dec 24 '12 at 21:52
  • Yes, because the controller class actually is named `MyProject\Controller\TestController` and *not* just `TestController`. You need to check how you can tell Zend Framework what your real controller classname is. Check how you can register the namespace / prefix. Maybe this is helpful: [Using PHP namespaces in a Zend Framework (v1) application](http://stackoverflow.com/questions/6513529/using-php-namespaces-in-a-zend-framework-v1-application) – hakre Dec 24 '12 at 21:55
  • That's the answer, thank you. So, in fact, ZF1 does not support autoloading PHP namespaces for application classes and one has to tweak around as said in the link mentioned above. – David Dec 24 '12 at 22:20
  • Ah cool, I was not entirely sure, but it smelled that it could fit. Great to hear this really helped you. – hakre Dec 24 '12 at 22:24
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.

Łukasz
  • 1
  • 1
  • 1