Mojoliciousã¯ãPerlçšã®ææ°ã®Webãã¬ãŒã ã¯ãŒã¯ã§ãã æ¬ ç¹ã®ãã¡ã2ã€ã ãååãä»ããããšãã§ããŸãïŒåŸæ¹äºææ§ããªã·ãŒãšããã¥ã¡ã³ãã
ãã®äžé£ã®èšäºã¯ãèªè ããã¬ãŒã ã¯ãŒã¯ã«ãã§ã«è¡šé¢çã«ç²ŸéããŠããããšãåæãšããŠãããããã¥ã¡ã³ãã«èšèŒãããŠããªãããååã«è©³çŽ°ãã€æ確ã«èšèŒãããŠããªã詳现ãç解ããå¿ èŠããããŸãã å ¬åŒææž ïŒè±èªïŒã¯ãæåã®çŽ¹ä»ã«æé©ã§ãã
å 容
- çæ
- ã«ãŒãã£ã³ã°ïŒå éšããã€ã¹
- ã«ãŒãã£ã³ã°ïŒã»ããã¢ãã
- HTTPãªã¯ãšã¹ããã©ã¡ãŒã¿
- 解æ
- ãã³ããšã³ã
- CGIã¢ãŒãã§ã®ãã³ããããã³ã°ã¢ããªã±ãŒã·ã§ã³ã®ãµããŒã
- Mojo ::ã¢ããªã±ãŒã·ã§ã³ããã¹ããããšãã®UserAgentã®ä»çµã¿
- ojoãšMojolicious :: Lite
- ç°å¢å€æ°
ãã®ã·ãªãŒãºã®ä»ã®èšäº
æ¬ ç¹
å ¬åŒFAQã«ã¯æ¬¡ã®ããã«æžãããŠããŸãïŒã...ã¡ãžã£ãŒãªãªãŒã¹éã§äºææ§ã®ãªãæ¹æ³ã§åé€ãŸãã¯å€æŽããåã«ãåžžã«æ©èœãå»æ¢ããŸã...å®éšçãæªãã¹ãããŸãã¯ææžåãããŠããªããã®ã䜿çšããŠããªãéããåžžã«åŸæ¹äºææ§ãæåŸ ã§ããŸã...ã ãŸãã2çªç®ã®ãã¬ãŒãºã¯æåã®ãã¬ãŒãºãšççŸããŸãã 次ã«ã Guides :: Contributingããã®åŒçšã§ãããæ©èœã¯ãã¡ãžã£ãŒãªãªãŒã¹ãŸãã¯å°ãªããšã3ãæéå»æ¢ãããåŸã«ã®ã¿å€æŽã§ããŸããã æ£çŽãªãšãããåŸæ¹äºææ§ã«é¢ããŠã¯3ãæã¯ãã§ã«ãšãã§ããªãæéã§ããããã®æéã§ããåžžã«å°éãããŠããããã§ã¯ãªãããã§ãïŒX-Forwarded-HTTPSãµããŒãã¯2ãæåã«å»æ¢ããã 1ãæåã«åé€ãããŸãã-ã¯ããããã§ããïŒãããã£ãŠããã¡ãžã£ãŒãªãªãŒã¹ãã§ã¯ãèŠåã«æ£åŒã«éåããããšã¯ãããŸãããã äžäœäºææ§ã«å¯Ÿããäžè¬çãªæ 床ã¯ããªã瀺åçã§ãã 3ãæã«1å以äžãã¬ãŒã ã¯ãŒã¯ãæŽæ°ããå»æ¢ãããèŠåã®å€æŽãã¢ããªã±ãŒã·ã§ã³ãã°ã泚ææ·±ãèªãã§ããéçºè ã¯äœäººããŸããïŒ åæã«ãæšå¹ŽãçŽ20ã®æ©èœãå»æ¢ãããŸããã ãã¡ãããå®éã«ã¯ããã¹ãŠã
$app->secret()
ã»ã©æªãã¯ãããŸãã-äœããããã»ã©é »ç¹ã«å£ããããšã¯ãããŸããïŒå人çã«ã¯ãæšå¹Žã
$app->secret()
ã
$app->secrets()
眮ãæããã ãã§åœ±é¿ãåããŸããïŒ ããããäºå®ã¯æ®ã£ãŠããŸã-äžäœäºææ§ã¯å£ããŠããããã°ãã°å£ããŠãããæ¬åœã«æ£åœãªçç±ããããŸããïŒããšãã°ã
secret()
å Žåãã³ãŒããžã®è¿œå ã劚ãããã®ã¯äœããããŸãã
sub secret { shift->secrets([shift]) }
ãŸãã¯ãäºææ§ããŸã£ããæãªãããšãªãç®çã®æ©èœãå®è£
ããããšsecrets()
ãæ°ããé¢æ°
secrets()
ãè¿œå ãã代ããã«ã
secret()
ã«è¿œå ã®ãã©ã¡ãŒã¿ã®ãµããŒããè¿œå ããŸãã
ããã¥ã¡ã³ãã«é¢ããŠã¯ãMojoliciousã®é倧ãªå©ç¹ã®1ã€ã§ãããããæ¬ ç¹ã§ã¯ãªããšå€ãã®äººãèããŠããŸãã ããã¥ã¡ã³ãã®åé¡ã¯ããã¹ãŠäŸã«çŠç¹ãåœãŠãããŠããããšã§ãã ãã¬ãŒã ã¯ãŒã¯ã®åŠç¿ãéå§ãããšããããã¯æ¬åœã«ã¯ãŒã«ã§ãã ããã«ãããæ©èœãäœæããå¿ èŠããããšãã«å€ãã®æéãç¯çŽã§ããå ¬åŒã¬ã€ãã§åæ§ã®æ©èœã®äŸããã°ããGoogleã§æ€çŽ¢ã§ããŸãã ããããæšæºã¿ã¹ã¯ã®ç¯å²ãè¶ ããŠãã€ããªãã®ãŒçãŸãã¯ã¢ãŒããã¯ãã£çã«èšèšãããæ¹æ³ã ãã®é¢æ°ãåãããšãã§ããç¹å®ã®ãã©ã¡ãŒã¿ãŒãããŸããŸãªç¶æ³ã§æ£ç¢ºã«è¿ãããšãã§ãããã®ãç解ããå¿ èŠããããšããã«ãå€ãã®Mojoliciousã¢ãžã¥ãŒã«ã§ã¯ãã®ãããªããã¥ã¡ã³ããæ¬ èœããŠããŸãååãšããŠã ãããŠããã®æ å ±ããææžåãããŠããªãæ©äŒããåç §ããŠããããã§ã¯ãããŸãããããã®ã»ãšãã©ãã¹ãŠã¯ãããŸããŸãªäŸã§ãã¡ãã¡ã§ç°¡åã«èšåãããŠããŸãã ç¹å®ã®ããŒã¿ ïŒèŠæ±ãã©ã¡ãŒã¿ãŒãå¿çæ¬æãªã©ïŒ ã«ã¢ã¯ã»ã¹ããæ¹æ³ã¯ããã€ããããŸãããããããäºãã«ã©ã®ããã«ç°ãªããã©ã®ãããªç¶æ³ã§ã©ã®ã¡ãœããã䜿çšããã®ãããé©åãã¯èª¬æãããŠããŸããã ãããŠæåŸ-ããã¯å ã®é¢æ°ã®ã¢ã«ãã¡ãããé ãçå£ã«ïŒïŒ ããããç§ã¯ãã¹ãŠã®äººãç°ãªã£ãŠãããäžéšã®äººã«ãšã£ãŠã¯ãããã䟿å©ã§ããããšãç解ããŠããŸãããå€ãã®äººã«ãšã£ãŠãã¿ã¹ã¯ããšã«æ©èœãã°ã«ãŒãåãããããã¥ã¡ã³ããç解ããæ¹ãã¯ããã«ç°¡åã§ãã ïŒç¹ã«ãVimã®ããã«æ€çŽ¢ã䜿çšããã®ã䟿å©ã§ã¯ãªããã©ãŠã¶ã§èªãå Žåã¯ãã³ãŒãå ã§ãé¢æ°ã®ã¢ã«ãã¡ãããé ãäºæ³å€ã«éåžžã«äŸ¿å©ã§ããããšãå€æããŸãã-new / DESTROY / AUTOLOADãé€ãããããã¯ãŸã å é ã«é 眮ãããŠããŸãïŒãããç解ããããã«ã¯ãã³ãŒããèªãå¿ èŠããããŸãïŒä»£ããã«ãã¹ããèŠãã®ã奜ãã§ãïŒïŒãããã¯ããã»ã©ç°¡åã§ã¯ãããŸãã-ãŸããèªã¿ãããã®æšæºã§ã¯ãããŸããïŒèè ã¯ã³ãŒããã³ã³ãã¯ãã«æžãããšãã§ããããŒã«ãããã䜿çšããã®ã奜ãã§ãïŒãããŠãã®ãããªã³ãŒãã¯ããéãåäœããŸãïŒèªã¿ããã æªåããŠãã; 第äºã«ããªããžã§ã¯ãéã®ç¶æ¿ãšã€ãã³ãã®äº€æã®äž¡æ¹ãç©æ¥µçã«äœ¿çšãããšãMojolicious-5ãæ§æãã104ã®ã¯ã©ã¹ã®å éšã§äœãèµ·ãã£ãŠããããç解ããããšãé£ãããªããŸãã
äžäœäºææ§ã®åé¡ã«ã€ããŠã¯ã»ãšãã©äœãã§ããŸããïŒãã ããå¯èœã§ããã°ãã€ã§ããšãã¥ã¬ãŒãããMojoliciousãã©ã°ã€ã³ãäœæã§ããŸãïŒã ãããã2çªç®ã®åé¡ã解決ããã®ã¯é£ãããããŸãããäžè¶³ããŠããããã¥ã¡ã³ããèªåã§äœæã§ããŸãã Mojoliciousãå匷ãããšããå ¬åŒã®ããã¥ã¡ã³ãã«å«ãŸããã¹ãããã€ãã®ããšã説æããäºå®ã§ãããããã£ãŠããã®èšäºã®ã¿ã€ãã«ã§ãã
$èªå·±
Mojoliciousã®ããã¥ã¡ã³ãã¯ãèªã¿ããããå¢ãããªã
$self
ãã䜿çšããŸã-ãã¬ãŒã ã¯ãŒã¯å ã®ã¯ã©ã¹ãå€ãããããããã®äŸã§ãã®ãªããžã§ã¯ããã©ã®ã¯ã©ã¹ã§
$self
ããç°¡åã«ææ¡ã§ããã®ã¯ãåžžã«
$self
èŠãããã§ã¯ãããŸããã ãããã£ãŠãäŸã§ã¯ã
$self
代ããã«ã以äžã䜿çšããŸãã
$app # YourApp â Mojolicious $r # Mojolicious::Routes ($app->routes) $c # YourApp::SomeController â Mojolicious::Controller $ua # Mojo::UserAgent
ã«ãŒãã£ã³ã°ïŒå éšããã€ã¹
Mojoliciousã®ã«ãŒãã£ã³ã°ããã€ã¹ã«ã€ããŠæåã«ç解ããå¿ èŠãããã®ã¯ãããŒãã®ããªãŒãšããŠå®è£ ãããŠããããã®ããªãŒã®æ§é ãïŒã»ãšãã©ïŒurlã®ãã¹éå±€ã«æ¥ç¶ãããŠããªãããšã§ãã äŸãèããŠã¿ãŸãããïŒ
$r->get("/a/b/c/d") ->to(text=>"1"); $ab = $r->route("/a")->route("/b"); $ab->get("/c") ->to(text=>"2-1"); $ab->get("/c/d") ->to(text=>"2-2"); $r->get("/a/b/c/d/e") ->to(text=>"3");
ãã®çµæããã®ãããªããªãŒãæ§ç¯ãããŸãïŒ
$ r {} ââ/ a / b / c / d {text => "1"} ââ/aâââ/bââ¬â/ c {text => "2-1"} âââ/ c / d {text => "2-2"} ââ/ a / b / c / d / e {text => "3"}
ãããŠããããã©ã®ããã«æ©èœãããã§ãïŒ
GET / a / b => 404 Not Found GET / a / b / c => "2-1" GET / a / b / c / d => "1" GET / a / b / c / d / e => "3" GET / a / b / c / d / e / f => 404 Not Found
ãæ³åã®ãšãããçŸåšã®ãªã¯ãšã¹ããšæåã«äžèŽãããŸã§ããªãŒãé çªã«ïŒè©³çŽ°ã«ïŒã¹ãã£ã³ãããŸã-ã«ãŒãã£ã³ã°å®çŸ©ã«åããªã¯ãšã¹ãã«äžèŽããããŒããããå ŽåãããªãŒå ã®å Žæã泚ææ·±ãç£èŠããŸã-ãªã¯ãšã¹ãã«äžèŽããå Žåãã³ãŒãã«èšè¿°ãããŠããé åºãšäžèŽããªãå ŽåããããŸãã
2çªç®ã«ç解ããããšã¯ãããªãŒã®ãªãŒãïŒã¿ãŒããã«ããŒãïŒã®ã¿ãçä¿¡ãªã¯ãšã¹ããåŠçããäžéïŒå éšïŒããŒãã¯ãã¹ãŠããªã¯ãšã¹ãã®äœææ¹æ³ã«é¢ä¿ãªãïŒ
route()
ãéåžžã®
get()
ããªã©ïŒããã³ãªã¯ãšã¹ããã³ãã©ããããã«èšå®ãããŠãããã©ããïŒ
"controller#action"
ãŸãã¯
{cb=>\&handler}
ãªã©ïŒã
ããšãã°ã
get()
ã䜿çšããŠããªãŒãäœæããŸãã
$r->get("a", {text=>"A"})->get("b", {text=>"B"});
GET / a => 404 Not Found GET / a / b => "B"
ãŸãã¯ãããŒãããŸã£ããäœæããã«ãæ¢åã®ã«ãŒãããŒããæ§æããŸãã
$app->routes->to(text=>"wow");
GET / => "ããã"
äžéããŒãã«æå®ããããã³ãã©ãŒã䜿çšãããã®ã¯ããã®ããŒããäžã«ããå Žåã®ã¿ã§ãã ãã®ãããªããŒãã¯
under()
ãä»ããŠäœæãããŸãããŸãã¯ã
inline(1)
åŒã³åºããŠæ¢åã®ããŒããunderã«ããããšãã§ããŸãã çŸåšã®èŠæ±ãåŠçãã端æ«ããŒãã決å®ããåŸãããªãŒã®ã«ãŒããã端æ«ãžã®ãã¹äžã®ãã¹ãŠã®äžäœããŒãã®ãã³ãã©ãŒãé çªã«åŒã³åºãããŸãã ãããã®ãã³ãã©ãŒã¯trueãŸãã¯falseãè¿ãå¿ èŠããããŸãïŒéåæã§ãå¯èœïŒ-falseãè¿ãå Žåãã¿ãŒããã«ããŒããã³ãã©ãŒãå«ãåŸç¶ã®ãã³ãã©ãŒã¯åŒã³åºãããŸããã
ããã«ãããªãŒããŒãã«ã¯æ¬¡ã®æ å ±ãå«ãŸããå ŽåããããŸãã
- HTTPã¡ãœãã
- ãã¹ãã¿ãŒã³ããã¬ãŒã¹ãã«ããŒãå«ããããšãã§ããŸã
"/something/:name/:id"
- èš±å¯ããããã¬ãŒã¹ãã«ããŒã®å¶çŽ
name => ["alex","nick"], id => qr/^\d+$/
- ç¹å¥ãªå¶éïŒãã¹ãã³ãã¬ãŒãã«æ¡åŒµåãè¿œå ããããšã¯å¯èœã§ããïŒ
format => 0 format => ["json","xml"]
- ç¹å¥ãªå¶éïŒãã¹ãã³ãã¬ãŒãã«æ¡åŒµåãè¿œå ããããšã¯å¯èœã§ããïŒ
- æ¡ä»¶-è¿œå ãã§ãã¯ïŒhttp-headersãªã©ïŒãå®è¡ããããã«ãã¹ããã³ãã¬ãŒããšäžèŽããåŸã«åŒã³åºãããçŸåšã®ãªã¯ãšã¹ããåŠçããããã«ãã®ããŒãã®äœ¿çšãæå¹ãŸãã¯ç¡å¹ã«ããtrue / falseãè¿ãé¢æ°
agent => qr/Firefox/
- ããã©ã«ãã®ãã©ã¡ãŒã¿ãŒïŒããã©ã«ãïŒ-ããã§å¶åŸ¡ãã©ã¡ãŒã¿ãŒãèšå®ããïŒã³ã³ãããŒã©ãŒ/ã¢ã¯ã·ã§ã³/ cb / ...ïŒããã¬ãŒã¹ãã«ããŒã®ããã©ã«ãå€ïŒãããã®ãã¬ãŒã¹ãã«ããŒããªãã·ã§ã³ã«ããŸãïŒããã³
$c->stash()
衚瀺ããããã®ä»ã®å€ãªã¯ãšã¹ãåŠç - ãã®ããŒãã®æ瀺çãªåå-äŸ
url_for
ã§äœ¿çšãããã
- èšå®ãããŠããªãå Žåãèªåçã«çæãããŸã
# defaults . $r->to("users#"); # /resource . $b = $r->route("/resource", format => 0); # $b $b # /resource. # get() , # , , .. /resource. $b->get()->over(agent=>qr/Firefox/)->to("#ff_only"); $b->get()->to("#not_ff");
ç§ã®ç¥ãéããããã©ã«ãå€ã
$app->defaults
ãšã«ãŒãããŒã
$app->routes->to
èšå®ããããšã®éã
$app->routes->to
ã»ãšãã©ãªãã§ãããïŒã«ãŒãã£ã³ã°ã®çŽåã«ããã€ãã®ããã¯ãæ©èœãã
$app->defaults
ãå©çšå¯èœã«ãªããŸãããã«ãŒãããŒãããã¯å©çšã§ããªãå ŽåããããŸãã
ããã€ãã®åŸ®åŠãªéãããããŸããããšãã°ãã¿ãŒããã«ããŒãã§ãã£ãŠããã¢ã³ããŒããŒãã¯ãªã¯ãšã¹ããåŠçããŸãããã«ãŒãããŒãã¯ãä»ã®ãã¹ãŠã®ããŒããšã¯å°ãç°ãªã圢åŒã®å€ãåŠçããŸã...ããããããã¯äžè¬çãªç解ã«ãšã£ãŠéèŠã ãšã¯æããªãã®ã§ã詳现ã«ã¯è§ŠããŸããã
å¥ã®Mojoliciousã¢ããªã±ãŒã·ã§ã³ããçŸåšã®ã¢ããªã±ãŒã·ã§ã³ãžã®æ¥ç¶ã
$r->any("/path")->to(app=>$otherapp)
ä»ããŠã©ã®ããã«æ©èœãããã¯ãŸã ããããŸããã
ã«ãŒãã£ã³ã°ïŒã»ããã¢ãã
MojoliciousãšMojolicious :: Liteã«ã¯éãããããŸã-Mojolicious :: Liteã®ïŒã°ã«ãŒããšå ±ã«ïŒåäœã¯ãMojoliciousã®å Žåãšã¯å°ãç°ãªããŸãã ããã§ã¯ãMojoliciousïŒããæ£ç¢ºã«ã¯ãMojolicious :: Routes :: RouteïŒã®æ©èœã«ã€ããŠèª¬æããŸãã
ãã¹ãŠã®é¢æ°ã®ãã¹ãŠã®ãã©ã¡ãŒã¿ãŒã¯ãªãã·ã§ã³ã§ãïŒ
over()
ã
to()
ããã³
via()
ãé€ã-ãã©ã¡ãŒã¿ãŒãªãã§åŒã³åºããããšãçŸåšã®å€ãè¿ããŸãïŒã
- ã·ã³ãã«ãªäœã¬ãã«æ©èœïŒ
-
$r->route()
ã¯ãæ°ããããŒãããã©ã¡ãŒã¿ãŒãäœæããŸãïŒ
- ãã¹ãã³ãã¬ãŒãïŒå¥æ°åã®ãã©ã¡ãŒã¿ãŒãæã€æåã®ã¹ã«ã©ãŒïŒ
- ãã©ãŒããããå«ãå¶éïŒââãã©ã¡ãŒã¿ãŒã®ãã¢ïŒ
-
$r->via()
ã¯HTTPã¡ãœãããèšå®ããŸã
- ã¡ãœããïŒãªã¹ãïŒãŸãã¯ãªã¹ããžã®ãªã³ã¯
-
$r->over()
ã¯æ¡ä»¶ãèšå®ããŸã
- æ¡ä»¶ïŒãã©ã¡ãŒã¿ãŒã®ãã¢ããŸãã¯ãã©ã¡ãŒã¿ãŒã®ãã¢ã®é åãžã®åç §ïŒ
-
$r->to()
ã¯ããã©ã«ãã®ãªãã·ã§ã³ãèšå®ããŸã
- ãã³ãã©ãŒïŒã¢ããªã±ãŒã·ã§ã³ãŸãã¯ããã±ãŒãžãã³ã³ãããŒã©ãŒãããã³/ãŸãã¯ã¢ã¯ã·ã§ã³ïŒïŒå¥æ°ã®ãã©ã¡ãŒã¿ãŒãæã€æåã®ã¹ã«ã©ãŒïŒ
- ããã©ã«ãã®ãã©ã¡ãŒã¿ãŒïŒãã©ã¡ãŒã¿ãŒã®ãã¢ãŸãã¯ããã·ã¥åç §ïŒ
-
- æ ãè
ã®ããã®å±±ç©ã¿ã®ã³ã³ãã€ã³ïŒ
-
$r->under()
ã¯ãã¢ã³ããŒããŒããäœæããŸã
-
get()
ãªã©ã®ãã¹ãŠã®ãã©ã¡ãŒã¿ãŒ
-
-
$r->any()
ã¯ãHTTPã¡ãœããã®ããŒããäœæããŸã
- ã¡ãœããã¯ãæåã®ãã©ã¡ãŒã¿ãŒãšããŠèšå®ã§ããŸãïŒé ååç §ïŒ
-
get()
ãªã©ã®ä»ã®ãã©ã¡ãŒã¿ãŒ
-
$r->get()
ã¯HTTP GETã¡ãœããã®ããŒããäœæããŸã
- ãã¹ãã³ãã¬ãŒãïŒã¹ã«ã©ãŒãã©ã¡ãŒã¿ãŒã®æåïŒ
- æ¡ä»¶ïŒã¹ã«ã©ãŒãšæ¬¡ã®ãã©ã¡ãŒã¿ãŒïŒ
- ããŒãåïŒæåŸã®ãã©ã¡ãŒã¿ãŒã§ããã¹ã«ã©ãŒïŒ
- ãã³ãã©ãŒé¢æ°ïŒé¢æ°åç §ãããã©ã«ãã®ãã©ã¡ãŒã¿ãŒå€ããcbãã«èšå®ïŒ
- ãã©ãŒããããå«ãå¶éïŒââé ååç §ïŒ
- ããã©ã«ããã©ã¡ãŒã¿ïŒããã·ã¥ãªãã¡ã¬ã³ã¹ïŒ
-
$r->post()
ã¯HTTP POSTã¡ãœããã®ããŒããäœæããŸã
-
get()
ãªã©ã®ãã¹ãŠã®ãã©ã¡ãŒã¿ãŒ
-
-
$r->put()
ã¯ãHTTP PUTã¡ãœããã®ããŒããäœæããŸã
-
get()
ãªã©ã®ãã¹ãŠã®ãã©ã¡ãŒã¿ãŒ
-
-
$r->delete()
ã¯ãHTTP DELETEã¡ãœããã®ããŒããäœæããŸã
-
get()
ãªã©ã®ãã¹ãŠã®ãã©ã¡ãŒã¿ãŒ
-
-
$r->patch()
ã¯HTTP PATCHã¡ãœããã®ããŒããäœæããŸã
-
get()
ãªã©ã®ãã¹ãŠã®ãã©ã¡ãŒã¿ãŒ
-
-
$r->options()
ã¯ãOPTIONS HTTPã¡ãœããã®ããŒããäœæããŸã
-
get()
ãªã©ã®ãã¹ãŠã®ãã©ã¡ãŒã¿ãŒ
-
-
# Mojolicious $r->get("/users/:id", [ format => 0 ], agent => qr/Firefox/, { id => -1, controller => "users" }, [ id => qr/^\d+$/ ], headers => { "X-Secret" => "letmeit" }, \&cb, { action => "list" }, "my_cool_route", ); # get() $r->route("/users/:id", id => qr/^\d+$/, format => 0) ->via("GET") ->over(agent => qr/Firefox/, headers => { "X-Secret" => "letmeit" }) ->to(id => -1, controller => "users", action => "list", cb => \&cb) ->name("my_cool_route");
HTTPãªã¯ãšã¹ããã©ã¡ãŒã¿
ãªã¯ãšã¹ããã©ã¡ãŒã¿ã«ã¢ã¯ã»ã¹ããæ¹æ³ã¯å€ããããŸãããããããããããŸãã ããã«ãããããã¹ãŠã䜿çšãã䟡å€ã¯ãããŸãã-å Žåã«ãã£ãŠã¯ãç°ãªãå ŽæããååŸããããã©ã¡ãŒã¿ãŒãæ··åãããã©ãããæ¥ãã®ããç解ããããšã¯éçŸå®çã«ãªããŸãã
Mojoliciousã«ã¯4çš®é¡ã®ãã©ã¡ãŒã¿ãŒããããŸãã
- GET -URLã®ã¯ãšãªæååããååŸããHTTPèŠæ±ã¡ãœããã¯GETãPOSTãªã©ãäœã§ãããŸããŸããã
- POST-ã¿ã€ã
application/x-www-form-urlencoded
ãŸãã¯ã¿ã€ãmultipart/form-data
POSTèŠæ±ã®æ¬äœããåä¿¡ãmultipart/form-data
-ãã ãããã®å Žåããã¡ã€ã«ãé€ããéåžžã®ãã©ã¡ãŒã¿ãŒã®ã¿ãååŸãããŸã - UPLOAD-ã¿ã€ã
multipart/form-data
POSTèŠæ±ã®æ¬æããåä¿¡ãããã¡ã€ã« - ROUTE-ã¹ã¿ãã·ã¥ çšã«äºçŽãããŠãããã®ãé€ããã«ãŒãã£ã³ã°ã®ãã¬ãŒã¹ãã«ããŒã䜿çšããŠURLãã¹ããåãåãããå€
ããã«ãåããã©ã¡ãŒã¿ãŒãè€æ°åæž¡ãããšãã§ããããã«ãGETãPOSTãUPLOADã®åæ¹æ³ã§è€æ°åæž¡ãããšãã§ããã«ãŒãã£ã³ã°ã決å®ãããšãã«åããã¬ãŒã¹ãã«ããŒãæ°åèšåã§ããããšã«æ³šæããŠãã ããã GETãPOSTãããã³UPLOADã®å Žåã1ã€ã®ãã©ã¡ãŒã¿ãŒã®æž¡ãããå€ã¯ãã¹ãŠä¿åãããŸãããROUTEã®å Žåã1ã€ã®ãã¬ãŒã¹ãã«ããŒãè€æ°åæå®ããããšã æåŸã®å€ã®ã¿ã䜿çšãããŸãã
ã»ãšãã©ã®å ŽåãäŸã§
$c->param
ãèšåãããŠããŸã-ãã®é¢æ°ã«ãã£ãŠè¿ãããå€ãã©ãããæ¥ãã®ãèŠãŠã¿ãŸãããïŒMojoliciousã§ã¯5.47ãŸã§ïŒ ïŒ
-
scalar $c->param()
-undefãè¿ããŸã -
@names = $c->param()
-ãã¹ãŠã®GETãPOSTãUPLOADããã³ROUTEãã©ã¡ãŒã¿ãŒã®ååãè¿ããŸã -
$value = $c->param("name")
-æ»ãå€ïŒ
- æåŸã®å€ã¯ãã¬ãŒã¹ãã«ããŒãååãã§ãããããã§ãªãå Žåã¯
- UPLOAD "name"ã®æåã®å€ã§ããå Žåãããã§ãªãå Žå
- æåã®POSTå€ "name"ïŒããå ŽåïŒãããã§ãªãå Žå
- æåã®å€GET "name"ã§ããå Žåãããã§ãªãå Žå
- æªå®çŸ©
-
@values = $c->param("name")
-æ»ãå€ïŒ
- æåŸã®å€ã¯ãã¬ãŒã¹ãã«ããŒãååãã§ãããããã§ãªãå Žåã¯
- ãã¹ãŠã®UPLOAD "name"å€ïŒååšããå ŽåïŒããã以å€ã®å Žå
- ãã¹ãŠã®POST "name"å€ããã®åŸã«ãã¹ãŠã®GET "name"å€ïŒååšããå ŽåïŒ
- ïŒïŒ
$c->param
ã¯ãåæªã®å¢çãè¶ ããŠããŸãïŒããšãã°ãGET / POSTãã©ã¡ãŒã¿ãŒãæåŸ ããå Žåãããã³UPLOADãååŸãã æååå€ã®ä»£ããã«Mojo :: Upload ãªããžã§ã¯ããååŸããŸã ïŒã ãã¹ãŠãç¯åºŠã«åªããŠãããéæ³ã®ãããªæ lazãªäººã§ãããMojoliciousã§å®è£ ããããããèŠçŽ ãäœãåºããŸãã
HTTPãªã¯ãšã¹ãã®ãã©ã¡ãŒã¿ãŒã«ã¢ã¯ã»ã¹ããããã«å¶éããããšãæšå¥šãããæ©èœã®ãªã¹ãã¯æ¬¡ã®ãšããã§ãã
- URLãã¹ãããã¬ãŒã¹ãã«ããŒå€ãååŸããŸãã
$value = $c->stash( "name" ) # ROUTE
- ãã¹ãŠã®ãã©ã¡ãŒã¿ãŒã®ååãšãã¹ãŠã®å€ãååŸããŸãã
# HASHREF: : # - ( ) # - ( ) $params = $c->req->params->to_hash # POST, GET $params = $c->req->query_params->to_hash # GET $params = $c->req->body_params->to_hash # POST # ARRAYREF: , Mojo::Upload $uploads = $c->req->uploads # UPLOAD
- ãã¹ãŠã®ãã©ã¡ãŒã¿ãŒã®ååãååŸããŸãã
@names = @{ $c->req->params->names } # POST GET @names = @{ $c->req->query_params->names } # GET @names = @{ $c->req->body_params->names } # POST @names = keys %{{ map { $_->name => 1 } @{ $c->req->uploads } }} # UPLOAD
- 1ã€ã®ãã©ã¡ãŒã¿ãŒã®æåŸã®å€ãååŸããŸãã
$c->req->params->param( "name" ) # POST, GET $c->req->query_params->param( "name" ) # GET $c->req->body_params->param( "name" ) # POST $c->req->upload( "name" ) # UPLOAD
- 1ã€ã®ãã©ã¡ãŒã¿ãŒã®ãã¹ãŠã®å€ãååŸããŸãã
$c->req->params->every_param( "name" ) # POST, GET $c->req->query_params->every_param( "name" ) # GET $c->req->body_params->every_param( "name" ) # POST $c->req->every_upload( "name" ) # UPLOAD
- äžèšã®é¢æ°ãåŒã³åºãããã®ä»£æ¿ãªãã·ã§ã³ïŒ
-
$c->req->param
ã¯ã$c->req->params->param
ãšåãã§ã -
$c->req->url->query
ã¯$c->req->query_params
ãšåã$c->req->query_params
-
-
to_hash
代ããã«to_hash
pairs
ã䜿çšã§ããŸãããã¹ãŠã®ãã©ã¡ãŒã¿ãŒã®ååãšå€ãé çªã«ç§»åããé åãžã®åç §ãè¿ããŸããäžæ¹ã1ã€ã®ååã¯è€æ°ååºçŸã§ããŸããããã¹ãŠã®å€ã¯ã¹ã«ã©ãŒã§ãã
解æ
ããã¯ç§ã®èŸæžããã¯å®å šã§ã¯ãããŸããããå¥ã®èª¬æãèŠã€ããããšãã§ããŸãããããŠã³ããŒããããããŒãžã®è§£æã«é¢ããŠã¯ãmojoã¯ã¡ãã£ãšããçããã®ã§ãã Mojoã®ãã®éšåã®ããã¥ã¡ã³ãã¯ãã¯ããã«åªããŠããŸãã ããã«ãããããããããã«è¿œå ãããã®ããããŸãã
å®éãMojoã¯ãµãŒããŒåŽã®Webãã¬ãŒã ã¯ãŒã¯ã§ã¯ãªãããµãŒããŒãšã¯ã©ã€ã¢ã³ãã®äž¡æ¹ã®Webãã¬ãŒã ã¯ãŒã¯äžè¬ã§ãã ãããã£ãŠãHTTPã¡ãã»ãŒãžåœ¢åŒãå®è£ ããã¢ãžã¥ãŒã«ã¯ãµãŒããŒãšã¯ã©ã€ã¢ã³ãã®äž¡æ¹ã§äœ¿çšããããããã®ã¡ãã»ãŒãžãåŠçããããã®ããŒãºã¯ãŸã£ããç°ãªããŸãã ãã®çµæãäœããããŠããã¥ã¡ã³ãã«ç®ãéãå¿ èŠãããå Žåããã®æç¹ã§ãµãŒããŒåŽã®æ©èœãŸãã¯ã¯ã©ã€ã¢ã³ãåŽã®æ©èœã®ããããã«èå³ããããŸã-ããã ãã§ãªããäž¡æ¹ãã¢ã«ãã¡ãããé ã«æ³šææ·±ããœãŒããããŠããããšãããããŸã ãã®çµæãç®çã®æ©èœãèŠã€ããããšã¯éåžžã«é£ãããèŠèŠçãªããŒãã·ãŒããå¿ èŠã§ãããã¯ã©ã€ã¢ã³ããŸãã¯ãµãŒããŒã®ã¿ã®æ©èœããããé©åãªåºæºã§ã°ã«ãŒãåãããŠããããšãæãŸããã§ãã
çµæã¯æ¬¡ã®ãã¬ãŒãã§ãã ããã¯æåã®ã¢ã«ãã¡çã§ã:)äœããæ確ã§ãªãããæ¹åã®ããã®ã¢ã€ãã¢ãããå Žå-æžã蟌ã¿ãç§ãã¡ã¯å ±åã®åªåã§ãããçµäºããŸãã ããã€ãã®ã¡ã¢ïŒ
- ãã³ãã³ã°ãšã©ãŒãçºçããŠãã
$tx->res
ã䜿çšå¯èœã«ãªããŸãïŒ404ã¹ã¿ãä»ãïŒã -
$dom
ã®ããªãŒã«ã¯ã«ãŒãããŒãããããhtml
ã¿ã°ã®ããããŒãïŒããŠã³ããŒããããããŒãžã«ããå ŽåïŒã¯ã«ãŒãããŒãã®åå«ã§ãã ãã®ãããã«ãŒãããŒãã®to_string
ãšcontent
ã¯åããã®ãè¿ããŸãã - ååšããªãèŠçŽ ã«ã¢ã¯ã»ã¹ããå ŽåïŒãŸãã¯ãããšãã°ãã³ã¬ã¯ã·ã§ã³ãæåŸ
ããã1ã€ã®èŠçŽ ã
$dom->child_tag_name
èŠã€ãããã³ã¬ã¯ã·ã§ã³ã¡ãœãããåŒã³åºãããå ŽåïŒãããŒã«ã¯ããã®ãããªã¡ãœããã¯ãããŸããããšããäŸå€ãã¹ããŒããŸã-ã€ãŸããã»ãšãã©åžžã«ããŒãµãŒãeval
ã§å²ãå¿ èŠããããŸãã - ããŒãã®ã³ã¬ã¯ã·ã§ã³ããäžéšã®èŠçŽ ã空ã®ã³ã¬ã¯ã·ã§ã³ã®ä»£ããã«ã誀ã£ãŠã³ã¬ã¯ã·ã§ã³ã®ã³ã¬ã¯ã·ã§ã³ãååŸããã®ã¯éåžžã«ç°¡åã§ããããã§ã¯ã
flatten
ã§compact
ã³ã¬ã¯ã·ã§ã³ã¡ãœããã圹ç«ã¡ãŸãã - ãã©ã¡ãŒã¿ãŒ
"*"
ã¯ãCSSã»ã¬ã¯ã¿ãŒãæã€è¡ãæå³ããŸãã
$tx = $ua->get($url); # Mojo::Transaction::HTTP â Mojo::Transaction $tx->error # undef {message=>'âŠ',âŠ} $tx->success # undef $tx->res $tx->req # Mojo::Message::Request â Mojo::Message $tx->res # Mojo::Message::Response â Mojo::Message $tx->redirects # [ Mojo::Transaction::HTTP, ⊠] $res = $tx->res; # Mojo::Message::Response â Mojo::Message $res->error # undef {message=>'Parse error',âŠ} $res->to_string # "âŠ" (headers+content) $res->is_status_class(200); # bool $res->code # 404 $res->message # "Not Found" $res->headers # Mojo::Headers $res->cookies # [ Mojo::Cookie::Response, ⊠] $res->cookie('name') # Mojo::Cookie::Response â Mojo::Cookie $res->body # "âŠ" $res->text # "âŠ" (decoded body using charset) $res->dom # Mojo::DOM $res->json # Mojo::JSON $headers = $res->headers; # Mojo::Headers $headers->names # [ "Content-Type", "Server", ⊠] $headers->to_hash # { "Content-Type" => "âŠ", ⊠} $headers->header('Server') # "âŠ" $headers->$standard_header_name # "âŠ" (shortcuts for useful headers) $dom = $res->dom; # Mojo::DOM $dom->to_string # "âŠ" ( , ) $dom->content # "âŠ" ( ) $dom->type # "âŠ" ( : root,tag,text,comment,âŠ) $dom->tag # "âŠ" "" ( ) $dom->attr # {name=>"val",âŠ} $dom->attr('name') # "val" $dom->{name} # $dom->attr("name") $dom->all_text # "âŠ" ( ) $dom->all_text(0) # "âŠ" ( , ) $dom->text # "âŠ" ( ) $dom->text(0) # "âŠ" ( , ) $dom->root # Mojo::DOM ( ) $dom->parent # Mojo::DOM undef (-) $dom->next # Mojo::DOM undef ( -) $dom->next_node # Mojo::DOM undef ( -) $dom->previous # Mojo::DOM undef ( -) $dom->previous_node # Mojo::DOM undef ( -) $dom->matches('*') # true/false ( ) $dom->at('*') # Mojo::DOM undef ( ) $dom->find('*') # Mojo::Collection ( ) $dom->ancestors # Mojo::Collection (-) $dom->ancestors('*') # Mojo::Collection ( -) $dom->following # Mojo::Collection ( -) $dom->following("*") # Mojo::Collection ( -) $dom->following_nodes # Mojo::Collection ( -) $dom->preceding # Mojo::Collection ( -) $dom->preceding("*") # Mojo::Collection ( -) $dom->preceding_nodes # Mojo::Collection ( -) $dom->children # Mojo::Collection (-) $dom->children('*') # Mojo::Collection ( -) $dom->descendant_nodes # Mojo::Collection ( ) $dom->child_nodes # Mojo::Collection ( -) $dom->[0] # $dom->child_nodes->[0] $res->dom('*') # $dom->find('*')
ãã³ããšã³ã
CGIã¢ãŒãã§ã®ãã³ããããã³ã°ã¢ããªã±ãŒã·ã§ã³ã®ãµããŒã
Mojoliciousã¢ããªã±ãŒã·ã§ã³ãå®è¡ããã¹ã¯ãªããããããã«å€æŽãããšãcgiã¢ãŒãã§äœæ¥ããŠããå Žåã§ãããã³ããããã³ã°ã³ãŒãã®ãµããŒããæäŸã§ããŸãïŒhttps : //gist.github.com/powerman/5456484
Mojo ::ã¢ããªã±ãŒã·ã§ã³ããã¹ããããšãã®UserAgentã®ä»çµã¿
ãã¹ãã§ã©ã®ããã«
$ua
ã¢ããªã±ãŒã·ã§ã³ã«ãªã¯ãšã¹ããéä¿¡ããããŒãã§TCPãµãŒããŒãèµ·åããã«åçãåä¿¡ããæ¹æ³ãèããå Žåãçãã¯ç°¡åã§ãïŒã¢ããªã±ãŒã·ã§ã³
$app
ãããã³ãã¹ãŠã®Mojo :: UserAgentãªããžã§ã¯ããã°ããŒãã«ã«èšå®ã§ããŸããŸãã¯ããããã³ã«ãšãã¹ããæå®ãããŠããªãURLãžã®ãªã¯ãšã¹ããããã®URLãä»ããŠå®è¡ããŸã
$app
ã
éåžžããã®æäœã¯Mojolicious :: Liteã«ãã£ãŠå®è¡ãããããããæ£åžžã«åäœããŸããããã ãããã¹ãã§ãã®ã¢ãžã¥ãŒã«ã䜿çšãããŠããããMojo :: UserAgentãªããžã§ã¯ããäœæããã³ãŒãã®ã©ããã«ããå Žåããããèªäœã§æ©èœããããã¹ãŠãçªç¶æ©èœããªããªããŸãã次ã®ããã«ä¿®æ£ã§ããŸãã
Mojo::UserAgent::Server->app($app);
ojoãšMojolicious :: Lite
ojoã®ããã¥ã¡ã³ãã§ã¯ã説æãããŠããojoé¢æ°ã«å ããŠãMojolicious :: Liteé¢æ°ãå©çšã§ããããšãå¿ããŠããŸãããããã«ãããMojoliciousã§ã®å®éšãç°¡åã«ãªããŸãã
$ perl -Mojo -E 'get "/", {text=>"wow\n"}; app->start' get / wow
ç°å¢å€æ°
Mojoliciousã¯ãååãã§å§ãŸãããŸããŸãªç°å¢å€æ°ããµããŒãããŠããŸã
MOJO_
-ãã¹ãŠã説æããçç±ã¯ãããŸããããªã¹ãã¯Mojoliciousã®ããŒãžã§ã³ã«ãã£ãŠç°ãªãå¯èœæ§ããããŸãããããã€ãèšåãã䟡å€ããããŸãã
-
MOJO_MODE
-ä»ã®ã»ãšãã©ã®ç°å¢å€æ°ã¯1ã€ã®äºæã«åœ±é¿ããŸãããããã¯ç°ãªãäºæã«åœ±é¿ããŸããããã¯å¥ã®å Žæã§å床ææžåãããŠããããããã¹ãŠããŸãšããŠãã ããã
- ã»ãã
$app->mode
- ãã°ãã¡ã€ã«ã®ååãå®çŸ©ããŸã
- 決å®ãããã³ãã¬ãŒãã®ããŒãžã§ã³
exception
ãããã³not_found
䜿çšãããŸãïŒãã¡ã€ã«åã«ã¯å€ãå«ãŸããŸãMOJO_MODE
äŸãã°ãã«äŸå€ã$ mode.html.epÂ»ïŒ -
$app->log->level
«debug» «info»MOJO_MODE
«development» - ,
${mode}_mode()
startup()
( , ) .
- ã»ãã
-
MOJO_LOG_LEVEL
â «warn» Mojolicious::Lite, .