MVC design question

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

MVC design question

debussy007
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
Reply | Threaded
Open this post in threaded view
|

Re: MVC design question

Hector Virgen
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 <[hidden email]> 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.
>
--
Hector Virgen
Reply | Threaded
Open this post in threaded view
|

Re: MVC design question

Marian Meres-2
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 <[hidden email]> 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 <[hidden email]> 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.
>>
>
Reply | Threaded
Open this post in threaded view
|

Re: MVC design question

Christian Riesen
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|

Re: MVC design question

Jurian Sluiman-4
In reply to this post by Marian Meres-2
On Saturday 30 Oct 2010 09:50:56 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.

For me, it's more clean for a controller specific tasks to put it in init() or
preDispatch(), but that's an opinion.

Otherwise, you can also create an action helper. For example in your
blogModule you have an articleController. Then you can place an action helper
in /modules/blog/plugins/Article.php which is able to fetch an article from
specific parameters and do some checks.

Then you're even capable of fetching an article outside the ArticleController.

Regards, Jurian
--
Jurian Sluiman
Soflomo - http://soflomo.com