| Revision 18 |
Current |
| |
Note: page has been moved to https://github.com/markstos/CGI--Application/wiki/MVC by MidLifeXis |
| |
|
| = MVC = Model-View-Controller |
= MVC = Model-View-Controller |
| /by MarkRajcok/ |
/by MarkRajcok/ |
| |
|
| For web development, MVC is a term used to describe how to separate a web application into the following three pieces (roughly speaking): |
For web development, MVC is a term used to describe how to separate a web application into the following three pieces (roughly speaking): |
| * model = core functionality (domain logic, business rules) and data/content (stored in a database, flat file(s), etc.) |
* model = core functionality (domain logic, business rules) and data/content (stored in a database, flat file(s), etc.) |
| * view = displays information to the user (presentation: HTML, CSS, etc.) based on model data |
* view = displays information to the user (presentation: HTML, CSS, etc.) based on model data |
| * controller = handles user input, controls what you see and notifies the model of user actions |
* controller = handles user input, controls what you see and notifies the model of user actions |
| Both the views and controllers comprise the user interface. The model should not have any knowledge of the views. |
Both the views and controllers comprise the user interface. The model should not have any knowledge of the views. |
| |
|
| CGI::Application helps you obtain the separation: |
CGI::Application helps you obtain the separation: |
| * plugin modules such as {pod:CGI::Application::Plugin::DBH} make it easy to interact with your database/model (but separate "business" objects, not derived from CGI::Application, should be used for the model and hence database interaction) |
* plugin modules such as {pod:CGI::Application::Plugin::DBH} make it easy to interact with your database/model (but separate "business" objects, not derived from CGI::Application, should be used for the model and hence database interaction) |
| * CGI::Application has built-in {pod:HTML::Template} support, and support for other templating systems, which keeps your view/HTML in separate files (and not mixed with your Perl code) |
* CGI::Application has built-in {pod:HTML::Template} support, and support for other templating systems, which keeps your view/HTML in separate files (and not mixed with your Perl code) |
| * CGI::Application's "run modes" help you divide your application into pieces of controller logic, which normally correspond to pages on a website, or user actions. For example: |
* CGI::Application's "run modes" help you divide your application into pieces of controller logic, which normally correspond to pages on a website, or user actions. For example: |
| |
|
| package MyOnlineStore; |
package MyOnlineStore; |
| use strict; |
use strict; |
| use base 'CGI::Application'; |
use base 'CGI::Application'; |
| use CGI::Application::Plugin::AutoRunmode; |
use CGI::Application::Plugin::AutoRunmode; |
| use CGI::Application::Plugin::DBH (qw/dbh_config dbh/); |
use CGI::Application::Plugin::DBH (qw/dbh_config dbh/); |
| |
|
| sub show_items_for_sale : StartRunmode { |
sub show_items_for_sale : StartRunmode { |
| my $self = shift; |
my $self = shift; |
| my $t = $self->load_tmpl('list_of_items.tmpl'); |
my $t = $self->load_tmpl('list_of_items.tmpl'); |
| ... access some business objects to get the items ... |
... access some business objects to get the items ... |
| ... populate the template with the data ... |
... populate the template with the data ... |
| return $t->output |
return $t->output |
| } |
} |
| sub show_single_item : Runmode { |
sub show_single_item : Runmode { |
| ... |
... |
| } |
} |
| sub new_user : Runmode { |
sub new_user : Runmode { |
| ... |
... |
| } |
} |
| |
|
| For each runmode, you'll often have an HTML template file, which allows you to cleanly separate your view/presentation/HTML from your controller logic/code. This is particularly handy if you're working with people who don't know Perl, but you want/need them to make changes to the HTML (view/presentation). They simply modify the .tmpl files (and CSS files), while you work on the Perl code. |
For each runmode, you'll often have an HTML template file, which allows you to cleanly separate your view/presentation/HTML from your controller logic/code. This is particularly handy if you're working with people who don't know Perl, but you want/need them to make changes to the HTML (view/presentation). They simply modify the .tmpl files (and CSS files), while you work on the Perl code. |
| |
|
| See also |
See also |
| * [Sitepoint article http://www.sitepoint.com/article/cgi-application] |
* [Sitepoint article http://www.sitepoint.com/article/cgi-application] |
| * [Tim's MVC explanation http://web.archive.org/web/20041205104109/dev.slaggle.com/archives/2004/10/15/authentication-with-cgiapplication/] |
* [Tim's MVC explanation http://web.archive.org/web/20041205104109/dev.slaggle.com/archives/2004/10/15/authentication-with-cgiapplication/] |
| * [Wikipedia MVC http://en.wikipedia.org/wiki/Model-view-controller] |
* [Wikipedia MVC http://en.wikipedia.org/wiki/Model-view-controller] |
| * [Wikipedia Three-tier architecture http://en.wikipedia.org/wiki/Three-tier_%28computing%29] |
* [Wikipedia Three-tier architecture http://en.wikipedia.org/wiki/Three-tier_%28computing%29] |
| |
|
| |
|