Added by Federico Cargnelutti, last edited by Federico Cargnelutti on Sep 01, 2008  (view change)

Labels

 
(None)

Zend Framework: Zend_Controller_Directory Component Proposal

Proposed Component Name Zend_Controller_Directory
Developer Notes http://framework.zend.com/wiki/display/ZFDEV/Zend_Controller_Directory
Proposers Federico Cargnelutti
Revision 1.0 - 21 August 2008: Initial Proposal. (wiki revision: 43)

Table of Contents

1. Overview

The Zend_Controller_Directory component allows users to define directory names and paths.

Goals

This proposal attempts to solve the following problems:

  1. Zend_Controller_Front violates the single responsibility principle, each responsibility should be a separate class.
  2. Zend_Controller_Front is not polymorphic and therefore makes the system less tolerant to change in requirements.
  3. Zend_Controller_Front provides data to the Zend_Controller_Dispatcher_Standard object creating control coupling.
  4. Extending Zend_Controller_Dispatcher_Abstract violates the DRY principle.
  5. Zend_Controller_Dispatcher_Standard allows components not only to retrieve directory names and paths, but also to dispatch controllers. This includes Action Controllers and Action Helpers.
Example

The Front Controller needs one or more paths to directories containing action controllers in order to dispatch the request:

To add additional directory names and paths, the user has to create a class that extends Zend_Controller_Dispatcher_Abstract and implements Zend_Controller_Dispatcher_Interface:

Extending the Zend_Controller_Dispatcher_Abstract class leads to the following problems:

1. Code Duplication

In order to add additional methods, the user has to duplicate the behaviour and internal structure of the Zend_Controller_Dispatcher_Standard class. This approach violates the DRY principle:

2. API Inconsistency

Because the Front Controller is a singleton and cannot be subclassed, the user is unable to add additional accessor methods to the Front Controller, therefore there's no consistency in the way the data is provided and retrieved from the Dispatcher:

2. References

3. Component Requirements, Constraints, and Acceptance Criteria

  • This component will provide abstracts that facilitate the extension of the base functionality.

4. Dependencies on Other Framework Components

  • Zend_Exception

5. Theory of Operation

Zend Framework organizes code in a project structure and puts the project files into different directory structures:

  1. MVC directory structure
  2. Modular directory structure

The goal of this component is to move this responsibility away from the Front Controller, avoid code duplication, reduce control coupling and improve the consistency of the API.

6. Milestones / Tasks

  • Write proposal
  • Gather feedback
  • Review by the Zend team
  • Develop full implementation and unit tests
  • Documentation

7. Class Index

  • Zend_Controller_Directory_Abstract
  • Zend_Controller_Directory_Exception
  • Zend_Controller_Directory_Standard

8. Use Cases

UC-01 Bootstrapper example

UC-02 Bootstrapper example

9. Class Skeletons

  • Zend_Controller_Directory_Abstract
  • Zend_Controller_Directory_Standard
  • Zend_Controller_Front
  • Zend_Controller_Dispatcher_Abstract
  • Zend_Controller_Dispatcher_Standard
  • Zend_Controller_Action_Helper_ViewRenderer

Can you provide some more information about what problem this proposal is attempting to solve? I've read your blog post, but it's still unclear to me what the exact problem is you perceive, and how this solution then addresses it.

Yes sure. This refactoring proposal attempts to solve the following issues:

  1. Zend_Controller_Front violates the single responsibility principle, each responsibility should be a separate class.
  2. Zend_Controller_Front is not polymorphic and therefore makes the system less tolerant to change in requirements.
  3. Zend_Controller_Front provides data to other objects.
  4. Extending Zend_Controller_Dispatcher_Abstract violates the DRY principle.
  5. Zend_Controller_Dispatcher_Abstract allows components not only to retrieve directory names and paths, but also to dispatch controllers. This includes Action Controllers and Action Helpers.
  6. Zend_Controller_Front acts as a global container and provides access to other objects.

This proposal focuses on issues 1 to 5. Issue 6 can be solved by introducing the Zend_Container component (or Zend_Registry).

Basically, the refactoring I'm proposing solves the problem of having to create a new Dispatcher class every time the user extends the functionality of the system. Also, it prevents components from using the Front Controller, the Dispatcher or both to manage directory names and paths. This way, each class has a single responsibility: The Directory component knows where the classes are located and the Dispatcher which class to dispatch.

Posted by Federico Cargnelutti at Aug 26, 2008 07:56 Updated by Federico Cargnelutti

Hi Matthew, I've just provided more information about the problem this refactoring attempts to solve. Let me know what you think, thanks.

Posted by Federico Cargnelutti at Aug 26, 2008 14:56 Updated by Federico Cargnelutti

I need better explanations of the problems you're solving. FOr instance, "Extending Zend_Controller_Dispatcher_Abstract violates the DRY principle" – how? Or, "Zend_Controller_Front is not polymorphic and therefore makes the system less tolerant to change in requirements" – what changes in requirements would not be supported by Zend_Controller_Front? Or with the first point, which responsibility are you addressing? (I know which one, and it becomes clear in your proposal, but you may want to note if there are others, and explicitly note which particular one you are addressing). Regarding number 6, how is this a problem? (again, spell it out)

As noted on the lists and other proposals, I'm actually in agreement with you on most points. However, this proposal absolutely cannot be implemented prior to 2.0.0, as it presents a very big BC break with the current architecture, even though it improves it. Let's answer the above questions very explicitly so that we can get good community discussion around them.

I agree, this cannot be implemented prior to 2.0. Should I change the parent document of this proposal back to "New"?

In the meantime, I'll keep adding more examples.

If I'm understanding your first example correctly, you're trying to show that if an application wanted to have multiple controller directories (within the same module), the only way to do this is to subclass the dispatcher, which results in a lot of code duplication?

If so, it might be simpler to instead change the standard dispatcher so that it supports multiple controller directories for a module. Calling addControllerDirectory more than once could add the path to an internal array rather than replacing it. E.g.

I'm also a little confused as to why a 'controller directory' class would hold variables specifying the names of model and view directories, surely these are separate things?

Unless I'm going crazy, the blog post you wrote that spawned this proposal originally called this class 'Zend_Module_Front', so it was like a mini front controller for modules. My understanding of it was that it would make it easier for modules to use completely different names for controller/view/model directories and move this level of configuration away from the main front controller. Now that the class has been renamed I don't think it makes sense to use it to control paths to anything except controllers.

Also, whilst I can appreciate the advantages of refactoring, I think we need to be very careful about any change which potentially requires users to add more code to their bootstrap (i.e. having to instantiate a dispatcher as well as the front controller). The bootstrap already seems to be a cause of confusion among new ZF users, so IMHO the MVC components should try and facilitate as lean a bootstrap as possible.

Hi Tim,

It's a complex issue. It's not about controller or view directories, it's about defining responsibilities. I spent more than 3 months analysing individuals components of the Zend_Controller package, and I came to the conclusion that the Dispatcher is doing to much. It not only manages directory names and paths, but also dispatches controllers. The first responsibility can be decoupled from the process. There are several advantages of doing this, for example, improve performance, consistency, etc.

Posted by Federico Cargnelutti at Sep 28, 2008 13:45 Updated by Federico Cargnelutti

In case you are interested in knowing how this proposal can help improve the performance of the framework, here's an example:

When you dispatch the request the front controller throws an exception. If you look at Zend_Controller_Action, line 160, you'll see that it calls the front controller and retrieves a list of directories:

And the front controller calls the Dispatcher:

And that's why, unfortunately, it doesn't work.

Posted by Federico Cargnelutti at Sep 28, 2008 15:38 Updated by Federico Cargnelutti

Really interesting examples, I've been following your proposal for a while. Did you move forward?