LoginSignup
38

More than 5 years have passed since last update.

MacのVagrantでPHPフレームワークLaravelを試す。

Last updated at Posted at 2014-12-16

PHPの今流行りのフレームワーク

Macで人気PHPフレームワークのLaravelをMacの仮想サーバー上にさくっと入れて動かしてみます。
MacOSX10.10 Yosemiteです。
Vagrant、VirtualBox、git、composerはインストール済みとします。

VagrantのBox

個人的にVagrantはPHP開発になくてはならないものになりました。
開発環境が数分で手に入りますし、不要になったら気軽に捨てることができます。

ということで仮想サーバーの基であるいい感じのBoxを取ってきます。
http://box.scotch.io/ ←このBoxを使います。

laravelTestというプロジェクトを作ります。

下準備

$cd ~
$mkdir Project
$cd Project

Vagrantfileクローン

$git clone https://github.com/scotch-io/scotch-box.git laravelTest
$cd laravelTest
$ls // README.md   Vagrantfile public

Vagrantfile修正

vi Vagrantfile

synced_folderの"." -> "./www"にします。
hostnameの"scotchbox" -> "first-laravel"にします。

編集後Vagrantfile
Vagrant.configure("2") do |config|

  config.vm.box = "scotch/box"
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.hostname = "first-laravel"
  config.vm.synced_folder "./www", "/var/www", :mount_options => ["dmode=777", "fmode=666"]

end

publicディレクトリ不要なので削除します。

rm -rf public

Laravelインストール

composerを使ってインストールします。

composer create-project laravel/laravel --prefer-dist www

Vagrant起動します。

vagrant up

途中でローカルの/etc/hostsファイルを変更するためにMacのパスワードを求められるので入力します。

Laravel確認

ブラウザでhttp://first-laravel/ と入力してLaravelのアイコンが出ればOKです。

Laravelちょっとだけ触ってみる。

ルーティングのファイルに追記します。

vi ~/Project/laravelTest/www/app/routes.php
app/routes.php
<?php
Route::get('/', function()
{
    return View::make('hello');
});

// 追記
Route::get('/abc', function()
{
    return 'Hello!';
});

ブラウザでhttp://first-laravel/abc と入力して「Hello!」と表示されればOKです。

Laravelを初めて試した時にまず、このクロージャーな感じにワクワクしました。
古臭いPHPから脱却したいと思っている方々はきっと惹きつけられる!

インストールできたらコントローラーあたりから始めるのがおすすめです。
http://laravel.com/docs/4.2/controllers

Vagrant破棄

Vagrantは不要なら破棄しておきましょう。

vagrant destroy

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38