MasonX::MiniMVC::Cookbook -- examples of MiniMVC usage
Create a controller method that looks like this:
sub help { my ($self, $m) = @_; $m->comp("view/something/help.mhtml") }
Your help.mhtml view file will (presumably) contain only static HTML.
help.mhtml
Create a controller method that does most of the work of figuring out the details, then passes a pile o' data to the view for display:
sub details { my ($self, $m, $id) = @_; # let's pretend fetch() gives us a hashref... my $details = MyApp::Model::Whatever->fetch($id); $m->comp("view/something/details.mhtml", details => $details); }
The view looks something like this:
<%args> %details </%args> <h1>Details for <% $details{title} %></h1> <p> Description: <% $details{description} %> </p>
In your controller, do:
sub view { my ($self, $m, @args) = @_; # ... }
If the user requested the URL http://example.com/article/view/foo/bar/baz and the controller for "article" is MyApp::Article, then this will call MyApp::Article::view() with @args set to ("foo", "bar", "baz").
MyApp::Article::view()
@args
("foo", "bar", "baz")
sub add { my ($self, $m, @args) = @_; my %fields = $m->request_args(); }
If you want a single form field, you can use $m-request_args->{$field}>.
$m-
Here's an example of combining the previous two techniques, taken from the examples/library demo included with the MiniMVC distro.
examples/library
sub search { my ($self, $m, @args) = @_; $m->notes("title", "MiniMVC Library Demo: Search Results"); if (my $query = $m->request_args->{query}) { # search by form $m->comp("view/book/search_results.mhtml", query => $query); } elsif (@args) { # search by URL $m->comp("view/book/search_results.mhtml", query => join(" ", @args)); } else { $m->notes("title", "MiniMVC Library Demo: Search"); $m->comp("view/book/search_form.mhtml"); } }
Edit the autohandler file to include whatever HTML you want.
autohandler
Use Mason's notes() facility:
notes()
$m->notes("key", "value"); $m->notes("title", "Details for item $id");
Then, in the autohandler, do something like:
<head> <title><% $m->notes("title") || "My Application" %></title> </head>
MasonX::MiniMVC
To install MasonX::MiniMVC, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MasonX::MiniMVC
CPAN shell
perl -MCPAN -e shell install MasonX::MiniMVC
For more information on module installation, please visit the detailed CPAN module installation guide.