开发者

doctrine2 zend framework namespaceing controllers

I'm trying to integrate the doctrine2 sandbox with a default Zend Framework App. When I try to use namespacing in the controller I get an 'Invalid controller class ("IndexController")' error

This Works:

use Entities\User, Entities\Address;


class IndexController extends Zend_Controller_Action
{
...
}

This does not (but should?):

namespace Entities;


class IndexController exten开发者_高级运维ds \Zend_Controller_Action
{
...
}


In your first example, you are importing namespaces into the controller. In your second example, you are assigning the controller to a namespace.

Importing a namespaces allows you to reference classes without having to user their fully qualified class name.

new \Entities\User() // without import
new User() // with import

Assigning a namespace to your controller actually changes the fully-qualified name for your class.

new \IndexController() // without namespace
new \Entities\IndexController() // with namespace

(Classes inside a namespace can always reference other classes in that same namespace without having to 'use' it. I suspect that was the primary reason you were trying to use option 2).

Zend Framework 1.10 is still namespace ignorant. When parsing a URL and trying to load a controller, it will look only look in the default global namespace for \IndexController, and have no idea that it's been assigned to a user defined namespace (\Entities\IndexController).

My recommendation is that when working with controllers in ZF, don't assign namespaces to them. Importing works fine. We'll have to wait until ZF 2.0 for full namespace support.


After going thru the manual and reading some of this page it would seem that in PHP when you want to declare and construct around a namespace you use your second syntax. So that would create objects like

Entities\IndexController 

so its not found anymore by Zend.

According to those site you have to use use to import a namespace and use it.

Thats why it works in your first example and not in your second one.

Hope I am right and this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜