views:

53

answers:

2

In a classic Zend Framework MVC setup, there seems to be access to a generic _request object from within the model/view/controller instance as outlined here:

$this->_request->getPost('variablename');

is this request object somehow available in a non-MVC setup as well?

If yes: how would I initialize and access it?

+1  A: 

The front controller initalizes the request and response objects and passes them to the router. The router calls the requested action, and (usually) appends the returned content to the response. So these objects are available in the Controller layer from the MVC.

Edit:
The frontController uses the following code to set up the request:

$request = new Zend_Controller_Request_Http();

Using the request object this way is very straighforward, so using it outside from the Zend Controller or the Application module shouldn't be a problem. Zend_Controller_Request_Http uses Zend_Uri.

erenon
but what if I'm not employing the MVC pattern at all? Is there a way I can take advantage of the `_request` object somehow? It's not really a big deal, but I'm using ZF as a component library anyway in the project, so it would be nice to switch, maybe in conjuction with the `Zend_Filter_Input` functions.
Pekka
@Pekka: check the edited answer.
erenon
+2  A: 

I would not use the Zend_Controller_Request_* objects outside ZF's MVC structure (even if possible). You can see by the class name already that it belongs to the controller package and it's API exposes a number of methods that are completely targeted at being used within ZF's MVC and these are inherited by all subtypes.

That's okay when using ZF's MVC, but for usage outside of that the Request Objects do too much. The idea of a Request object is to encapsulate and maybe decouple the current Request environment from the Superglobals (which ZF doesn't. It just sits on top). All that extra stuff about modules, controllers and actions shouldn't be part of it in my opinion. It makes only sense in ZF.

Writing a Request object isn't difficult, so I'm sure you can easily come up with a more lightweight version. Here is some basic examples:

In the end it's your choice though.

Gordon
I had the same uneasy feeling when reading `Controller_`. I will most likely roll my own, or use one of the one you link to - they look very clean and nice. Thanks!
Pekka
(Would +1 but out of votes for today.)
Pekka