Marian has brought it to the point. Simply create a function that does
the same thing over and over.
I have created an abstraction of the controller which I extend for all
controllers and in that have a function that goes a bit like this. No
guarantees as it's stripped down from a slightly more involved
version.
public function getRecordFromRequest($modelName, $parameterName = 'id')
{
$value = $this->_request->getParam($parameterName);
$model = new $modelName();
return $model->find($value)->current();
}
Now if your model is 'User' and the id value is given as a request
parameter called 'id' you call it:
$user = $this->getRecordFromRequest('User');
And there you do whatever is individual in each controller.
Again, no guarantee on the code as I just wrote this from memory, no
checking or anything. So udnerstand what it does and don't just copy
paste. Also this asumes that you use Zend_Db_Table as the model in
this instance.
Hope this helps you.
On Sat, Oct 30, 2010 at 9:50 AM, Marian Meres wrote:
> If the duplication is isolated to that particular controller only, why
> not just simply create a "no action" method to be called in those two
> actions... the controller is still just a regular class.
>
> m.
>
> On Sat, Oct 30, 2010 at 12:41 AM, Hector Virgen wrote:
>> You can probably place that common code in your preDispatch() hook:
>>
>> if ($article = $this->_request->getParam('article')) {
>> // Load article and assign to view
>> }
>>
>> --
>> *Hector Virgen*
>> Sr. Web Developer
>> http://www.virgentech.com
>>
>>
>>
>> On Fri, Oct 29, 2010 at 3:32 PM, debussy007 wrote:
>>
>>>
>>> Hi,
>>>
>>> That's a recurring problem I have had for a long time :
>>>
>>> Let's say I have an action allowing to view an article, and an action
>>> allowing to edit an article.
>>>
>>> To setup the views, the code is the same for both actions (get the article
>>> id from GET parameter, fetch article, execute some checks, and other
>>> things). Only the view files are different, in the edit version, there is a
>>> form with input elements.
>>>
>>> I have here duplicated code. How would you handle this ?
>>>
>>> Thank you for any advice
>>> --
>>> View this message in context:
>>> http://zend-framework-community.634137.n4.nabble.com/MVC-design-question-tp3019840p3019840.html
>>> Sent from the Zend Framework mailing list archive at Nabble.com.
>>>
>>
>
--
Greetings,
Christian Riesen
http://christianriesen.com/ - My personal page
http://toreas.com/ - Toreas a free fantasy novel
http://gamewiki.net/ - Open Videogames Wiki