views:

69

answers:

3

Hello Stackoverflow,

I am quite a new Zend Framework user but I am not new to PHP or Programming. I have been going through the code to try and get to grips with how everything slots together. One of the parts I have been looking at is how classes are Autoloaded in the system. I can see that:

  • Zend_Application_Bootstrap references Zend/Application/Bootstrap.php
  • Zend_Controller_Action references Zend/Controller/Action.php
  • ... etc etc

Essentially _ get converted to the directory sepearator and get autoloaded.

What I don't understand (although im sure there is a good reason) is why this convention isn't followed for action controllers?

  • IndexController references Application/controllers/IndexController.php

Why not:

  • Application_Controllers_Index -> /Application/Controllers/Index.php
  • or
  • Controllers_Index -> /Application/Controllers/Index.php

?

I am guessing giving the class a suffix reduces some complexity in the system somewhere, from first looks at the zend framework everything is very well thought out - I cannot imagine conventions are introduced without good reason.

Can anyone explain why controllers are prefixed, or even better point me to some code in the core showing why they have to be prefixed?

Thanks :)

A: 

File names and classes names of Controllers should has a postfix Controller. Zend see a postfix in class name, and understand, that it is a controller. He finds this class in controllers folder.

Alexander.Plutov
Surely it is possible to tell if a controller is a controller because it is in the controllers directory?
mike
A: 

I'm only fairly new to Zend Framework myself, but from what I can tell, given the way the Zend Framework currently stands there is no good reason for this. I'm not sure if it's a hangover from the way things started out, or just the preference of people involved, but I've heard this will be changed for ZF 2.0.

Cags
+4  A: 

To be honest, I'm not entirely certain as to why the conventions were originally developed. I suspect they had to do with how other frameworks were evolving at the time -- in particular, RoR was spiking in popularity, and this was how they defined application resources. (At the time of the original MVC iteration, I was just starting at Zend; I did a rewrite in fall of 2006, but the goal of that was to keep it as consistent with what had been developed previously, while simultaneously offering better and more flexibility.)

We've continued along the paragigm as it reinforces the idea that all items under the application/ hierarchy are resources, and not your library code. This is particularly important due to the fact that you may have non-class code within this tree (view scripts, layouts, etc.).

However, this has certainly made for a few headaches -- the introduction of the resource loader shows that we had a problem we needed to resolve. The resource loader basically addresses the symptoms, but not necessarily the root cause (poor conventions). As we work on ZF2, this is something we will be revisiting. If you're interested in posting your thoughts, I invite you to do so on the zf-contributors mailing list.

weierophinney
Hi Matthew, many thanks for this answer -- I was itching to find a technical reason as to the convention. Glad this is something you are looking into on ZF2 and I will definitely post some thoughts on the mailing list.
mike