views:

60

answers:

2

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 extends \Zend_Controller_Action
{
...
}
A: 

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!

Iznogood
Entities\IndexController returns the following error: syntax error, unexpected T_NS_SEPARATOR, expecting '{'I thought along similar lines and tried \IndexController earlier - but I got the same error
waigani
But why do you want to use the second version^ it doesnt work like that.If you want to import classes you type use myobject. So you want to use something in your controller is that it?
Iznogood
+2  A: 

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.

Bryan M.
Thanks for the breakdown. What I don't understand is why placing a leading backwards slash in front of the controller name produces a syntax error. Both \IndexController and \Entities\IndexController produce the following error: syntax error, unexpected T_NS_SEPARATOR, expecting T_STRING
waigani
At any rate I will take your advice and just import namespaces for now.
waigani
I would add that Zend Framework isn't _completely_ namespace ignorant. It is simply that MVC controllers currently can't be namespaced. The rest of your library code, helpers, forms, etc. can be namespaced.
wilmoore