catalyst overview

Upload: buntyray

Post on 09-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Catalyst Overview

    1/22

    INTRODUCTION TO CATALYST Object oriented Perl MVC Framework

    By

    BUNTY RAY

  • 8/8/2019 Catalyst Overview

    2/22

    WHAT IS MVC?

    Model View Controller: Architecture that separates an application's data model,

    user interface, and control logic into 3 distinct components

    modifications to one component can be made with minimal

    impact to the others.

    Responsibility model - domain-specific representation of the information

    on which the application operates (database)

    view - renders the model into a form suitable for

    interaction, typically a user interface (html) controller- responds to events and invokes changes on the model

    and/or the view

  • 8/8/2019 Catalyst Overview

    3/22

    WHAT IS CATALYST?

    A framework for development of web application

    using Model-View-Controller pattern: Promotes the re-use of existing Perl modules that already handle common web

    application

    Catalyst is controller

    View: Template::Toolkit, Mason, HTML::Template, etc.

    Model: Class::DBI, Tangram, Rose::DB, etc.

    platform independent (really!)

    Engines: CGI, FastCGI, mod_perl (1.3 and 2.0), self restarting

    development engine

  • 8/8/2019 Catalyst Overview

    4/22

    WHY CATALYST?

    Doesn't aim to provide end to end solution (RnR)

    Flexibility use different components as M or V Reusability plugins (extends runtime functionality):

    session, form validation, static files serving, unicode

    Built-in development tools:

    Self-restarting development web server on dev's machine Distribute the application as CPAN module

    Framework for auto and unit tests

    Doesn't need web server to generate pages

  • 8/8/2019 Catalyst Overview

    5/22

    CATALYST ARCHITECTURE FLOW

  • 8/8/2019 Catalyst Overview

    6/22

    INSTALLING AND RUNNING

    Installation from CPAN

    $ perl -MCPAN -e 'install Task::Catalyst Confirm successful installation catalyst.pl

    (Before proceeding please check the next slide to confirm the output) Creating stub application $ catalyst.pl MyApp

    # output omitted [Next Slide] $ cd MyApp $ script/myapp_create.pl controller Library::Login Running $ script/myapp_server.pl Open in browser: http://localhost:3000/library/login/

    C:\Users\BUNTY\

    op\Server-Starte

  • 8/8/2019 Catalyst Overview

    7/22

    CHECK IF INSTALLED PROPERLY

    When you execute catalyst.pl , ensure you see the bellow output

    developer@dev01-th:~$ catalyst.pl

    Usage:catalyst.pl [options] application-name

    'catalyst.pl' creates a skeleton for a new application, and allows youto upgrade the skeleton of your old application.

    Options:-force don't create a .new file where a file to be created exists

    -help display this help and exit-makefile only update Makefile.PL-scripts only update helper scripts-short use short names, M/V/C instead of Model/View/Controller.

    application-name must be a valid Perl module name and can include "::",which will be converted to '-' in the project name.

    Examples:catalyst.pl My::Appcatalyst.pl MyApp

    To upgrade your app to a new version of Catalyst:

    catalyst.pl -force -scripts MyApp

  • 8/8/2019 Catalyst Overview

    8/22

    CATALYST URL

  • 8/8/2019 Catalyst Overview

    9/22

    APPS

    developer@dev01-th:~$ catalyst.pl MyApp

    created "MyApp"

    created "MyApp/script"created "MyApp/lib"

    created "MyApp/root"

    created "MyApp/root/static

    created "MyApp/root/static/images"

    created "MyApp/t"

    created "MyApp/lib/MyApp"

    created "MyApp/lib/MyApp/Model"

    created "MyApp/lib/MyApp/View"

    created "MyApp/lib/MyApp/Controller"

    created "MyApp/myapp.conf"

    created "MyApp/lib/MyApp.pmcreated "MyApp/lib/MyApp/Controller/Root.pm"

    created "MyApp/README"

    created "MyApp/Changes"

    created "MyApp/t/01app.t"

    created "MyApp/t/02pod.t"

  • 8/8/2019 Catalyst Overview

    10/22

    CONTINUED

    created "MyApp/t/03podcoverage.t"

    created "MyApp/root/static/images/catalyst_logo.png"

    created "MyApp/root/static/images/btn_120x50_built.png"

    created "MyApp/root/static/images/btn_120x50_built_shadow.png"

    created "MyApp/root/static/images/btn_120x50_powered.png"

    created "MyApp/root/static/images/btn_120x50_powered_shadow.png"

    created "MyApp/root/static/images/btn_88x31_built.png"

    created "MyApp/root/static/images/btn_88x31_built_shadow.png"

    created "MyApp/root/static/images/btn_88x31_powered.png"created "MyApp/root/static/images/btn_88x31_powered_shadow.png"

    created "MyApp/root/favicon.ico

    created "MyApp/Makefile.PL"

    created "MyApp/script/myapp_cgi.pl"

    created "MyApp/script/myapp_fastcgi.pl"

    created "MyApp/script/myapp_server.pl"

    created "MyApp/script/myapp_test.pl"

    created "MyApp/script/myapp_create.pl"

  • 8/8/2019 Catalyst Overview

    11/22

    DIRECTORY

    drwxr-xr-x 2 developer developer 4096 2010-08-22 01:06 script

    -rw-r--r-- 1 developer developer 52 2010-08-22 01:06 README-rw-r--r-- 1 developer developer 133 2010-08-22 01:06 myapp.conf

    -rw-r--r-- 1 developer developer 144 2010-08-22 01:06 Changes

    drwxr-xr-x 3 developer developer 4096 2010-08-22 01:20 lib

    drwxr-xr-x 4 developer developer 4096 2010-08-22 01:49 root

    drwxr-xr-x 4 developer developer 4096 2010-08-22 01:53 inc-rw-r--r-- 1 developer developer 488 2010-08-22 01:53 META.yml

    -rw-r--r-- 1 developer developer 26474 2010-08-22 01:53 Makefile

    -rw-r--r-- 1 developer developer 0 2010-08-22 01:54 pm_to_blib

    drwxr-xr-x 8 developer developer 4096 2010-08-22 01:54 blib

    -rw-r--r-- 1 developer developer 624 2010-08-22 01:55 Makefile.PL

    drwxr-xr-x 2 developer developer 4096 2010-08-22 02:19 t

  • 8/8/2019 Catalyst Overview

    12/22

    CONTINUED

    Makefile.PL

    This script generates a Makefile to build , test and install your application. Thisfile is invoked (perl Makefile.Pl) after you executed the command

    catalyst.pl MyApp (output attached)

    It also contains a list of your applications CPAN dependencies andautomatically install them.

    Changes This is simply a text file where you can document changes to your application.This can be used as an help for end user or other developers.

    R EADME

    Its a text with information on your application.

    myapp.conf This is the main configuration file for the application which is loaded when yourapplication starts.

    C:\Users\BUNTY\p\Running_Mak

  • 8/8/2019 Catalyst Overview

    13/22

    CONTINUED/lib Directory

    Its the hear of the application. This directory contains a file MyApp.pm, which

    defines the namespace and inheritance that are necessary to make this Catalystapplication. It also contains list of plugins to load application specificconfiguration.

    /lib directory contains 3 key directories

    Controller , View and Model

    Catalyst loads the Controller, Model and View from these directories.

    Basically the default Controller is Root.pm . This will handle all root level ( / )URLs. This is where the code that generates the welcome page is located.

    MyApp under /lib is like a namespace of a module. The application is createdwith whatever argument to catalyst.pl.

    e.g. catalyst.pl MyCompany::HR::Timeslips

    This will create the application with namespace MyCompany::HR::Timeslipsand in file system its will be /lib/MyCompany/HR/Timeslips

  • 8/8/2019 Catalyst Overview

    14/22

    CONTINUED

    /root Directory

    Contains the template and other non code support file.

    Contains a /static sub directory which holds static contents like image files andstylesheet, css files etc.

    /script Directory

    Contains scripts needed to run, test and modify your application

    (i) myapp_server.pl->This is the development server (a self-contained HTTPD server) that you canuse to run your application while you are developing it.

    (ii) myapp_cgi.pl->

    This is a script for deploying your application with a web server that can not use

    mod_perl or FastCGI. Its very slow and low performer, so should be used as thelast option.

    (iii) myapp_fastcgi.pl ->

    Allows you to run your application in a FastCGI server.(Production Mode)

  • 8/8/2019 Catalyst Overview

    15/22

    CONTINUED

    (iv) myapp_test.pl ->

    Basically used to test an action without opening browser.(v) myapp_create.pl ->

    This is a helper script. This is customized version of the catalyst.pl for yourapplication. It can be used to create Controller, Model and View.

    /t Directory

    The t directory is where your application's automatic tests are stored.

    01app.t, which is a test that passes if your application compiles.

    02pod.t, which will pass if your Plain Old Documentation (POD) embeddedAPI documentation, inside your application, is valid.

    03podcoverage.t, which tests that every public function in your applicationhas some documentation.

    You can run the tests in this command by running make test, afterMakefile.PL creates the Makefile.

  • 8/8/2019 Catalyst Overview

    16/22

    HANDLING URL REQUEST

    When a user makes a request using Browser, Catalyst looks for the appropriatemethod that can handle the request within package called Controller.

    http://url/controller.action

    (hello) (test)

    /lib/appname/Controller/Hello.pm

    e.g. http://192.168.187.132:3000/hello/index.html

    Will match the hello Controller (Hello.pm) index will matchthe subroutine

    within the Hello.pm

    http://url/controller.actionhttp://192.168.187.132:3000/hello/index.htmlhttp://192.168.187.132:3000/hello/index.htmlhttp://url/controller.actionhttp://url/controller.action
  • 8/8/2019 Catalyst Overview

    17/22

    CREATING A CONTROLLER

    perl script/myapp_create.pl controller Hello

    This will create /lib/MyApp/Controller/Hello.pm

    package MyApp::Controller::Hello;

    use strict;

    use warnings;

    use parent 'Catalyst::Controller';

    =head1 NAMEMyApp::Controller::Hello - Catalyst Controller

    =head1 DESCRIPTION

    Catalyst Controller.

    =head1 METHODS=cut

    =head2 index

    =cut

    C:\Users\BUNTY\op\Creating A c

  • 8/8/2019 Catalyst Overview

    18/22

    CONTINUED

    sub index :Path :Args(0) {

    my ( $self, $c ) = @_;

    $c->response->body('Matched MyApp::Controller::Hello in Hello.');

    }

    =head1 AUTHOR

    developer,,,

    =head1 LICENSE

    This library is free software, you can redistribute it and/or modify

    it under the same terms as Perl itself.=cut

    1;

    At the top is the package declaration (so Perl knows what this module is

    named), and then some use statements (to tell Perl that this module uses

    Moose). Then, BEGIN {extends 'Catalyst::Controller'; } to tell Moose/Perl thatthis module is a Catalyst Controller. Next, there is some POD (the text starting

    with =) that you can fill in to provide some API documentation (see man

    perlpod for the syntax.

  • 8/8/2019 Catalyst Overview

    19/22

    CONTINUED

    Let us check what just happened by running the server with the following

    command: perl script/myapp_server.pl

    [debug] Loaded Path actions:

    . -------------------------------------+--------------------------------------.

    | Path | Private |

    +-------------------------------------+--------------------------------------+| / | /index |

    | / | /default |

    | /hello | /hello/index |

    Opening the URL http://localhost:3000/hello will show you the following:

    Matched MyApp::Controller::Hello in Hello

  • 8/8/2019 Catalyst Overview

    20/22

    ADDING A VIEW

    Now the URL is mapped to Controllers method. Its time now to show someHTML content using View.

    View is a system that defines how content can be rendered. Currently we willmake use of Template Toolkit templating system to enable TT view for our HTMLpage. (Template Toolkit module is available with Catalyst)

    Step 1:

    Remove the below line from Hello.pm Controller under /lib/MyApp/Controllerlocation from the subroutine index().

    $c ->response ->body()Catalyst knows how to take care of forwarding the response to the default viewafter the Controllers execution.

    Step 2:

    Now create the view

    perl script/myapp_create.pl view TT TTThis means create a view called View/TT.pm (1st TT) based on the standardCatalyst::View::TT (2nd TT). Technically this means we can create as many viewsof Catalyst::View:TT with different names.

    C:\Users\BUNTY\op\Creating a T

  • 8/8/2019 Catalyst Overview

    21/22

    CONTINUEDStep 3:

    Now update the Makefile.Pl with the below content under requires section. This step is

    not mandatory, but is advised to do since it will be useful when you are migrating theapplication to a different machine.

    requires Catalyst::View::TT =>0;

    Now run the below commands

    (a) perl Makefile.PL

    (b) make

    Step 4:

    Now create a template under /root/hello directory with a name index.tt

    /root is the default location from where Catalyst::View::TT will look for templates.

    Now add content to the .tt file to display

    hello , world!

    C:\Users\BUNTY\p\Running_Mak

    C:\Users\BUNTY\p\Running_mak

    TML Document

  • 8/8/2019 Catalyst Overview

    22/22