ktats****@users*****
ktats****@users*****
2012年 9月 27日 (木) 23:17:20 JST
Index: docs/articles/www.perl.com/pub/2005/06/02/catalyst.html diff -u /dev/null docs/articles/www.perl.com/pub/2005/06/02/catalyst.html:1.1 --- /dev/null Thu Sep 27 23:17:20 2012 +++ docs/articles/www.perl.com/pub/2005/06/02/catalyst.html Thu Sep 27 23:17:20 2012 @@ -0,0 +1,947 @@ +<html> +<head><title>Catalyst - Perl.com</title> +<link REL="STYLESHEET" HREF="../basestyle.css"> +<link REL="STYLESHEET" HREF="perl_study.css"> +</head> +<body> +<h1>Catalyst</h1> +<p> +Jesse Sheidlowerããã®è¨±å¯ãå¾ã¦ã翻訳ãå ¬éãã¦ããã¾ãããªãããã®ç¿»è¨³ã¯ãunder same terms of Perl itselfã§ãé å¸å¯è½ã§ãã<br><br> +åæã¯ãã¡ã: <a href="http://www.perl.com/pub/a/2005/06/02/catalyst.html">Catalyst</a> +</p> +<h2>ç®æ¬¡</h2> +<div class="right"> +<div class="menu"> +<strong>Perl é¢é£</strong> +<br>ã»<a href="./Perl_variable.html">Perl å¤æ°ã«ã¤ãã¦</a> +<br>ã»<a href="./Perl_tips.html">Perl Tips</a> +<br>ã»<a href="./Perl_oo.html">Perlãªãã¸ã§ã¯ãæåããã°ã©ãã³ã°</a> +<br>ã»<a href="./Perl_unicode.html">Perl 5.8 Unicode é¢é£</a> +<br>ã»<a href="./Books.html">Perl(ã¨ã)ã®æ¸ç±</a> +</div> + +<ul> +<li> <a href="#1">Catalyst</a></li> +<li> <a href="#2">ã¤ã³ã¹ãã¬ã¼ã·ã§ã³ãåãããã®</a></li> +<li> <a href="#3">ç¹å¾´</a></li> +<ul> +<li> <a href="#4">ã¹ãã¼ã</a></li> +<li> <a href="#5">ç°¡ææ§</a></li> +<li> <a href="#6">æè»æ§</a></li> +</ul> +<li> <a href="#7">ãµã³ãã«ã¢ããªã±ã¼ã·ã§ã³: 30è¡ã®ã³ã¼ãã§ãMiniMojo, Ajax-Based Wiki</a></li> +<ul> +<li> <a href="#8">ã¤ã³ã¹ãã¼ã«</a></li> +<li> <a href="#9">ã¢ããªã±ã¼ã·ã§ã³ã®ã¹ã±ã«ãã³ã®ä½æ</a></li> +<li> <a href="#10">Application Classã«åºæ¬çãªã¡ã½ããã追å </a></li> +<li> <a href="#11">Model(SQLite ãã¼ã¿ãã¼ã¹)ã®ã»ããã¢ããã¨ãã«ãã¼ã¹ã¯ãªããã«ããModelã¯ã©ã¹ã®çæ</a></li> +<li> <a href="#12">Viewã®ã»ããã¢ãã(<code>Template::Toolkit</code>) ã¨ãã«ãã¼ã¹ã¯ãªããã«ããViewã¯ã©ã¹ã®ä½æ</a></li> +<li> <a href="#13">表示ã¨ç·¨éã®ã³ã¼ãã®è¿½å </a></li> +<li> <a href="#14">Ajaxã§Wikiãã»ããã¢ãããã</a></li> +</ul> +<li> <a href="#15">ãªã½ã¼ã¹</a></li> +<li> <a href="#16">翻訳ã«ã¤ãã¦</a></li> +</ul> +<a name="1"> </a> +<h2>Catalyst</h2> +<p> Jesse Sheidlower<br> +June 02, 2005 +</p> +<p class="original">Web frameworks are an area of significant interest at the moment. Now that +we've all learned the basics of web programming, we're ready to get the +common stuff out of the way to concentrate on the task at hand; no one wants to +spend time rewriting the same bits of glue to handle parameter processing, +request dispatching, and the like.</p> + +<p> +Web ãã¬ã¼ã ã¯ã¼ã¯ã¯ãç®ä¸ãéè¦ãªé¢å¿ãéãã¦ããåéã§ããç¾å¨ãã¿ããªWebããã°ã©ãã³ã°ã®åºç¤ãå¦ãã§ãã¾ã£ã¦ã +ä»ã¯ç®åã«ããä»äºã«éä¸ããããããã§ã¯ãªããå ±éã®ãã®ãå¾ãææã§ã; +ãã誰ãããã©ã¡ã¿ã®å¦çããªã¯ã¨ã¹ãã®æ¯ãåãããããã£ããã®ãããããã®ã +åããããªãã¡ã£ã½ããªç³ã®ãããªãã®ãæ¸ãæããã®ã«æéã浪費ãããããã¾ããã +</p> + +<p class="original">A model currently favored for web applications is <em>MVC</em>, or +Model-View-Controller. This design pattern, originally from Smalltalk, supports +the separation of the three main areas of an application--handling application +flow (Controller), processing information (Model), and outputting results +(View)--so that it is possible to change or replace any one without affecting +the others.</p> + +<p> +ç¾å¨ã Web ã¢ããªã±ã¼ã·ã§ã³ã§å¥½ã¾ãã¦ããã¢ãã«ã¯ã<em>MVC</em>ãModel-View-Controllerã§ãã +ãã®ãã¶ã¤ã³ã¿ã¼ã³ã¯ããã¨ãã¨ã¯Smalltalkãããã¦ãããModeã Viewã Controllerã®3ã¤ã®ä¸»è¦ãªé¨å +--ã¢ããªã±ã¼ã·ã§ã³ããã¼(Controller)ãæ å ±ã®å¦ç(Model)ãçµæã®è¡¨ç¤º(View) -- +ãåå²ãããã¨ã«å¯¾å¿ãã¦ãã¾ãããããããã¨ã§ãä»ã¸ã®å½±é¿ãªãã«ãä½ã«ã§ãå¤æ´ãç½®æãããã¨ãã§ãã¾ãã +</p> + +<p class="original">Catalyst is a new MVC framework for Perl. It is currently under rapid +development, but the core API is now stable, and a growing number of projects +use it. Catalyst borrows from other frameworks, such as Ruby on Rails and +Apache Struts, but its main goal is to be a flexible, powerful, and fast +framework for developing any type of web project in Perl. This article, the +first of a series of two, introduces Catalyst and shows a simple application; a later +article will demonstrate how to write a more complex project.</p> + +<p> +Catalyst ã¯æ°ããPerlã®MVCãã¬ã¼ã ã¯ã¼ã¯ã§ããç¾å¨ãæ¥éã«éçºããã¦ãã¾ããã +ã³ã¢ã®APIã¯å®å®ãã¦ãã¾ããããã«ãCatalyst ã使ãããã¸ã§ã¯ãã®æ°ãå¢ãã¤ã¤ããã¾ãã +Catalyst ã¯ä»ã®ãã¬ã¼ã ã¯ã¼ã¯ - Ruby on RailsãApache Strutsã®ãã㪠- ããåãã¦ãã¾ãã +ã§ãããCatalyst ã®ã¡ã¤ã³ã´ã¼ã«ã¯ãPerlã§ä½ãããã©ã®ãããªã¿ã¤ãã®Webããã¸ã§ã¯ãã®éçºã«ã使ããã +æè»æ§ããããå¼·åãªãéããã¬ã¼ã ã¯ã¼ã¯ã§ãã +ãã®è¨äºã¯ã2ã¤ã®ã·ãªã¼ãºã®1ã¤ç®ã§ããCatalystã®ç´¹ä»ã¨ç°¡åãªã¢ããªã±ã¼ã·ã§ã³ãè¦ãã¾ã; +å¾ã®è¨äºã§ã¯ãã£ã¨è¤éãªããã¸ã§ã¯ãã®æ¸ãæ¹ããã¢ããã§ãããã +</p> + +<div class="back"> +<a href="#top">â² to Top</a> +</div> + +<a name="2"> </a> +<h2>ã¤ã³ã¹ãã¬ã¼ã·ã§ã³ãåãããã®</h2> + +<p class="original">Catalyst grew out of Maypole, an MVC framework developed by Simon Cozens +(and discussed last year on Perl.com; see "<a href="http://www.perl.com/pub/a/2004/04/15/maypole.html">Rapid Web Application +Development with Maypole</a>," for example). Maypole works well for typical CRUD (Create, Retrieve, Update, Delete) +databases on the Web. It includes a variety +of useful methods and prewritten templates and template macros that make it +very easy to set up a powerful web database. However, it focuses so strongly +on CRUD that it is less flexible for other tasks. One of the goals of Catalyst +is to provide a framework well suited for any web-related project.</p> + +<p> +Catalyst ã¯ãMaipoleãSimon Cozensã«ãã£ã¦éçºãããMVCãã¬ã¼ã ã¯ã¼ã¯ãMaypole +( Perl.comã§æ¨å¹´è°è«ããã¾ãã; "<a href="http://www.perl.com/pub/a/2004/04/15/maypole.html">Rapid Web Application Development with Maypole</a>," åç §) +ããã大ãããªãã¾ãããMaypoleã¯ãWebã®ãã¼ã¿ãã¼ã¹ã®å ¸åçãªCRUD(Create, Retrieve, Update, Delete)ã§ããã¾ãåãã¾ãã +Maypoleã¯ãããããã®æçãªã¡ã½ããã¨æ¢åã®ãã³ãã¬ã¼ãã¨å¼·åãªWebãã¼ã¿ãã¼ã¹ãã»ããã¢ããããã®ãã¨ã¦ãç°¡åã«ãããã³ãã¬ã¼ããã¯ããããã¾ãã +ã§ãããMaypoleã¯CRUDã«å¼·ãç¦ç¹ãå½ã¦ããã¦ãããä»ã®ä»äºã«é¢ãã¦ã¯æè»æ§ãä½ããªã£ã¦ãã¾ãã +Catalystã®ã´ã¼ã«ã®1ã¤ã¯ãã©ã®ãããªWebã«é¢é£ããããã¸ã§ã¯ãã«ã§ããã¾ãåãããã¬ã¼ã ã¯ã¼ã¯ãæä¾ãããã¨ã§ãã +</p> + +<p class="original"><a href="http://www.rubyonrails.org/">Ruby on Rails</a> was another +inspiration; this popular system has done much to promote interest in the Ruby +programming language. Features we borrowed from RoR are the use of helper +scripts to generate application components and the ability to have multiple +controllers in a single application. Both RoR and Struts allow the use of +forwarding within applications, which also proved useful for Catalyst.</p> +<p> +<a href="http://www.rubyonrails.org/">Ruby on Rails</a> ã¯ãããã²ã¨ã¤ã®ã¤ã³ã¹ãã¬ã¼ã·ã§ã³ãåãããã®ã§ã; +ãã®äººæ°ã®ããã·ã¹ãã ã¯ãããã°ã©ãã³ã°è¨èªRubyã«å¤ãã®é¢å¿ãéãããã¾ããã +RoRããåããç¹å¾´ã¯ãã¢ããªã±ã¼ã·ã§ã³ã®ã³ã³ãã¼ãã³ããçæããããã«ãã¼ã¹ã¯ãªãã㨠+1ã¤ã®ã¢ããªã±ã¼ã·ã§ã³ã«è¤æ°ã®Controllerãæã¤ãã¨ã§ããã¨ããã§ãã +RoRã¨Strutsã¯ä¸¡æ¹ã¨ããã¢ããªã±ã¼ã·ã§ã³å ã§è»¢é(forwarding)ã使ãã¨ãã§ãã¾ãã +転éã¯ãCatalystã«ã¨ã£ã¦ãæçã ã¨åããã¾ããã +</p> + +<div class="back"> +<a href="#top">â² to Top</a> +</div> + +<a name="3"> </a> +<h2>ç¹å¾´</h2> + +<a name="4"> </a> +<h3>ã¹ãã¼ã</h3> + +<p class="original">We planned Catalyst as an enterprise-level framework, able to handle a significant + load. It makes heavy use of caching. Catalyst applications register their actions + in the dispatcher at compile time, making it possible to process runtime requests + quickly, without needing elaborate checks. Regex dispatches are all precompiled. + Catalyst builds only the structures it needs, so there are no delays to generate + (for example) unused database relations.</p> + +<p> +Catalystã¯ã¨ã³ã¿ã¼ãã©ã¤ãºã¬ãã«ã®ãããªãã®è² è·ãæ±ããã¨ãã§ãããã¬ã¼ã ã¯ã¼ã¯ã¨ãã¦è¨ç»ããã¦ãã¾ãã +ã¨ã³ã¿ã¼ãã©ã¤ãºã¬ãã«ã®ãã¬ã¼ã ã¯ã¼ã¯ã¯ããã£ãã·ã³ã°ãé »ç¹ã«ä½¿ãã¾ããCatalystã®ã¢ããªã±ã¼ã·ã§ã³ã¯ã +ã³ã³ãã¤ã«æã«ãã£ã¹ãããã£ã¼ã«ã¢ã¯ã·ã§ã³ãç»é²ãã¾ãããã®ãã¨ã§å®è¡æã®ãªã¯ã¨ã¹ãããè¤éãªãã§ãã¯ãªãã«ç´ æ©ãå¦çã§ãã¾ãã +æ£è¦è¡¨ç¾ã®ãã£ã¹ãããã¯å ¨ã¦ããªã³ã³ãã¤ã«ããã¾ãã +Catalystã¯å¿ è¦ã¨ããæ§é ã ããä½ãã¾ããã§ãã®ã§ã(ä¾ãã°)ãã¼ã¿ãã¼ã¹é¢ä¿ã使ããªããã®ãä½ãã®ã«é ãã¯ããã¾ããã +</p> + +<div class="back"> +<a href="#top">â² to Top</a> +</div> + +<a name="5"> </a> +<h3>ç°¡ææ§</h3> +<p><em>ã³ã³ãã¼ãã³ã</em></p> +<p class="original">Catalyst has many prebuilt components and plugins for common modules and tasks. + For example, there are <code>View</code> classes available for Template Toolkit, HTML::Template, + Mason, Petal, and PSP. Plugins are available for dozens of applications and + functions, including <a href="http://search.cpan.org/perldoc?Data::FormValidator">Data::FormValidator</a>, + authentication based on LDAP or <a href="http://search.cpan.org/perldoc?Class::DBI">Class::DBI</a>, several caching + modules, <a href="http://search.cpan.org/perldoc?HTML::FillInForm">HTML::FillInForm</a>, + and XML-RPC.</p> + +<p> +Catalystã«ã¯ãå ±éã®ã¢ã¸ã¥ã¼ã«ã¨ä»äºç¨ã«ããããããã³ã³ãã¼ãã³ãããã©ã°ã¤ã³ãããã¾ãã +ä¾ãã°ã<code>View</code>ã¯ã©ã¹ã«ã¯Template ToolkitãHTML::TemplateãMasonãPetalãPSPãªã©ãå©ç¨ã§ãã¾ãã +ãã©ã°ã¤ã³ã§ä½åãã®ã¢ããªã±ã¼ã·ã§ã³ã¨æ©è½ã使ãã¾ãã +<a href="http://search.cpan.org/perldoc?Data::FormValidator">Data::FormValidator</a>ã +LDAPãã¼ã¹ã®èªè¨¼ã¨ã<a href="http://search.cpan.org/perldoc?Class::DBI">Class::DBI</a>ã +è¤æ°ã®ãã£ãã·ã³ã°ã¢ã¸ã¥ã¼ã«, <a href="http://search.cpan.org/perldoc?HTML::FillInForm">HTML::FillInForm</a>ã +XML-RPCãªã©ãå«ã¿ã¾ãã +</p> + +<p class="original">Catalyst supports component auto-discovery; if you put a component in the + correct place, Catalyst will find and load it automagically. Just place a Catalog + controller in <em>/AppName/Controller/Catalog.pm</em> (or, in practice, in the + shortened <em>/AppName/C/Catalog.pm</em>); there's no need to <code>use</code> each + item. You can also declare plugins in the application class with short names, + so that:</p> + +<p>Catalystã¯ãã³ã³ãã¼ãã³ãã®ãªã¼ããã£ã¹ã«ããªã¼ããµãã¼ããã¾ã; +ã³ã³ãã¼ãã³ããæ£ããå ´æã«ç½®ãã°ãCatalystã¯ãèªåçã«ãããè¦ä»ãã¦ãèªã¿è¾¼ã¿ã¾ãã +<em>/AppName/Controller/Catalog.pm</em>(ã¾ãã¯ãå®éã«ã¯ãç¥ã㦠<em>/AppName/C/Catalog.pm</em>) +ã«ãã«ã¿ãã°ã³ã³ããã¼ã©ãç½®ãã°ããã ãã§ã; +ããããã®ãã®ã<code>use</code>ããå¿ è¦ã¯ããã¾ããã +ã¾ããçãååã§ã¢ããªã±ã¼ã·ã§ã³ã¯ã©ã¹ã®ãã©ã°ã¤ã³ã宣è¨ã§ãã¾ãã +次ã®ããã«ãã¾ã: +</p> + +<pre class=code><code>use Catalyst qw/Email Prototype Textile/;</code></pre> +<p>will load <code>Catalyst::Plugin::Email</code>, <code>Catalyst::Plugin::Prototype</code>, + and <code>Catalyst::Plugin::Textile</code> in one shot.</p> + +<p><code>Catalyst::Plugin::Email</code>, <code>Catalyst::Plugin::Prototype</code>, + and <code>Catalyst::Plugin::Textile</code>ãä¸åã§ãã¼ããã¾ãã</p> + +<p><em>éçº</em></p> +<p class="original">Catalyst comes with a built-in lightweight HTTP server for development purposes. + This runs on any platform; you can quickly restart it to reload any changes. + This server functions similarly to production-level servers, so you can use + it throughout the testing process--or longer; it's a great choice if you want + to deliver a self-contained desktop application. Scalability is simple, though: + when you want to move on, it is trivial to switch the engine to use plain CGI, + <code>mod_perl1</code>, <code>mod_perl2</code>, FastCGI, or even the Zeus web server.</p> + +<p>Catalystã¯éçºæ段ã«ã©ã¤ãã¦ã§ã¤ããªHTTPãµã¼ããçµã¿è¾¼ã¾ãã¦ãã¾ãã +ãã®ãµã¼ãã¯ã©ããªãã©ãããã©ã¼ã ã§ãåãã¾ã; ãªãããã®å¤æ´ããªãã¼ãããããã«ã +ããã«ãªã¹ã¿ã¼ãã§ãã¾ã(訳註:ææ°çã¯å¤æ´ãèªåã§èªã¿åããèªã¿ç´ãã¾ãã®ã§ããªã¹ã¿ã¼ãããªãã¦ããã§ã)ã +ãã®ãµã¼ãã®æ©è½ã¯è£½åã¬ãã«ã®ãµã¼ãã«è¿ãã§ãã +ããããã¹ãã®ããã»ã¹ã§ä½¿ãã¾ãããã¾ãããã£ã¨é·ã使ããã¨ãã§ãã¾ã; +å èµã¿ã¤ãã®ãã¹ã¯ãããã¢ããªã±ã¼ã·ã§ã³ãä¾çµ¦ãããã¨ããããªãããã®ãµã¼ãã使ãã®ã¯è¯ãé¸æã§ãããã +ã¨ã¯ãããæ¡å¼µããã®ã¯ç°¡åã§ãã: +移è¡ããããªã£ã¦ãããã ã®CGIã使ãã¨ã³ã¸ã³ã<code>mod_perl1</code>ã <code>mod_perl2</code>ã FastCGIãZeus web server ã§ãã +å¤æ´ããã®ã¯ãåãã«è¶³ããªããã¨ã§ãã</p> + +<p class="original">Debugging (Figure 1) and logging (Figure 2) support is also built-in. With + debugging enabled, Catalyst sends very detailed reports to the error log, including + summaries of the loaded components, fine-grained timing of each action and + request, argument listings for requests, and more. Logging works by using the + the <code>Catalyst::Log</code> class; you can log any action for debugging + or information purposes by adding lines like:</p> + +<p>ãããã®ã³ã° (Figure 1) 㨠ãã®ã³ã° (Figure 2) ãµãã¼ããã¾ãçµã¿è¾¼ã¾ãã¦ãã¾ãã +ãããã®ã³ã°ãæå¹ã«ããã¨ãCatalystã¯ãã¨ã©ã¼ãã°ã«ã¨ã¦ã詳細ãªã¬ãã¼ããéãã¾ãã +ãã¼ããããã³ã³ãã¼ãã³ãã®ãµããªããããããã®ã¢ã¯ã·ã§ã³ã¨ãªã¯ã¨ã¹ãã®ãããã¾ããã¿ã¤ãã³ã°ã +ãªã¯ã¨ã¹ãã®å¼æ°ã®ãªã¹ãããã®ä»å¤ãã®ãã®ãå«ã¿ã¾ãã +<code>Catalyst::Log</code> ã¯ã©ã¹ã§ãã®ã³ã°ãåããã¦ãã¾ã; +ãããã®ã³ã°ã®ãããç®çã®æ å ±ãå¾ãããã«ã以ä¸ã®ãããªè¡ã追å ãããã¨ã§ãã©ããªã¢ã¯ã·ã§ã³ãè¨é²ã§ãã¾ãã + +<pre class=code><code>$c->log->info("We made it past the for loop"); +$c->log->debug( $sql_query );</code></pre> +<p><img src="http://www.perl.com/2005/06/02/graphics/log-screenshot.gif" alt="Log screenshot" height="411" width="515"><br> + <em>Figure 1. Logging</em></p> +<p class="original">Crashes will display a flashy debug screen showing details of relevant data + structures, software and OS versions, and the line numbers of errors.</p> + +<p>ã¯ã©ãã·ã¥ããã¨ãé¢é£ãããã¼ã¿æ§é ãã½ããã¦ã§ã¢ãOSã®ãã¼ã¸ã§ã³ãã¨ã©ã¼ã®è¡æ°ããæ´¾æãªãããã°ã¹ã¯ãªã¼ã³ã«è¡¨ç¤ºãã¾ãã</p> + +<p><img src="http://www.perl.com/2005/06/02/graphics/debug-screenshot.gif" alt="Debug screenshot" height="376" width="515"><br> + <em>Figure 2. Debugging</em></p> +<p class="original">Helper scripts, generated with Template Toolkit, are available for the main + application and most components. These allow you to quickly generate starter code (including + basic unit tests) for the application framework. With a single line, + you can create a <code>Model</code> class based on <code>Class::DBI</code> that pulls in + the appropriate Catalyst base model class, sets up the pattern for the CDBI + configuration hash, and generates a <code>perldoc</code> skeleton.</p> + +<p> +ãã«ãã¼ã¹ã¯ãªãã - Template Toolkitã§çæããã - ã¯ãã¡ã¤ã³ã®ã¢ããªã±ã¼ã·ã§ã³ã¨ã»ã¨ãã©ã®ã³ã³ãã¼ãã³ãã§å©ç¨ã§ãã¾ãã +ãã«ãã¼ã¹ã¯ãªããã§ãã¢ããªã±ã¼ã·ã§ã³ãã¬ã¼ã ã¯ã¼ã¯ã®æåã®ã³ã¼ããããã«çæã§ãã¾ã(åºæ¬çãªã¦ããããã¹ããå«ã¿ã¾ã)ã +ä¸è¡ã§ãé©åãªCatalystã®ãã¼ã¹Modelã¯ã©ã¹ãå¼ãå ¥ããã +<code>Class::DBI</code> ããã¼ã¹ã¨ãã <code>Model</code> ã¯ã©ã¹ãä½ãã¾ãã +CDBIã®è¨å®ããã·ã¥ãã»ããã¢ãããã<code>perldoc</code>ã¹ã±ã«ãã³ãä½ãã¾ãã +</p> + +<div class="back"> +<a href="#top">â² to Top</a> +</div> + +<a name="6"> </a> +<h3>æè»æ§</h3> +<p class="original"> +Catalyst allows you to use multiple models, views, and controllers--not just +as an option when setting up an application, but as a totally flexible part of +an application's flow. You can mix and match different elements within the same +application or even within the same method. Want to use <code>Class::DBI</code> for +your database storage and LDAP for authentication? You can have two models. Want +to use Template Toolkit for web display and <a href="http://search.cpan.org/perldoc?PDF::Template">PDF::Template</a> for print +output? No problem. Catalyst uses a simple building-block approach to its add-ins: +if you want to use a component, you say so, and if you don't say so, Catalyst +won't use it. With so many components and plugins available, based on CPAN modules, +it's easy to use what you want, but you don't have to use something you don't +need. Catalyst features advanced URL-to-action dispatching. There are multiple +ways to map a URL to an action (that is, a Catalyst method), depending on your +requirements. First, there is literal dispatching, which will match a specific +path: +</p> +<p> +Catalyst ã¯è¤æ°ã®ModelãViewã Controller ãæ±ãã¾ã -- +ã¢ããªã±ã¼ã·ã§ã³ã®ã»ããã¢ããæã®ãªãã·ã§ã³ã¨ãã¦ã ãã§ãªããã¢ããªã±ã¼ã·ã§ã³ã®ããã¼ã®å ¨ä½çã«æè»ãªé¨åã¨ãã¦ãã +åãã¢ããªã±ã¼ã·ã§ã³å ãåãã¡ã½ããå ã«ãéã£ãè¦ç´ ãããã¯ã¹ãããããããã¨ãã§ãã¾ãã +ãã¼ã¿ãã¼ã¹ã¹ãã¬ã¼ã¸ã«<code>Class::DBI</code>ã¨ãèªè¨¼ã®ããã«ãLDAPã使ãããï¼ +両æ¹ã®ã¢ãã«ã使ãã¾ããWebã®è¡¨ç¤ºã«Template Toolkitã使ããããªã³ãç¨ã®åºåã« +<a href="http://search.cpan.org/perldoc?PDF::Template">PDF::Template</a>ã使ãããï¼ +ä½ã®åé¡ãããã¾ããã +Catalystã¯ãç°¡åãªbuilding-blockã¢ããã¼ããCatalystã®ã¢ãã¤ã³ã«ä½¿ã£ã¦ãã¾ã: +ã³ã³ãã¼ãã³ãã使ããããã°ãããè¨ãã°ãããã¾ããããè¨ããªããã°ã +Catalystã¯ä½¿ããã¨ãã¾ãããå©ç¨å¯è½ãªããããã®ã³ã³ãã¼ãã³ãã¨ãã©ã°ã¤ã³ - +CPANã¢ã¸ã¥ã¼ã«ããã¼ã¹ã¨ãã¦ãã - ã§ãç°¡åã«æããã®ã使ããã¨ãã§ãã¾ãã +ã§ãããå¿ è¦ã§ãªããã®ã使ãå¿ è¦ã¯ããã¾ããã + +Catalyst features advanced URL-to-action dispatching. There are multiple +ways to map a URL to an action (that is, a Catalyst method), depending on your +requirements. First, there is literal dispatching, which will match a specific +path: + +Catalystã¯ãå é²ã® URL-to-action ãã£ã¹ããããåãã¦ãã¾ãã +URLã¨ã¢ã¯ã·ã§ã³ã®ãããã³ã°ã«ã¯è¤æ°ã®ãããããããã¾ã(that is, a Catalyst method)ã +ããªããã©ãããããã«ä¾ãã¾ããã¾ããæåã©ããã®ãã£ã¹ããããç¹å®ã®ãã¹ã«ããããããã®ã¯: + +<pre class=code><code>package MyApp::C::Quux; + +# http://localhost:3000/foo/bar/yada ã ãã«ããã +sub baz : Path('foo/bar/yada') { }</code></pre> + +<p class="original">A top-level, or global, dispatch matches the method name directly at the application + base:</p> + +<p>ãããã¬ãã«ãã¾ãã¯ãã°ãã¼ãã«ã¯ãã¢ããªã±ã¼ã·ã§ã³ãã¼ã¹ã§ã¡ã½ããåã«ç´æ¥ããããã¾ã:</p> + +<pre class=code><code>package MyApp::C::Foo; + +# http://localhost:3000/bar ã«ã®ã¿ããã +sub bar : Global { }</code></pre> + +<p class="original">A local, or namespace-prefixed, dispatch acts only in the namespace derived + from the name of your Controller class:</p> + +<p>ãã¼ã«ã«ãã¾ãã¯ãnamespace-prefixedã®ãã£ã¹ãããã¯ã +Controllerã¯ã©ã¹ã®ååããç±æ¥ããåå空éã§ã®ã¿åãã¾ãã +</p> + +<pre class=code><code>package MyApp::C::Catalog::Product; + +# http://localhost:3000/catalog/product/buy ã«ããã +sub buy : Local { } + +package MyApp::C::Catalog::Order; + +# http://localhost:3000/catalog/order/review ã«ããã +sub review : Local { }</code></pre> + +<p class="original">The most flexible is a regex dispatch, which acts on a URL that matches the + pattern in the key. If you use capturing parentheses, the matched values are + available in the <code>$c->request->snippets</code> array.</p> + +<p>ä¸çªæè»æ§ãé«ãã®ã¯ãæ£è¦è¡¨ç¾ã«ãããã£ã¹ãããã§ãã +ãã¼ã®ãã¿ã¼ã³ã«ãããããURLã§åãã¾ãã + +ãã¼ã¬ã³ããã£ããã£ãªã³ã°ããã®ã«ä½¿ã(訳注: /([a-z]+)(\d+)/ ããã$1, $2ãªã© ã使ã)ãªãã +ãããããå¤ã¯ã<code>$c->request->snippets</code>ã®é åããå©ç¨ã§ãã¾ãã +</p> + +<pre class=code><code>package MyApp::C::Catalog; + +# http://localhost:3000/item23/order189 ã«ããã +sub bar : Regex('^item(\d+)/order(\d+)$') { + my ( $self, $c ) = @_; + my $item_number = $c->request->snippets->[0]; + my $order_number = $c->request->snippets->[1]; + # ... +}</code></pre> + +<p class="original">The regex will act globally; if you want it to act only on a namespace, use + the name of the namespace in the body of the regex:</p> + +<p>æ£è¦è¡¨ç¾ã¯ã°ãã¼ãã«ã«åãã¾ã; åå空éã§ã®ã¿åãããããã°ãæ£è¦è¡¨ç¾ã«åå空éã®ååã使ã£ã¦ãã ãã:</p> + +<pre class=code><code>sub foo : Regex('^catalog/item(\d+)$') { # ...</code></pre> + +<p class="original">Finally, you can have private methods, which are never available through URLs. + You can only reach them from within the application, with a namespace-prefixed + path:</p> + +<p>æå¾ã«ããã©ã¤ãã¼ãã¡ã½ãããæã¤ãã¨ãã§ãã¾ããããã¯ãURLã«ã¯æ±ºãã¦ä½¿ããã¾ããã +ã¢ããªã±ã¼ã·ã§ã³å ããã®ã¿ãnamespace-prefixed ãªãã¹ã§ä½¿ããã¨ãã§ãã¾ã: +</p> + +<pre class=code><code>package MyApp::C::Foo; +# matches nothing, and is only available via $c->forward('/foo/bar'). +sub bar : Private { }</code></pre> + +<p class="original">A single <code>Context</code> object (<code>$context</code>, or more usually as its alias <code>$c</code>) + is available throughout the application, and is the primary way of interacting + with other elements. Through this object, you can access the request object + (<code>$c->request->params</code> will return or set parameters, <code>$c->request->cookies</code> will + return or set cookies), share data among components, and control the flow of + your application. A response object contains response-specific information + (<code>$c->response->status(404)</code>) and the <code>Catalyst::Log</code> class + is made directly available, as shown above. The <code>stash</code> is a universal hash + for sharing data among application components:</p> + +<p>1ã¤ã®<code>Context</code> ãªãã¸ã§ã¯ã(<code>$context</code>, ã¾ãã¯ã æ®é㯠<code>$c</code>) +ãã¢ããªã±ã¼ã·ã§ã³ãéãã¦ä½¿ç¨ã§ãã¾ãã主ã«ä»ã®è¦ç´ ã«ç¸äºã«ä½ç¨ããããã«ä½¿ãã¾ãã +ãã®ãªãã¸ã§ã¯ããéãã¦ããªã¯ã¨ã¹ããªãã¸ã§ã¯ã(<code>$c->request->params</code> ããã©ã¡ã¼ã¿ãè¿ã/ã»ãããã¾ãã +<code>$c->request->cookies</code> ãã¯ããã¼ãè¿ã/ã»ãããã¾ã)ã +ã³ã³ãã¼ãã³ãéã§ãã¼ã¿ãå ±æããã¢ããªã±ã¼ã·ã§ã³ã®ããã¼ãã³ã³ããã¼ã«ãã¾ãã +ã¬ã¹ãã³ã¹ãªãã¸ã§ã¯ãã¯ãã¬ã¹ãã³ã¹ã«ç¹åããæ å ±ãå«ã¿ã¾ã +(<code>$c->response->status(404)</code>)ãã¾ããæ¢ã«è¦ãã<code>Catalyst::Log</code> ã¯ã©ã¹ +ãç´æ¥ã«å©ç¨å¯è½ã§ã. <code>stash</code> ã¯ãã¦ããã¼ãµã«ãªããã·ã¥ã§ã +ã¢ããªã±ã¼ã·ã§ã³ã®ã³ã³ãã¼ãã³ãã§ãã¼ã¿ãå ±æããããã®ãã®ã§ã:</p> + +<pre class=code><code>$c->stash->{error_message} = "You must select an entry"; + +# TT ã®ãã³ãã¬ã¼ãã§: +[% IF error_message %] + <h3>[% error_message %]</h3> +[% END %]</code></pre> + +<p class="original">Stash values go directly into the templates, but the entire context object + is also available:</p> + +<p>Stash ã®å¤ã¯ãã³ãã¬ã¼ãã«ç´æ¥ã«å ¥ãã¾ãã ã§ãããå ¨ã¦ã®ã³ã³ããã¹ããªãã¸ã§ã¯ããã¾ãå©ç¨å¯è½ã§ã:</p> + +<pre class=code><code><h1>[% c.config.name %]</h1></code></pre> + +<p class="original">To show a Mason example, if you want to use <code>Catalyst::View::Mason</code>:</p> + +<p>Masonã®ä¾ãè¦ãããã«, <code>Catalyst::View::Mason</code>ã欲ãããã°:</p> + +<pre class=code><code>% foreach my $k (keys $c->req->params) { + param: <% $k %>: value: <% $c->req->params->{$k} %> +% }</code></pre> + +<div class="back"> +<a href="#top">â² to Top</a> +</div> + +<a name="7"> </a> +<h2>ãµã³ãã«ã¢ããªã±ã¼ã·ã§ã³: 30è¡ã®ã³ã¼ãã§ãMiniMojo, Ajax-Based Wiki</h2> + +<p class="original">Now that you have a sense of what Catalyst is, it's time to look at what it +can do. The example application is MiniMojo, a wiki based on <a href="http://www.adaptivepath.com/publications/essays/archives/000385.php">Ajax</a>, +which is a JavaScript framework that uses the <code>XMLHttpRequest</code> object to create +highly dynamic web pages without needing to send full pages back and forth +between the server and client.</p> + +<p>ãããCatalystãä½ããããã¾ããããä½ãã§ããããè¦ã¦ã¿ã¾ãããã +ã¢ããªã±ã¼ã·ã§ã³ä¾ã¯ãMiniMojoã§ãã<a href="http://www.adaptivepath.com/publications/essays/archives/000385.php">Ajax</a>ã®Wikiã§ãã +<code>XMLHttpRequest</code>ã使ã£ãJavaScriptã®ãã¬ã¼ã ã¯ã¼ã¯ã§ããµã¼ããã¯ã©ã¤ã¢ã³ãéã§åå¾ã«ãã¼ã¸ãéãå¿ è¦ã®ãªãã +é«åº¦ã«åçãªWebãã¼ã¸ãä½ãã¾ãã</p> + +<p class="original">Remember that from the Catalyst perspective, Ajax is just a case of sending +more text to the browser, except that this text is in the form of client-side +JavaScript that talks to the server, rather than a boilerplate copyright notice +or a navigation sidebar. It makes no difference to Catalyst.</p> + +<p> +Catalystã®è¦³ç¹ãããAjaxããå½é²çå ±éå¥ç´æ¸ç権æ å ±ã¨ãããã²ã¼ã·ã§ã³ãµã¤ããã¼ã¨ãã§ã¯ãªãã +ãµã¼ãã¨éä¿¡ããã¯ã©ã¤ã¢ã³ããµã¤ãJavaScriptã®ãã©ã¼ã ã«ããã¹ãããããã¨ãé¤ãã¦ã +ããå¤ãã®ããã¹ãããã©ã¦ã¶ã«éã1ã±ã¼ã¹ã§ãããã¨ãè¦ãã¦ãã¦ãã ããã ãã®ãã¨ã¯Catalystã«éè¦ã§ã¯ããã¾ããã +</p> + +<div class="back"> +<a href="#top">â² to Top</a> +</div> + +<a name="8"> </a> +<h3>ã¤ã³ã¹ãã¼ã«</h3> + +<p class="original">Catalyst has a relatively large number of requirements; most, however, are +easy to install, along with their dependencies, from CPAN. The following list +should take care of everything you need for this project:</p> + +<p>Catalyst å¿ è¦ãªã¢ã¸ã¥ã¼ã«ãæ¯è¼çããããããã¾ã; ã§ããã +ã»ã¨ãã©ã¯ç°¡åã«CPANããä¾åæ§ã«æ²¿ã£ã¦ã¤ã³ã¹ãã¼ã«ã§ãã¾ãã +ä¸ã®ãªã¹ãã§ããã®ããã¸ã§ã¯ãã«å¿ è¦ãªãã®ãæãã¾ãã +</p> + +<ul> +<li><a href="http://search.cpan.org/perldoc?Catalyst">Catalyst</a></li> +<li><a href="http://search.cpan.org/perldoc?Catalyst::Model::CDBI">Catalyst::Model::CDBI</a></li> +<li><a href="http://search.cpan.org/perldoc?Class::DBI::SQLite">Class::DBI::SQLite</a></li> +<li><a href="http://search.cpan.org/perldoc?Catalyst::View::TT">Catalyst::View::TT</a> </li> +<li><a href="http://search.cpan.org/perldoc?Catalyst::Plugin::Textile">Catalyst::Plugin::Textile</a> </li> +<li><a href="http://search.cpan.org/perldoc?Catalyst::Plugin::Prototype">Catalyst::Plugin::Prototype</a></li> +<li><a href="http://www.sqlite.org/">SQLite</a> (ãã¤ããªãPerlã¢ã¸ã¥ã¼ã«ã§ã¯ããã¾ãã)</li> +</ul> + +<div class="back"> +<a href="#top">â² to Top</a> +</div> + +<a name="9"> </a> +<h3>ã¢ããªã±ã¼ã·ã§ã³ã®ã¹ã±ã«ãã³ã®ä½æ</h3> + +<p class="original">Run this command:</p> + +<p>ãã®ã³ãã³ããå®è¡:</p> + +<pre class=code><code>$ catalyst.pl MiniMojo +$ cd MiniMojo</code></pre> + +<p class="original">You've just created the skeleton for your entire application, complete with +a helper script keyed to MiniMojo to generate individual classes, basic test +scripts, and more.</p> + +<p>ã¢ããªã±ã¼ã·ã§ã³å ¨ä½ã®ã¹ã±ã«ãã³ãã§ããããã¾ããã +ããããã®ã¯ã©ã¹ãä½ãããã®ãMiniMojoããã¼ã¨ãããã«ãã¼ã¹ã¯ãªããã +åºæ¬çãªãã¹ãã¹ã¯ãªããããã®ä»å¤ãã®ãã®ãæã£ã¦ãã¾ãã</p> + +<p class="original">Run the built-in server:</p> + +<p>ãã«ãã¤ã³ãµã¼ãã®èµ·å:</p> + +<pre class=code><code>$ script/minimojo_server.pl</code></pre> + +<p class="original">MiniMojo is already running, though it isn't doing much +just yet. (You should have received a web page consisting solely +of the text "Congratulations, MiniMojo is on Catalyst!") Press +<code>Ctrl</code>-<code>C</code> to stop the server.</p> + +<p>MiniMojo ã¯ããåãã¦ãã¾ããã§ãããã¾ã å¤ãã®ãã¨ã¯ãã¾ããã +(åã«"Congratulations, MiniMojo is on Catalyst!"ã¨ããWebãã¼ã¸ã ãã§ã) +<code>Ctrl</code>-<code>C</code>ãæ¼ãã¦ããµã¼ããæ¢ãã¾ãããã</p> + +<div class="back"> +<a href="#top">â² to Top</a> +</div> + +<a name="10"> </a> +<h3>Application Classã«åºæ¬çãªã¡ã½ããã追å </h3> + +<p class="original">Add a private <code>end</code> action to your application class, +<em>lib/MiniMojo.pm</em>, by editing the new file:</p> + +<p>ãã©ã¤ãã¼ããª<code>end</code> ã¢ã¯ã·ã§ã³ãã¢ããªã±ã¼ã·ã§ã³ã¯ã©ã¹ã<em>lib/MiniMojo.pm</em>ã«è¿½å ãã¾ãã +ãã®æ°ãããã¡ã¤ã«ãç·¨éãã¾ã:</p> + +<pre class=code><code>sub end : Private { + my ( $self, $c ) = @_; + $c->forward('MiniMojo::V::TT') unless $c->res->output; +}</code></pre> + +<p class="original">Catalyst automatically calls the <code>end</code> action at the end of a +request cycle. It's one of four built-in Private actions. It's a typical +pattern in Catalyst to use <code>end</code> to forward the application to the +View component for rendering, though if necessary you could do it yourself (for +example, if you want to use different Views in the same application--perhaps +one to generate web pages with Template Toolkit and another to generate PDFs +with PDF::Template).</p> + +<p>Catalyst ã¯ããªã¯ã¨ã¹ããµã¤ã¯ã«ã®æå¾ã«èªåçã«<code>end</code>ãå¼ã³ã¾ããããã¯ã4ã¤ã®ãã«ãã¤ã³ã¢ã¯ã·ã§ã³ã®1ã¤ã§ãã +ã¬ã³ããªã³ã°ã®ããã«Viewã³ã³ãã¼ãã³ãã«ã¢ããªã±ã¼ã·ã§ã³ããã©ã¼ã¯ã¼ãããã®ã«ã<code>end</code>ã使ãã®ã¯ãCatalystã®å ¸åçãªãã¿ã¼ã³ã§ãã +ã§ãããå¿ è¦ãªããèªåã§ãããè¡ããã¨ãã§ãã¾ã(ä¾ãã°ãåãã¢ããªã±ã¼ã·ã§ã³ã§éãViewã使ããã--ãã¶ããä¸æ¹ã§ Tempalte Toolkit 㧠Webãã¼ã¸ãçæãã +ä»æ¹ã§PDF::Templateã§ãPDFãçæãããããªå ´å)ã +</p> + +<p class="original">Replace the existing, helper-generated <code>default</code> action in the same class with:</p> + +<p>ãã«ãã¼ã¹ã¯ãªãããä½ã£ã ã¢ããªã±ã¼ã·ã§ã³ã¯ã©ã¹ã®<code>default</code>ã¢ã¯ã·ã§ã³ãç½®ãæãã¾ã:</p> + +<pre class=code><code>sub default : Private { + my ( $self, $c ) = @_; + $c->forward('/page/show'); +}</code></pre> + +<p class="original">In case the client has specified no other appropriate action, this will +forward on to the page controller's <code>show</code> method. As Private +actions, nothing can call these from outside the application. Any method from +within the application can call them. The <code>default</code> action is +another built-in Private action, along with <code>begin</code>, +<code>auto</code>, and <code>end</code>. Again, Catalyst calls them +automatically at relevant points in the request cycle.</p> + +<p> +ã¯ã©ã¤ã¢ã³ããä»ã®é©å½ãªã¢ã¯ã·ã§ã³ãä½ãæå®ããªããã°ã +ãã¼ã¸ã³ã³ããã¼ã©ã®<code>show</code>ã¡ã½ããã«ãã©ã¼ã¯ã¼ããã¾ãã +ãã©ã¤ãã¼ãã¢ã¯ã·ã§ã³ã¯ãã¢ããªã±ã¼ã·ã§ã³ã®å¤å´ããå¼ã³åºããã¨ã¯ã§ãã¾ããã +ã¢ããªã±ã¼ã·ã§ã³ãªãã®åº¦ã®ã¡ã½ãããããããå¼ã¶ãã¨ãã§ãã¾ãã +<code>default</code>ã¢ã¯ã·ã§ã³ã¯ã<code>begin</code>ã<code>auto</code>ã<code>end</code>ã¨åæ§ã«ã +å¥ã®ãã©ã¤ãã¼ãã¢ã¯ã·ã§ã³ã§ãã +Catalystã¯ããªã¯ã¨ã¹ããµã¤ã¯ã«ã§é¢é£ã®ããç®æã§èªåçã«ããããå¼ã³åºãã¾ãã +</p> + +<div class="back"> +<a href="#top">â² to Top</a> +</div> + +<a name="11"> </a> +<h3>Model(SQLite ãã¼ã¿ãã¼ã¹)ã®ã»ããã¢ããã¨ãã«ãã¼ã¹ã¯ãªããã«ããModelã¯ã©ã¹ã®çæ</h3> + +<p class="original">Next, create a file, <em>minimojo.sql</em>, that contains the SQL for setting +up your <code>page</code> table in SQLite.</p> + +<p>次ã«ã<em>minimojo.sql</em>ã¨ãããã¡ã¤ã«ãä½ãã¾ãã +SQLiteã«<code>page</code>ãã¼ãã«ãã»ããã¢ããããSQLã§ãã</p> + +<pre class=code><code>-- minimojo.sql +CREATE TABLE page ( + id INTEGER PRIMARY KEY, + title TEXT, + body TEXT +);</code></pre> + +<p class="original">Create a database from it, using the <code>sqlite</code> command-line +program:</p> + +<p>ããããããã¼ã¿ãã¼ã¹ãä½æãã¾ãã<code>sqlite</code>ã³ãã³ãã©ã¤ã³ããã°ã©ã ã使ãã¾ã:</p> + +<pre class=code><code>$ sqlite minimojo.db < minimojo.sql</code></pre> + +<p class="original">Depending on your setup, it might be necessary to call this as <code>sqlite3</code>.</p> + +<p>ã»ããã¢ããã«ãã£ã¦ã <code>sqlite3</code>ã¨ãã¦ãã³ãã³ããå¼ã³åºãå¿ è¦ãããããããã¾ãã.</p> + +<p class="original">Use the helper to create model classes and basic unit tests (Figure 3 shows the results):</p> + +<p>ãã«ãã¼ã¹ã¯ãªããã§Modelã¯ã©ã¹ã¨åºæ¬ã®ã¦ããããã¹ããä½ãã¾ã(Figure 3ã«çµæãããã¾ã):</p> + +<pre class=code><code>$ script/minimojo_create.pl model CDBI CDBI dbi:SQLite:/path/to/minimojo.db</code></pre> + +<p><img src="http://www.perl.com/2005/06/02/graphics/model-create-screenshot.gif" alt="Model-creation screenshot" height="372" width="515"><br><em>Figure 3. Creating the model</em></p> + +<p class="original">The <em>minimojo_create.pl</em> script is a helper that uses Template +Toolkit to automate the creation of particular modules. The previous command +creates a model (in contrast to a controller or a view) called +<em>CDBI.pm</em>, using the CDBI helper, setting the connection string to +<code>dbi:SQLite:/path/to/minimojo.db</code>, the database you just created. (Use the appropriate path for +your system.) The helper will write the models into <em>lib/MiniMojo/M/</em>. +There are various options for the helper scripts; the only requirement is the +type and the name. (You can create your own modules from scratch, without using +the helper.)</p> + +<p>The <em>minimojo_create.pl</em> ã¹ã¯ãªããã¯Template Toolkitã使ã£ããã«ãã¼ã§ã +ç¹å®ã®ã¢ã¸ã¥ã¼ã«ã®ä½æãèªååãã¾ããåã®ã³ãã³ãã¯(ControllerãViewã¨ã§ã¯ãªã)ã<em>CDBI.pm</em>ã¨å¼ã°ããModelãä½ãã¾ãã +CDBIãã«ãã¼ã使ãã<code>dbi:SQLite:/path/to/minimojo.db</code>ã®ãå ã»ã©ä½ã£ããã¼ã¿ãã¼ã¹ã¸ã®æ¥ç¶æååãã»ãããã¾ãã +(èªåã®ã·ã¹ãã ã«åã£ããã¹ã使ã£ã¦ãã ãã)ã +ãã«ãã¼ã¯ã <em>lib/MiniMojo/M/</em>ã«Modelãæ¸ãã¾ãã +ãã«ãã¼ã¹ã¯ãªããã«ã¯ãã¾ãã¾ãªãªãã·ã§ã³ãããã¾ã; +å¿ é ã¯ã¿ã¤ãã¨ååã ãã§ãã(ãã«ãã¼ã使ããã«ãã¹ã¯ã©ããããèªåã®ã¢ã¸ã¥ã¼ã«ãä½ããã¨ãã§ãã¾ã)</p> + +<div class="back"> +<a href="#top">â² to Top/a> +</div> + +<a name="12"> </a> +<h3>Viewã®ã»ããã¢ãã(<code>Template::Toolkit</code>) ã¨ãã«ãã¼ã¹ã¯ãªããã«ããViewã¯ã©ã¹ã®ä½æ</h3> + +<p>ãã«ãã¼ã¹ã¯ãªããã«ããViewã¯ã©ã¹ã®ä½æ:</p> + +<pre class=code><code>$ script/minimojo_create.pl view TT TT</code></pre> + +<p class="original">View classes go into <em>lib/MiniMojo/V/</em>.</p> + +<p>Viewã¯ã©ã¹ã¯<em>lib/MiniMojo/V/</em>ã«ã§ããããã¾ãã</p> + +<p>ãã«ãã¼ã¹ã¯ãªããã«ããControllerã¯ã©ã¹ã®ä½æ:</p> + +<p class="original">Create a controller class called <code>Page</code> with the helper:</p> + +<p>ãã«ãã¼ã¹ã¯ãªããã«ãã<code>Page</code>Controllerã¯ã©ã¹ãä½ãã¾ã:</p> + +<pre class=code><code>$ script/minimojo_create.pl controller Page</code></pre> + +<p class="original">Controller classes live in <em>lib/MiniMojo/C/</em>.</p> + +<p><em>lib/MiniMojo/C/</em>ã«Controllerã¯ã©ã¹ãå ¥ãã¾ãã</p> + +<p class="original">Add a <code>show</code> action to <em>lib/MiniMojo/C/Page.pm</em>:</p> + +<p><em>lib/MiniMojo/C/Page.pm</em>ã«<code>show</code>ã¢ã¯ã·ã§ã³ã追å ãã¾ã:</p> + +<pre class=code><code>sub show : Regex('^(\w+)\.html$') { + my ( $self, $c ) = @_; + $c->stash->{template} = 'view.tt'; + # $c->forward('page'); +}</code></pre> + +<p class="original">The <code>Regex</code> dispatch matches a page in <i><code>foo</code>.html</i>, where +<code>foo</code> is any sequence of word characters. This sequence is available +in the <code>$context->request->snippets</code> array, where the <code>page</code> +action uses it to display an existing page or to create a new one. The rest of +this action sets the appropriate template and sends the application to the +<code>page</code> action. (Leave the <code>forward</code> command commented out until you have written the <code>page</code> action.)</p> + +<p><code>Regex</code>ãã£ã¹ãããã<i><code>foo</code>.html</i>ã®ãã¼ã¸ã«ããããã¾ã +(<code>foo</code>ã¯ãã©ããªé çªã®åèªã®æååé åã§ãæ§ãã¾ãã)ã +ãã®é çªã¯<code>$context->request->snippets</code> é åããå©ç¨ã§ãã¾ãã +<code>page</code>ã¢ã¯ã·ã§ã³ã¯åå¨ãããã¼ã¸ã表示ããããæ°ãããã®ãä½ãã®ã«ä½¿ããã¾ãã +ãã®ã¢ã¯ã·ã§ã³ã®æ®ãã§ã¯ãé©å½ãªãã³ãã¬ã¼ããã»ããããã¢ããªã±ã¼ã·ã§ã³ã«ã +<code>page</code>ã¢ã¯ã·ã§ã³ãéãã¾ãã(<code>page</code>ã¢ã¯ã·ã§ã³ãæ¸ãã¾ã§ã +<code>forward</code>ãã³ã¡ã³ãããã¾ã¾ã«ãã¦ããã¾ã)</p> + +<p class="original">Restart the server with <code>$ script/minimojo_server.pl</code> and point a web browser to <i>http://localhost:3000/show/</i> to see +the debug screen (you don't yet have the template that <code>show</code> is +trying to send people to).</p> + +<p>ãµã¼ãã <code>$ script/minimojo_server.pl</code> ã§ããªã¹ã¿ã¼ããã<i>http://localhost:3000/show/</i> +ãWebãã©ã¦ã¶ã¼ã§ã¿ãã¨ããããã°ã¹ã¯ãªã¼ã³ã表示ããã¾ã +(<code>show</code> ã使ããã¨ãããã³ãã¬ã¼ãã¯ãã¾ã ç¨æããã¦ããªãã®ã§)</p> + +<p><em>root/view.tt</em>ãä½ãã¾ã:</p> + +<pre class=code><code><html> + <head><title>MiniMojo</title></head> + <body> + <h1>MiniMojo is set up!</h1> + </body> +</html></code></pre> + +<p class="original">Test again by killing the server with <code>Ctrl</code>-<code>C</code> and restarting it, and go to +<i>http://localhost:3000/show/</i>. You should see the page you just +defined.</p> + +<p> +<code>Ctrl</code>-<code>C</code>ã§ãµã¼ãã殺ãã¦ããªã¹ã¿ã¼ããã +<i>http://localhost:3000/show/</i>ãè¦ã¦ã¿ã¾ããä»ä½ã£ããã¼ã¸ãè¦ãããã¯ãã§ãã +</p> + +<div class="back"> +<a href="#top">â² to Top</a> +</div> + +<a name="13"> </a> +<h3>表示ã¨ç·¨éã®ã³ã¼ãã®è¿½å </h3> + +<p class="original">Modify the application class <em>lib/MiniMojo.pm</em> to include the +<code>Prototype</code> and <code>Textile</code> plugins:</p> + +<p><code>Prototype</code> 㨠<code>Textile</code> ãã©ã°ã¤ã³ã使ãããã«ã +ã¢ããªã±ã¼ã·ã§ã³ã¯ã©ã¹ã®<em>lib/MiniMojo.pm</em>ãå¤æ´ãã¾ããã:</p> + +<pre class=code><code>use Catalyst qw/-Debug Prototype Textile/;</code></pre> + +<p class="original"> +Note that you can use the plugins by specifying their base names; Catalyst +figures out what you mean without making you use <code>Catalyst::Plugin::Prototype</code>. +</p> +<p> +ãã¼ã¹ã®ååãæå®ãããã¨ã§ããã©ã°ã¤ã³ä½¿ããã¨ãã§ããã®ã«æ³¨æãã¦ãã ãã; +Catalystã¯ã<code>Catalyst::Plugin::Prototype</code>ã使ããªãã¦ããæå³ãããã®ã®è¦å½ãä»ãã¾ãã +</p> + +<p class="original">Modify the <code>page</code> controller, <em>lib/MiniMojo/C/Page.pm</em>, to +add page-view and editing code:</p> + +<p><code>page</code> Controllerã <em>lib/MiniMojo/C/Page.pm</em>ãä¿®æ£ãã¾ãã +ãã¼ã¸Viewã¨ãç·¨éã®ã³ã¼ãã追å ãã¾ã:</p> + + +<pre class=code><code>sub page : Private { + my ( $self, $c, $title ) = @_; + $title ||= $c->req->snippets->[0] || 'Frontpage'; + my $query = { title => $title }; + $c->stash->{page} = MiniMojo::M::CDBI::Page->find_or_create($query); +}</code></pre> + +<p class="original">The private <code>page</code> method sets a title--whether passed in to it, +taken from the <code>snippets</code> array (that matches the regex in +<code>show</code>), or defaulting to "Frontpage." The <code>$query</code> +variable holds a hashref used for <code>Class::DBI</code>'s +<code>find_or_create</code> method, seeding the stash for the <code>page</code> +variable with the result of this CDBI query. At the end of the method, control +flow returns to the calling method.</p> + +<p> +ãã©ã¤ãã¼ããª<code>page</code> ã¡ã½ããã§ã¿ã¤ãã«ãã»ãããã¾ã -- +(<code>show</code>å ã®æ£è¦è¡¨ç¾ã«ããããã)<code>snippets</code> é åããåå¾ãããã°ãããã +ããã§ãªããã°ã "Frontpage"ãããã©ã«ãã¨ãã¦ã¿ã¤ãã«ã«ä½¿ããã¾ãã +<code>$query</code>å¤æ°ã¯ã<code>Class::DBI</code>ã®<code>find_or_create</code> ã¡ã½ããã§ä½¿ããã +ããã·ã¥ãªãã¡ã¬ã³ã¹ãæã¡ã¾ããCDBIã¯ã¨ãªã®çµæã¨ä¸ç·ã«<code>page</code>å¤æ°ãåãã¾ãã +ã¡ã½ããã®çµããã«ãã³ã³ããã¼ã«ããã¼ã¯ãå¼ã³åºãã¡ã½ãããè¿ãã¾ãã +</p> + +<p class="original">Now uncomment the <code>$c->forward('page');</code> line in the <code>show</code> action.</p> + +<p><code>show</code>ã¢ã¯ã·ã§ã³ä¸ã®<code>$c->forward('page');</code>ãã³ã¡ã³ããå¤ãã¾ãã</p> + +<pre class=code><code>sub edit : Local { + my ( $self, $c, $title ) = @_; + $c->forward('page'); + $c->stash->{page}->body( $c->req->params->{body} ) + if $c->req->params->{body}; + my $body = $c->stash->{page}->body || 'Just type something...'; + my $html = $c->textile->process($body); + + my $base = $c->req->base; + $html =~ s{(?<![\?\\\/\[])(\b[A-Z][a-z]+[A-Z]\w*)} + {<a href="$base$1.html">$1</a>}g; + + $c->res->output($html); +}</code></pre> + +<p class="original">The <code>edit</code> method first forwards the action off to +<code>page</code>, so that the stash's <code>page</code> object contains the +result of the CDBI query. If there is a value for <code>body</code>, it will +use this; otherwise "Just type something..." is the default. The code then +processes the body with Textile, which converts plain text to HTML, and then runs +the body through a regex to convert camel-case text into links, with the URL +base taken from the Catalyst request object. Finally, it outputs the HTML.</p> + +<p><code>edit</code> ã¡ã½ããã¯ãã¾ã<code>page</code>ã«ã¢ã¯ã·ã§ã³ããã©ã¼ã¯ã¼ããã¾ãã +stashã® <code>page</code>ãªãã¸ã§ã¯ãã¯CDBIã¯ã¨ãªã®çµæãå«ãã§ãã¾ãã +<code>body</code>ã®ããã®å¤ãããã°ãããã使ãã¾ã; +ããã§ãªããã°ã"Just type something..." ãããã©ã«ãã§ãã +ã³ã¼ãã¯ãbodyãTextileã§å¦çãã¾ããããã¯ããã¬ã¤ã³ããã¹ããHTMLã«å¤æãã¾ãã +Catalystãªã¯ã¨ã¹ããªãã¸ã§ã¯ãããåãåã£ãURLãã¼ã¹ã§ã +camel-caseããã¹ãããªã³ã¯ã«å¤æããæ£è¦è¡¨ç¾ãbodyã«éãã¾ãã +æå¾ã«ãHTMLãåºåãã¾ãã</p> + + +<div class="back"> +<a href="#top">â² to Top</a> +</div> + +<a name="14"> </a> +<h3>Ajaxã§Wikiãã»ããã¢ãããã</h3> + +<p class="original">Modify <em>root/view.tt</em> to include Ajax code:</p> + +<p><em>root/view.tt</em> ãå¤æ´ããAjax codeãå«ãã¾ã:</p> + +<pre class=code><code><html> + <head><title>MiniMojo</title></head> + [% c.prototype.define_javascript_functions %] + [% url = base _ 'page/edit/' _ page.title %] + <body Onload="new Ajax.Updater( 'view', '[% url %]' )"> + <h1>[% page.title %]</h1> + <div id="view"></div> + <textarea id="editor" rows="24" cols="80">[% page.body %]</textarea> + [% c.prototype.observe_field( 'editor', { + url => url, + with => "'body='+value", + update => 'view' } + ) %] + </body> +</html></code></pre> + +<p>ãã®è¡:</p> + +<pre class=code><code>[% c.prototype.define_javascript_functions %]</code></pre> + +<p class="original">includes the whole <em>prototype.js</em> library in a <code>script</code> +block. Note that the <code>prototype</code> plugin is available in the context +object.</p> + +<p><code>script</code>ãããã¯ã«ãã¹ã¦ã®<em>prototype.js</em> ãã¤ã³ã¯ã«ã¼ããã¾ãã +<code>prototype</code>ãã³ã³ããã¹ããªãã¸ã§ã¯ãã§ä½¿ãããã¨ã«æ³¨æãã¦ãã ããã</p> + +<p>ã»ã¯ã·ã§ã³</p> + +<pre class=code><code>[% url = base _ 'page/edit/' _ page.title %] +<body Onload="new Ajax.Updater( 'view', '[% url %]' )"> +<h1>[% page.title %]</h1> +<div id="view"></div></code></pre> + +<p class="original">constructs the Ajax URL and updates the view <code>div</code> when loading +the page.</p> + +<p>ãã¼ã¸ããã¼ãããã¨ãã«ãAjax URLãçµã¿ç«ã¦ãviewã<code>div</code>ãã¢ãããã¼ããã¾ãã</p> + +<p>æå¾ã«:</p> + +<pre class=code><code><textarea id="editor" rows="24" cols="80">[% page.body %]</textarea> + [% c.prototype.observe_field( 'editor', { + url => url, + with => "'body='+value", + update => 'view' } + ) %]</code></pre> + +<p>periodically checks the <code>textarea</code> for changes and makes an Ajax +request on demand.</p> + +<p>å®æçã« <code>textarea</code> ããå¤æ´ãããããã§ãã¯ãããã¼ã¢ã³ã§Ajaxãªã¯ã¨ã¹ããä½ãã¾ãã</p> + +<p class="original">That's it! Now you can re-run the server and your wiki is up and running +(Figure 4). To use the wiki, simply start typing in the <code>textarea</code>. +As you type, the wiki will regularly echo your entry above, passing it through +the formatter. When you type something in camel case, it will automatically +create a link you can click to go to the new page.</p> + +<p>ããè¿! ãµã¼ããåèµ·åããã°ãWikiã¯åãã¦ãã¾ã(Figure 4). +Wikiã使ãããã«ã¯ã<code>textarea</code>ã«ã¿ã¤ãããã ãã§ãã +ã¿ã¤ãéãã«ãwikiã¯ãå®æçã«ä¸textareaã®ä¸ã«ããã©ã¼ããã¿ã¼ãéãã¦ãã¨ã³ããªã¼ã表示ãã¾ãã +ãã£ã¡ã«ã±ã¼ã¹ã§ä½ããæ¸ãã°ãèªåçã«ãªã³ã¯ãä½ããã¦ãã¯ãªãã¯ãã¦ãæ°ãããã¼ã¸ã¸è¡ããã¨ãã§ãã¾ãã</p> + +<p><img src="http://www.perl.com/2005/06/02/graphics/running-wiki-screenshot.gif" alt="screenshot of the running wiki" height="376" width="515"><br><em>Figure 4. The running wiki</em></p> + +<p class="original">Enjoy your new Catalyst-powered Ajax wiki!</p> + +<p>Catalyst-powered Ajax wikiã楽ããã§ãã ãã!</p> + +<div class="back"> +<a href="#top">â² to Top</a> +</div> + +<a name="15"> </a> +<h2>ãªã½ã¼ã¹</h2> + +<p class="original">For more information, see the Catalyst documentation, in particular the <a href="http://search.cpan.org/perldoc?Catalyst::Manual::Intro">Catalyst::Manual::Intro +module</a>, which gives a thorough introduction to the framework. There are two +<a href="http://lists.rawmode.org/mailman/listinfo">Catalyst mailing lists</a>, +a general list and a developer list. The best place to discuss Catalyst, +though, is the <i>#catalyst</i> IRC channel at <a href="http://irc.perl.org/">irc.perl.org</a>. The <a href="http://catalyst.perl.org/">Catalyst home page</a> is currently just a +collection of a few links, but we will extend it in the near future.</p> + +<p> +詳ããã¯ãCatalystã®ããã¥ã¡ã³ããè¦ã¦ãã ãããç¹ã«ã<a href="http://search.cpan.org/perldoc?Catalyst::Manual::Intro">Catalyst::Manual::Intro module</a>ã¯ã +ãã¬ã¼ã ã¯ã¼ã¯ã®è©³ç´°ãªã¤ã³ãããã¯ã·ã§ã³ã§ãã +ã¡ã¼ãªã³ã°ãªã¹ãã2ã¤ããã¾ãã<a href="http://lists.rawmode.org/mailman/listinfo">Catalyst mailing lists</a>ã +ä¸è¬åãã®ãã®ã¨ãéçºè åãã®ãã®ã§ããCatalystã«ã¤ãã¦è°è«ããä¸çªã®å ´æã¯ã +<a href="http://irc.perl.org/">irc.perl.org</a>ã«ãããIRCã®<i>#catalyst</i>ãã£ã³ãã«ã§ãã +<a href="http://catalyst.perl.org/">Catalyst home page</a>ã¯ç¾å¨ã¯ãå°ãªããªã³ã¯éã«ãªã£ã¦ãã¾ãã +è¿ãå°æ¥ãæ¡å¼µããã§ãããã +</p> + +<p class="original">Thanks to Catalyst lead developer Sebastian Riedel for help with this +article and, of course, for Catalyst itself.</p> + +<p>Catalystã®ãªã¼ããã£ããããã¼ã® Sebastian Riedelã«ããã®è¨äºãæ¸ãããã«å©ãã¦ãããã¾ãããæè¬ãã¾ãã +ããã¦ããã¡ãããCatalystèªèº«ã«ãã</p> + +<div class="back"> +<a href="#top">â² to Top</a> +</div> + +<a name="16"> </a> +<h2>翻訳ã«ã¤ãã¦</h2> +<p> +<p> +翻訳è : Ktat<br> +é£çµ¡å : ktat.****@gmail***** +</p> +<ul> +<li>2005/11/23 +<ul> +<li>誤訳修æ£&ã¡ãã£ã¨åæç¡è¦ +<li>ä¼ç¤¾ã§<a href="http://watape.ddo.jp/">Wæ°</a>ã®ææãåãã¦ä¿®æ£ +</ul> +<li>2005/11/15 +<ul> +<li>èª¤è¨³ä¿®æ£ +<li>æªè¨³é¨åããã£ãã®ã§è¿½å ã§è¨³ +<li>typoä¿®æ£ +<li>表ç¾ã®å¤æ´ +</ul> +</ul> +<div class="back"> +<a href="#top">â² to Top</a> +</div> +<div class="copyright"> +Copyright©2002-2003, Ktat All rights reserved. + +</div> +</body></html> \ No newline at end of file