Yii Framework Forum: Better Loadmodel() - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Better Loadmodel() MVC Best Practices Rate Topic: -----

#1 User is offline   danschmidt5189 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 19-February 13

Posted 05 November 2013 - 03:33 PM

Here's a quick tip that should help you write clean code in large CRUD-centric applications. This is something that's helped me considerably working on and refactoring a fairly large web application that supports over 20 user types with context- and model-dependent behavioring.

The basic idea is that a controller should be lean and "dumb":
  • Manage only one class of records.
  • Customize its records' behaviors based on the user/application context
  • Allow for searching, creating, viewing, modifying, and deleting specific records.
  • Delegate all complex logic to the model (e.g. how to search, or what records to find)
  • Delegate loading data and invoking model methods to Actions.


See the gists in my reply below for implementation ideas.
0

#2 User is offline   Shahcheraghean 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 12-December 12
  • Location:Mashhad, IRAN

Posted 06 November 2013 - 01:15 AM

Thank you.
I suggest you to extend it in Gii and published it in github as an extension.
Good luck.
www.shgn.ir
www.shgn.ir/yii-framework/
yii2.swsco.ir
In a world without any fences or walls,Who needs Gates or Windows!
0

#3 User is offline   danschmidt5189 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 19-February 13

Posted 14 November 2013 - 06:38 PM

View PostShahcheraghean, on 06 November 2013 - 01:15 AM, said:

Thank you.
I suggest you to extend it in Gii and published it in github as an extension.
Good luck.


I've actually moved to a more general concept just involving loadModel(). I think the docs and auto-generators leave a lot to be desired. Here's the gist of it as well as a conversation starter for self-describing controllers.
0

#4 User is offline   Shahcheraghean 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 12-December 12
  • Location:Mashhad, IRAN

Posted 15 November 2013 - 10:02 AM

So great, Thanks.:D
www.shgn.ir
www.shgn.ir/yii-framework/
yii2.swsco.ir
In a world without any fences or walls,Who needs Gates or Windows!
0

#5 User is offline   hajdar 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 16-November 13

Posted 17 November 2013 - 03:27 PM

Thanks, great tutorial
0

#6 User is offline   danschmidt5189 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 19-February 13

Posted 19 November 2013 - 03:20 AM

Thanks for the feedback.

I bundled the classes I'm using into an extension that I've posted to Github.

This extension differs from the Yii philosophy (expressed by Gii) that controllers should contain the logic used to generate their action parameters. (E.g. using loadModel().) In the extension, the controller delegates the task of generating action parameters to a form class.

The flow is as follows:
  • Controller parses raw request data using an adaptor class.
  • The adaptor class passes attributes to an action parameters model in a standardized format
  • The parameters model validates the request parameters and loads additional dependent parameters, e.g. a model
  • The filters validate the parameters model
  • The parameters model attributes are bound as action parameters automatically by Yii


A standard RESTController with this extension is just a controller extending from the base class. In many cases you will not have to modify the default actions() or filters() configurations at all.
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users