开发者

Zend Framework - How to modify a single parameter from the request object

开发者 https://www.devze.com 2023-01-16 13:39 出处:网络
A submitted form on my site returns an array of request data that is accessible with $data = $this->getRequest();

A submitted form on my site returns an array of request data that is accessible with

$data = $this->getRequest();

This happens in the controller which gathers the data and then pa开发者_如何转开发sses this array to my model for placing/updating into the database.

Here is the problem. What if I want to manipulate one of the values in that array? Previously I would extract each value, assigning them to a new array, so I could work on the one I needed. Like so:

$data = $this->getRequest();
$foo['site_id'] = $data->getParam('site_id');
$foo['story'] = htmlentities($data->getParam('story'));

and then I would pass the $foo array to the model for placing/updating into the database.

All I am doing is manipulating that one value (the 'story' param) so it seems like a waste to extract each one and reassign it just so I can do this. Additionally it is less flexible as I have to explicitly access each value by name. It's nicer to just pass the whole request to the model and then go through getting rid of anything not needed for the database.

How would you do this?


Edit again: Looking some more at your question what I am talking about here all goes on in the controller. Where your form`s action will land.

Well you have a couple of options.

First of all $_GET is still there in ZF so you could just access it.

Second there is:

$myArray = $this->_request->getParams();

or

$myArray = $this->getRequest()->getParams();

Wich would return all the params in an array instead of one by one.

Thirdly if the form is posted you have:

$myArray = $this->_request()->getPost();

Wich works with $this->_request->isPost() wich returns true if some form was posted.

About accessing all that in your view you could always just in controller:

$this->view->myArray = $this->_request->getParams();

edit: right I taught you meant the view not the model. I guess I do not understand that part of the question.

If you want to deal with the post data inside your model just:

$MyModel = new Model_Mymodels();

$data = $this->_request->getParams();
$data['story'] = htmlentities($data['story']);
$myModels->SetItAll($data);

And then inside your model you create the SetItAll() function (with a better name) and deal with it there.

Edit: oh wait! I get it. You hate sanytising your input one by one with your technique. Well then what I showed you about how to access that data should simplify your life a lot.

edit: There is always the Zend_Form route if the parameters are really coming from a form. You could create code to interface it with your model and abstract all this from the controller. But at the end of the day if you need to do something special to one of your inputs then you have to code it somewhere.

0

精彩评论

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

关注公众号