| 42 | | <pre><code># Mojo をダウンロード |
| 43 | | % wget http://mojolicious.org/Mojo-latest.tar.gz |
| 44 | | |
| 45 | | # 解凍 |
| 46 | | % tar xzvf Mojo-latest.tar.gz |
| 47 | | % cd Mojo |
| 48 | | |
| 49 | | # Makefile 生成 |
| 50 | | % perl Makefile.PL |
| 51 | | |
| 52 | | # make する |
| 53 | | % make |
| 54 | | |
| 55 | | # テストスィーツをうごかしてみる |
| 56 | | % make test |
| 57 | | |
| 58 | | # インストール |
| 59 | | % sudo make install |
| 60 | | |
| 61 | | # うまくいったか確認 |
| 62 | | % mojolicious daemon |
| 63 | | Server available at http://127.0.0.1:3000. |
| 64 | | |
| 65 | | # アクセスしたら、こんな画面がでます |
| 66 | | </code></pre> |
| 67 | | |
| 68 | | <p><img src="http://gyazo.com/ef11a052ae49b35fd69825287f3ae26f.png " /></p> |
| 69 | | |
| | 45 | <pre> |
| | 46 | # Mojo をダウンロード |
| | 47 | % wget http://mojolicious.org/Mojo-latest.tar.gz |
| | 48 | |
| | 49 | # 解凍 |
| | 50 | % tar xzvf Mojo-latest.tar.gz |
| | 51 | % cd Mojo |
| | 52 | |
| | 53 | # Makefile 生成 |
| | 54 | % perl Makefile.PL |
| | 55 | |
| | 56 | # make する |
| | 57 | % make |
| | 58 | |
| | 59 | # テストスィーツをうごかしてみる |
| | 60 | % make test |
| | 61 | |
| | 62 | # インストール |
| | 63 | % sudo make install |
| | 64 | |
| | 65 | # うまくいったか確認 |
| | 66 | % mojolicious daemon |
| | 67 | Server available at http://127.0.0.1:3000. |
| | 68 | </pre> |
| | 69 | |
| | 70 | <p>アクセスしたら、こんな画面がでます</p> |
| | 71 | |
| | 72 | <p><img src="<a href="http://gyazo.com/ef11a052ae49b35fd69825287f3ae26f.png">http://gyazo.com/ef11a052ae49b35fd69825287f3ae26f.png</a> " /></p> |
| | 73 | </div> |
| | 74 | <div class="section"> |
| 72 | | <pre><code># スケルトン生成 |
| 73 | | % mojolicious generate app BBS |
| 74 | | [mkdir] /private/tmp/bbs/bin |
| 75 | | [write] /private/tmp/bbs/bin/bbs |
| 76 | | [chmod] bbs/bin/bbs 744 |
| 77 | | [mkdir] /private/tmp/bbs/lib |
| 78 | | [write] /private/tmp/bbs/lib/BBS.pm # ディスパッチャ |
| 79 | | [mkdir] /private/tmp/bbs/lib/BBS |
| 80 | | [write] /private/tmp/bbs/lib/BBS/Example.pm # controller |
| 81 | | [exist] /private/tmp/bbs/lib/BBS |
| 82 | | [write] /private/tmp/bbs/lib/BBS/Context.pm # コンテキストクラス |
| 83 | | [mkdir] /private/tmp/bbs/t |
| 84 | | [write] /private/tmp/bbs/t/basic.t |
| 85 | | [mkdir] /private/tmp/bbs/public |
| 86 | | [write] /private/tmp/bbs/public/404.html |
| 87 | | [exist] /private/tmp/bbs/public |
| 88 | | [write] /private/tmp/bbs/public/index.html |
| 89 | | [mkdir] /private/tmp/bbs/templates/example |
| 90 | | [write] /private/tmp/bbs/templates/example/welcome.phtml |
| 91 | | # はしらせてみる |
| 92 | | % cd bbs |
| 93 | | % perl bin/bbs daemon |
| 94 | | Server available at http://127.0.0.1:3000. |
| 95 | | </code></pre> |
| 96 | | |
| 97 | | <p><img src="http://gyazo.com/90a6a75811d3fbbb5294e48ea32d655e.png" /></p> |
| 98 | | |
| | 77 | <pre> |
| | 78 | # スケルトン生成 |
| | 79 | % mojolicious generate app BBS |
| | 80 | [mkdir] /private/tmp/bbs/bin |
| | 81 | [write] /private/tmp/bbs/bin/bbs |
| | 82 | [chmod] bbs/bin/bbs 744 |
| | 83 | [mkdir] /private/tmp/bbs/lib |
| | 84 | [write] /private/tmp/bbs/lib/BBS.pm # ディスパッチャ |
| | 85 | [mkdir] /private/tmp/bbs/lib/BBS |
| | 86 | [write] /private/tmp/bbs/lib/BBS/Example.pm # controller |
| | 87 | [exist] /private/tmp/bbs/lib/BBS |
| | 88 | [write] /private/tmp/bbs/lib/BBS/Context.pm # コンテキストクラス |
| | 89 | [mkdir] /private/tmp/bbs/t |
| | 90 | [write] /private/tmp/bbs/t/basic.t |
| | 91 | [mkdir] /private/tmp/bbs/public |
| | 92 | [write] /private/tmp/bbs/public/404.html |
| | 93 | [exist] /private/tmp/bbs/public |
| | 94 | [write] /private/tmp/bbs/public/index.html |
| | 95 | [mkdir] /private/tmp/bbs/templates/example |
| | 96 | [write] /private/tmp/bbs/templates/example/welcome.phtml |
| | 97 | # はしらせてみる |
| | 98 | % cd bbs |
| | 99 | % perl bin/bbs daemon |
| | 100 | Server available at http://127.0.0.1:3000. |
| | 101 | </pre> |
| | 102 | |
| | 103 | <p><img src="<a href="http://gyazo.com/90a6a75811d3fbbb5294e48ea32d655e.png">http://gyazo.com/90a6a75811d3fbbb5294e48ea32d655e.png</a>" /></p> |
| | 104 | </div> |
| | 105 | <div class="section"> |
| 105 | | <pre><code>% my $c = shift; |
| 106 | | <!doctype html> |
| 107 | | <head><title>Welcome to the Mojolicious Web Framework!</title></head> |
| 108 | | <body> |
| 109 | | <h2>Welcome to the Mojolicious Web Framework!</h2> |
| 110 | | This page was generated from the template |
| 111 | | "templates/example/welcome.phtml", |
| 112 | | <a href="<%= $c->url_for %>"> |
| 113 | | click here |
| 114 | | </a> |
| 115 | | to reload the page or |
| 116 | | <a href="/index.html"> |
| 117 | | here |
| 118 | | </a> |
| 119 | | to move forward to a static page. |
| 120 | | </body> |
| 121 | | </html> |
| 122 | | </code></pre> |
| | 112 | <pre> |
| | 113 | % my $c = shift; |
| | 114 | <!doctype html> |
| | 115 | <head><title>Welcome to the Mojolicious Web Framework!</title></head> |
| | 116 | <body> |
| | 117 | <h2>Welcome to the Mojolicious Web Framework!</h2> |
| | 118 | This page was generated from the template |
| | 119 | "templates/example/welcome.phtml", |
| | 120 | <a href="<%= $c->url_for %>"> |
| | 121 | click here |
| | 122 | </a> |
| | 123 | to reload the page or |
| | 124 | <a href="/index.html"> |
| | 125 | here |
| | 126 | </a> |
| | 127 | to move forward to a static page. |
| | 128 | </body> |
| | 129 | </html> |
| | 130 | </pre> |
| 126 | | <pre><code>% my $c = shift |
| 127 | | <!doctype html> |
| 128 | | <head><title>Mojolicious 掲示板</title></head> |
| 129 | | <body> |
| 130 | | <h1>Mojolicious 掲示板</h1> |
| 131 | | <form method="post" action="<%= $c->url_for %>"> |
| 132 | | <input type="text" name="body"> |
| 133 | | <input type="submit" value="送信"> |
| 134 | | </form> |
| 135 | | |
| 136 | | <ul> |
| 137 | | % for my $entry (@{$c->stash->{rows}}) { |
| 138 | | <li><%= $entry->[0] %> <%= $entry->[1] %></li> |
| 139 | | % } |
| 140 | | </ul> |
| 141 | | </body> |
| 142 | | </html> |
| 143 | | </code></pre> |
| | 134 | <pre> |
| | 135 | % my $c = shift |
| | 136 | <!doctype html> |
| | 137 | <head><title>Mojolicious 掲示板</title></head> |
| | 138 | <body> |
| | 139 | <h1>Mojolicious 掲示板</h1> |
| | 140 | <form method="post" action="<%= $c->url_for %>"> |
| | 141 | <input type="text" name="body"> |
| | 142 | <input type="submit" value="送信"> |
| | 143 | </form> |
| | 144 | |
| | 145 | <ul> |
| | 146 | % for my $entry (@{$c->stash->{rows}}) { |
| | 147 | <li><%= $entry->[0] %> <%= $entry->[1] %></li> |
| | 148 | % } |
| | 149 | </ul> |
| | 150 | </body> |
| | 151 | </html> |
| | 152 | </pre> |
| 147 | | <pre><code>package BBS::Example; |
| 148 | | use strict; |
| 149 | | use warnings; |
| 150 | | |
| 151 | | use base 'Mojolicious::Controller'; |
| 152 | | use DBI; |
| 153 | | |
| 154 | | sub welcome { |
| 155 | | my ($self, $c) = @_; |
| 156 | | my $path = '/tmp/bbs.sqlite'; |
| 157 | | |
| 158 | | # データベースハンドラをひらく |
| 159 | | my $dbh = DBI->connect("dbi:SQLite:dbname=${path}", '', '') or die $DBI::errstr; |
| 160 | | # schema を定義する |
| 161 | | $dbh->do(q{CREATE TABLE IF NOT EXISTS entries (id INTEGER PRIMARY KEY, body VARCHAR(255))}) or die $dbh->errstr; |
| 162 | | |
| 163 | | if ($c->tx->req->method eq 'POST') { |
| 164 | | # POST のときは投稿。 |
| 165 | | |
| 166 | | # クエリをとりだす |
| 167 | | my $body = $c->tx->req->params->param('body') || ''; |
| 168 | | |
| 169 | | # INSERT する |
| 170 | | if ($body) { |
| 171 | | my $sth = $dbh->prepare('INSERT INTO entries (body) VALUES (?)') or die $dbh->errstr; |
| 172 | | $sth->execute($body) or die $dbh->errstr; |
| | 156 | <p>DBI をつかうので</p> |
| | 157 | <pre> |
| | 158 | sudo -H cpan DBI DBD::SQLite |
| | 159 | </pre> |
| | 160 | <p>しておきます</p> |
| | 161 | |
| | 162 | <pre> |
| | 163 | package BBS::Example; |
| | 164 | use strict; |
| | 165 | use warnings; |
| | 166 | |
| | 167 | use base 'Mojolicious::Controller'; |
| | 168 | use DBI; |
| | 169 | |
| | 170 | sub welcome { |
| | 171 | my ($self, $c) = @_; |
| | 172 | my $path = '/tmp/bbs.sqlite'; |
| | 173 | |
| | 174 | # データベースハンドラをひらく |
| | 175 | my $dbh = DBI->connect("dbi:SQLite:dbname=${path}", '', '') or die $DBI::errstr; |
| | 176 | # schema を定義する |
| | 177 | $dbh->do(q{CREATE TABLE IF NOT EXISTS entries (id INTEGER PRIMARY KEY, body VARCHAR(255))}) or die $dbh->errstr; |
| | 178 | |
| | 179 | if ($c->tx->req->method eq 'POST') { |
| | 180 | # POST のときは投稿。 |
| | 181 | |
| | 182 | # クエリをとりだす |
| | 183 | my $body = $c->tx->req->params->param('body') || ''; |
| | 184 | |
| | 185 | # INSERT する |
| | 186 | if ($body) { |
| | 187 | my $sth = $dbh->prepare('INSERT INTO entries (body) VALUES (?)') or die $dbh->errstr; |
| | 188 | $sth->execute($body) or die $dbh->errstr; |
| | 189 | } |
| | 190 | |
| | 191 | # リダイレクトする |
| | 192 | my $res = $c->tx->res; |
| | 193 | $res->code(302); |
| | 194 | $res->headers->header('Location' => $c->tx->req->url); # 本当は URL をとりだすコードのかきかたがあるとおもうのだけど、うまくうごかない |
| | 195 | } else { |
| | 196 | # GET のときは掲示板表示 |
| | 197 | my $sth = $dbh->prepare('SELECT id, body FROM entries ORDER BY id DESC LIMIT 30') or die $dbh->errstr; |
| | 198 | $sth->execute() or die $dbh->errstr; # 実行 |
| | 199 | my $rows = $sth->fetchall_arrayref(); # 取得 |
| | 200 | $c->stash->{rows} = $rows; # テンプレートエンジンにわたす |
| | 201 | $c->render(); # テンプレートエンジンで描画 |