📜 ⬆️ ⬇️

Basic principles of working with the Catalyst MVC Framework

Foreword
There are very few Habrés, or rather no articles at all on such a wonderful MVC framework like Catalyst. And so I decided to devote my first article to him.
Here I described the basic principles for working with Catalyst on the example of a simple web application, the so-called quick start for mastering this framework.

Introduction
Catalyst is a powerful MVC framework for developing Perl web applications.
A few words about MVC (Model / View / Controller):

Catalyst - contains an extensive number of CPAN modules that facilitate the development of a Perl web application.

What is the simplicity when using this framework?

')
The process of creating a new project is performed using the command: catalyst.pl Test::App (in the file system in the directory with the project name "::" it will be replaced with "-".

This script is part of the auxiliary system, which has the name Catalyst :: Helper, which generates the necessary scripts, folders, tests, files, etc. for the future application.

The structure of the created project:

The required models / views / controllers are added using the appropriate command: script/test_app_create.pl {model|view|controller} { }

Contents of the TestApp.pm file

 package Test::App; use Moose; use namespace::autoclean; use Catalyst::Runtime 5.80; use Catalyst qw/ -Debug #    ConfigLoader #    Static::Simple #     /root /; extends 'Catalyst'; our $VERSION = '0.01'; __PACKAGE__->config( name => 'Test::App', disable_component_resolution_regex_fallback => 1, enable_catalyst_header => 1, # Send X-Catalyst header ); #   __PACKAGE__->setup(); 1; 

As we can see, Catalyst uses the Moose module for the object display of the CPAN elements used, which greatly simplifies working with objects in the Perl language.

The Root.pm file which is located in the / lib / Test / App / Controller / folder looks like this:
 #       ,     . package Test::App::Controller::Root; use Moose; use namespace::autoclean; BEGIN { extends 'Catalyst::Controller' } __PACKAGE__->config(namespace => ''); #  ,     Args(0),  ,     url (. test.com/1a/2b/3c  Args(1)  "1b"  .) sub index :Path :Args(0) { my ( $self, $c ) = @ _; # Hello World $c->response->body( $c->welcome_message ); } #    ,      ,   404 sub default :Path { my ( $self, $c ) = @ _; $c->response->body( 'Page not found' ); $c->response->status(404); } # ,      sub end : ActionClass('RenderView') {} 1; 


Now, after getting acquainted with this framework, we will try to create a simple web application.

1. Create a view using the script/test_app_create.pl view Web TT command (TT - Template Toolkit, templates, other templates can also be used, for example, Mason)

2. Create the index.tt file in the root folder /
Paste this code there:
  [% IF result %]<p>[% result %]</p> [% END %] <form name="hello" method=POST action="hello"> <input type="text" name="hi" value=""> <input type="submit" value="Do something!"> </form> 


3. In the Root.pm file we delete the following line: $c->response->body( $c->welcome_message ); # $c->response->body( $c->welcome_message ); #

4. add hello method there:
 sub hello :Local { my ($self, $c) = @ _; my $hi = $c->req->body_params->{hi}; $c->stash( result => $c->model('Hello')->hello($hi), template => 'index.tt', ); } 

5. Create model script/test_app_create.pl Model Hello
script/test_app_create.pl Model Hello

6. In the created model we write the method:
 sub hello { my ($self, $hi) = @ _; return "Hello $hi!\n"; } 


7. Run the script/test_app_server test server

8. In the browser, enter localhost:3000/ localhost:3000/ (default port)

We admire the result.

PS Here, in principle, the basic ideas when working with Catalyst, and I would like more articles on Habré from people who know this framework. Documentation is certainly good, but real experience is always more interesting.

PPS While writing this article and here pearl was not without magic. The code uses the array "@_", which is translated in tags when specifying the language of pearls in a habrauser with the name "_", did not think of anything beautiful how to put spaces between @ and _. Be careful…

Source: https://habr.com/ru/post/146074/


All Articles