2016/04/28
(更新日: 2016/04/28)
LaravelをMacにインストールして起動する手順
PHPで人気のフレームワーク、Laravel を手元のPCにインストールして起動するチュートリアルです。 冗長な説明を除きLaravelを動かすために重要な部分のみ解説することでスピーディにLaravelを動作できるようにしています。
Laravelのインストール まずはcomposerでlaravelコマンドをインストールします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ⇒ composer global require "laravel/installer" Changed current directory to /Users/PMAC025S/.composer Using version ^1.3 for 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 - Installing symfony/console (v3.0.4 ) Downloading: 100 % - Installing guzzlehttp/promises (1.1 .0 ) Downloading: 100 % - Installing psr/http-message (1.0 ) Loading from cache - Installing guzzlehttp/psr7 (1.3 .0 ) Downloading: 100 % - Installing guzzlehttp/guzzle (6.2 .0 ) Downloading: 100 % - Installing laravel/installer (v1.3.3 ) Downloading: 100 % symfony/console suggests installing symfony/event-dispatcher () symfony/console suggests installing psr/log (For using the console logger) Writing lock file Generating autoload files
.bashrc や .zshrc にパスを追加します。
1 2 export PATH ="$PATH :/Users/PMAC025S/.composer/vendor/bin"
これでlaravelコマンドが実行できるようになります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ⇒ laravel Laravel Installer version 1.3 .3 Usage: command [options ] [arguments ] Options: -h, -q, -V, -n, -v|vv|vvv, Available commands: help Displays help for a command list Lists commands new Create a new Laravel application.
laravel new application_name でインストールします。
1 2 3 ⇒ laravel new blog Crafting application...
以下の構成で作成されています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ⇒ 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 3 ⇒ php artisan make:migration create_tasks_table --create =tasks Created Migration: 2016_04_28_011840_create_tasks_table
database/migrationsディレクトリに作成されます。 2016_04_28_011840_の部分はartisanが作成するので、makeするタイミングによって変わります。
2016_04_28_011840_create_tasks_table.phpを編集して、nameカラムを追加します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <?php use Illuminate \Database \Schema \Blueprint ;use Illuminate \Database \Migrations \Migration ;class CreateTasksTable extends Migration { public function up ( ) { Schema ::create ('tasks' , function (Blueprint $table ) { $table ->increments ('id' ); $table ->string ('name' ); $table ->timestamps (); }); } public function down ( ) { Schema ::drop ('tasks' ); } }
.envファイルのデータベースの設定を自分のローカルのPCのmysqlに変更します。 デフォルトでは、Homestead というLaravel開発用の仮想環境の設定です(今回は使いません)。
1 2 3 4 5 6 7 DB_CONNECTION =mysqlDB_HOST =127.0.0.1DB_PORT =3306DB_DATABASE =homesteadDB_USERNAME =rootDB_PASSWORD=
マイグレーションを実行します。
1 2 3 4 5 6 ⇒ php artisan migrate Migration table created successfully. Migrated: 2014 _10_12_000000_create_users_tableMigrated: 2014 _10_12_100000_create_password_resets_tableMigrated: 2016 _04_28_011840_create_tasks_table
モデルを作成する LaravelはEloquent というORMがデフォルトで使われるようになっています。
artisanでモデルを作成します。
1 2 3 ⇒ php artisan make:model Task Model created successfully.
モデルは app ディレクトリ配下に作成されます。
ルーティング ルーティングの設定は、app/Http/routes.phpに定義します。 (ルーティングの設定はsinatraっぽい)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <?php use App \Task ;use Illuminate \Http \Request ;Route ::get ('/' , function () { return view ('tasks' ); }); Route ::post ('/task' , function (Request $request ) { }); Route ::delete ('/task/{task}' , function (Task $task ) { });
Viewの設定 LarvelはデフォルトでBlade というテンプレートエンジンを使います。 viewのディレクトリはresources/viewsです。
1 2 $ mkdir resources/views/layouts
全体のレイアウトのテンプレートとして使うapp.blade.phpを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <!DOCTYPE html > <html lang ="en" > <head > <title > Laravel Quickstart - Basic</title > </head > <body > <div class ="container" > <nav class ="navbar navbar-default" > </nav > </div > @yield('content') </body > </html >
tasks.blade.phpを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 @extends('layouts.app') @section('content') <div class ="panel-body" > <form action =" {{ url ('task' ) }} " method ="POST" class ="form-horizontal" > {!! csrf_field() !!} <div class ="form-group" > <label for ="task" class ="col-sm-3 control-label" > Task</label > <div class ="col-sm-6" > <input type ="text" name ="name" id ="task-name" class ="form-control" > </div > </div > <div class ="form-group" > <div class ="col-sm-offset-3 col-sm-6" > <button type ="submit" class ="btn btn-default" > <i class ="fa fa-plus" > </i > Add Task </button > </div > </div > </form > </div > @endsection
サーバーを起動する さて、一通りの設定が整いました。 サーバーを起動してブラウザで確認してみましょう。 以下のコマンドでサーバーを起動します。
1 2 3 ⇒ php artisan serve Laravel development server started on http:
http://localhost:8000/ にブラウザでアクセスします。
Taskという文字とフォームが表示されれば成功です!
参考文献