⇒ composer globalrequire"laravel/installer" Changed current directory to /Users/PMAC025S/.composer
Using version ^1.3for laravel/installer ./composer.json has been created Loading composer repositories with package information Updating dependencies (including require-dev) - Installing symfony/process (v3.0.4) Downloading: 100%
- Installing symfony/polyfill-mbstring (v1.1.1) Loading from cache
Options: -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question -v|vv|vvv, --verbose Increase the verbosity of messages:1fornormal output, 2for more verbose output and3fordebug
Available commands: help Displays helpforacommand list Lists commands new Create anew Laravel application.
laravel new application_name でインストールします。
1 2
⇒ laravel new blog Crafting application...
以下の構成で作成されています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
⇒ tree -L 1 blog blog ├── app ├── artisan ├── bootstrap ├── composer.json ├── composer.lock ├── config ├── database ├── gulpfile.js ├── package.json ├── phpunit.xml ├── public ├── readme.md ├── resources ├── server.php ├── storage ├── tests └── vendor
設定
設定情報はconfigディレクトリに保存します。 今回は設定を変更しません。
データベースを準備する
データベースのマイグレーション
Laravelに同梱されているartisanというツールを使って作成します。
1 2
⇒ php artisan make:migration create_tasks_table --create=tasks Created Migration:2016_04_28_011840_create_tasks_table
$ ruby server.rb == Sinatra (v1.4.7) has taken the stage on 4567 for development with backup from Thin Thin web server (v1.6.4 codename Gob Bluth) Maximum connections setto 1024 Listening on 0.0.0.0:4567, CTRL+C to stop
APIを実行
1 2
$ curl http://localhost:4567 -X POST -d "url=http://www.yahoo.co.jp/" => {"url":"http://www.yahoo.co.jp/","title":"Yahoo! JAPAN","description":"日本最大級のポータルサイト。検索、オークション、ニュース、メール、コミュニティ、ショッピング、など80以上のサービスを展開。あなたの生活をより豊かにする「ライフ・エンジン」を目指していきます。","robots":"noodp","google-site-verification":"fsLMOiigp5fIpCDMEVodQnQC7jIY1K3UXW5QkQcBmVs"}
その他
内部的にはNokogiriを使ってhtmlをパースしています。
文字化けして内容がみれない場合があったので、以下のワークアラウンドをいれました。
1 2
html = URI.parse(url).read node = Nokogiri::HTML(html.toutf8, nil, 'utf-8')
Status Codeを設定するstatusヘルパー、headerを設定するheadersヘルパーも用意されています。
1 2 3 4 5 6 7
get'/foo'do status 418 headers \ "Allow" => "BREW, POST, GET, PROPFIND, WHEN", "Refresh" => "Refresh: 20; http://www.ietf.org/rfc/rfc2324.txt" body "I'm a tea pot!" end
get'/subscribe'do # register a client's interest in server events stream(:keep_open) do |out| connections << out # purge dead connections connections.reject!(&:closed?) end end
post '/message'do connections.each do |out| # notify client that a new message has arrived out << params['message'] << "\n"