Squatting::Controller - default controller class for Squatting
package App::Controllers; use Squatting ':controllers'; our @C = ( C( Thread => [ '/forum/(\d+)/thread/(\d+)-(\w+)' ], get => sub { my ($self, $forum_id, $thread_id, $slug) = @_; # # get thread from database... # $self->render('thread'); }, post => sub { my ($self, $forum_id, $thread_id, $slug) = @_; # # add post to thread # $self->redirect(R('Thread', $forum_id, $thread_id, $slug)); } ) );
Squatting::Controller is the default controller class for Squatting applications. Its job is to take HTTP requests and construct an appropriate response by setting up output headers and returning content.
The constructor takes a name, an arrayref or URL patterns, and a hash of method definitions. There is a helper function called C() that makes this slightly less verbose.
This will create a shallow copy of the controller. You may optionally pass in a hash of options that will be merged into the new clone.
These methods are called when their respective HTTP requests are sent to the controller. @args is the list of regex captures from the URL pattern in $self->urls that matched $self->env->{REQUEST_PATH}.
The following methods are lvalue subroutines that contain information relevant to the current controller and current request/response cycle.
This returns the name of the controller.
This returns the arrayref of URL patterns that the controller responds to.
This returns the Continuity::Request object for the current session.
This returns a hashref populated with a CGI-like environment. This is where you'll find the incoming HTTP headers.
This returns a hashref containing the incoming CGI parameters.
Example: Interpreting the query ?x=5&y=true&z=2&z=1&z=3 .
$self->input->{x} is 5 $self->input->{y} is "true" $self->input->{z} is [2, 1, 3]
This is an accessor for $self->input that provides an API that's a subset of the CGI module's param() function. It exists, because there are many perl modules that can make use of an object that follows this API. It is not complete, but it should be good enough for WWW::Facebook::API::Canvas and many other modules.
$self->input
param()
This returns a hashref that holds both the incoming and outgoing cookies.
Incoming cookies are just simple scalar values, whereas outgoing cookies are hashrefs that can be passed to CGI::Cookie to construct a cookie string.
Example: Setting a cookie named 'foo'
$self->cookies->{foo} = { -Value => 'bar', -Expires => '+1d' };
Example: Getting the value of a cookie named 'baz'
my $baz = $self->cookies->{baz};
If you've setup sessions, this method will return the current session data as a hashref.
This returns a hashref that represents the outgoing variables for this request. This hashref will be passed to a view's templates when render() is called.
This returns an integer representing the outgoing HTTP status code. See HTTP::Status for more details.
$self->status = 404; # Resource Not Found
This returns a hashref representing the outgoing HTTP headers.
Example: Setting the outgoing Content-Type to text/plain
$self->headers->{'Content-Type'} = 'text/plain';
This returns a logging object if one has been set up for your app. If it exists, you should be able to call methods like debug(), info(), warn(), error(), and fatal() against it, and the output of this would typically end up in an error log.
debug()
info()
warn()
error()
fatal()
This returns the name of the default view for the current request. If it's undefined, the first view in @App::Views::V will be considered the default.
This returns the name of the app that this controller belongs to.
This method will return a string generated by the specified template and view. If a view is not specified, the first view object in @App::Views::V will be used.
This method is a shortcut for setting $self->status to 302 and $self->headers->{Location} to the specified URL. You may optionally pass in a different status code as the second parameter.
Squatting, Squatting::View
To install Squatting, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Squatting
CPAN shell
perl -MCPAN -e shell install Squatting
For more information on module installation, please visit the detailed CPAN module installation guide.