Laravel 5.4: 環境設定
DotEnvライブラリ
設定ファイル
.env
: 各環境で使う設定ファイル- git管理対象外
.env.example
: サンプルファイル- git管理対象
$ diff .env.example .env 2c2 < APP_KEY= --- > APP_KEY=base64key...
設定の適用
- 設定ファイルの記述
.env
DB_DATABASE=homestead
- 設定の適用
.env
から値が取れないとき第二引数の値が入る
env('DB_DATABASE', 'foo')
- 設定の更新
storage/framework/config.php
が更新
$ php artisan config:cache
環境名の取得
use App; $environment = App::environment(); // get "local" if (App::environment('local')) { // The environment is local } if (App::environment('local', 'staging')) { // The environment is either local OR staging... }
TODO
- 各環境名の設定方法
- 実際に各環境を立てる
参照
Laravel 5.4: Blade テンプレートを使う
if
@if (count($people)) @endif
foreach
@foreach ($people as $person) {{ $person }} @endforeach
Template
- テンプレート側:
@yield('bar')
- テンプレート利用側:
- Fooテンプレートの継承:
@extends('foo')
- セクション書き換え:
@section('bar')
,@stop
- Fooテンプレートの継承:
- テンプレート:
resources/views/app.blade.php
<body> <div class="container"> @yield('content') </div> @yield('footer') </body>
- テンプレート利用:
resources/views/pages/about.blade.php
@extends('app') @section('content') <h1>About {{$name}}</h1> @if (count($people)) <h3>People I Like:</h3> <ul> @foreach ($people as $person) <li>{{ $person }}</li> @endforeach </ul> @endif @stop
参照
Laravel 5.4: Viewへデータを引き渡す
Summary
- テンプレートはBladeファイル:
foo.blade.php
withを使ってControllerからViewへ渡す
Controller側:
view('pages.about')->with('name', $name)
- View側:
- エスケープ:
{{$bar}}
- 非エスケープ:
{!! $bar !!}
- エスケープ:
How to basic
app/Http/routes.php
Route::get('about', 'PagesController@about');
- コントローラ作成
$ php artisan make:controller PagesController
- aboutメソッド作成
app/Http/Controllers/PagesController.php
class PagesController extends Controller { public function about() { $name = 'Jeffery Way'; return view('pages.about')->with('name', $name); } }
- テンプレート作成
resources/views/pages/about.blade.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h1>About {{$name}}</h1> </body> </html>
How to advance
エスケープ
- エスケープ:
{{$bar}}
- 非エスケープ:
{!! $bar !!}
- エスケープ:
データ渡しにwithを使う
- 単数:
- Controller:
view('pages.about')->with('name', $name)
- Template:
{{$name}}
- Controller:
- 複数(配列):
- Controller:
view('pages.about')->with(['first' => 'Jeffery', 'last' => Way'])
- Template:
{{$first}} {{$last}}
- Controller:
- 単数:
- データ渡しにwithを使わない
- 単数:
- Controller:
view('pages.about', $data)
- Template:
{{$first}} {{$last}}
- Controller:
- 複数(連想配列):
- Controller:
view('pages.about', $data)
- Template:
{{$name['first']}} {{$name['last']}}
- Controller:
- 複数(別変数):
- Controller:
view('pages.about', compact('first', 'last'))
- Template:
{{$first}} {{$last}}
- Controller:
- 単数:
参考
Laravel 5.4: Routing, Controller, Viewの流れを触る
What?
- ざっくり最小のRouting, Controller, Viewの流れを確認する
Why?
- ざっくり最小のものを作りたい
Routing
routes/
: ルーティングディレクトリ- ver 5.3 から
app/Http/routes.php
は廃止
- ver 5.3 から
$ less routes/ api.php channels.php console.php web.php
Route::get('/', 'welcome');
or
Route::get('/', function () { return view('welcome'); });
Controller
app/Http/Controllers/
以下にあるapp/Http/Controllers/WelcomeController.php
<?php namespace App\Http\Controllers; class WelcomeController extends Controller { public function index() { return view('welcome'); } }
View
- blade テンプレートエンジンを使用
resources/views/
以下にあるresources/views/welcome.blade.php
参考
Laravel5 入門 インストールからMVCの基本機能をさっと眺める | hrendoh's memo http://site.oganity.pw/154/
apt-getとyumの違い
Why?
同じところ
- apt も yum もパッケージ管理ソフト
- 自動ダウンロード、インストール、アップグレード
違うところ
apt
参考
Linux パッケージ管理コマンド比較(yum / dnf / apt 等) - らくがきちょう
TODO
Ubuntu に Java インストール
What?
Why?
- NeobundleInstall を叩いたときにJavaがなくてコケた
Solution
Apt-get でインストールする
Ubuntu version
$ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=16.04 DISTRIB_CODENAME=xenial DISTRIB_DESCRIPTION="Ubuntu 16.04.1 LTS"
- パッケージのIndexをアップデートする
$ sudo apt-get update
- Javaがインストールされているか確認
$ java -version bash: java: command not found
$ sudo apt-get install default-jre
- Apt-getでJava Development Kit (JDK)をインストールする
$ sudo apt-get install default-jdk
参考
How To Install Java on Ubuntu with Apt-Get | DigitalOcean
TODO List
Laravel homesteadで最初のVagrant upが止まる
What?
- ホストPC上では、以下で止まってエラー無しでタイムアウトする
$ vagrant up Bringing machine 'homestead-7' up with 'virtualbox' provider... ==> homestead-7: Checking if box 'laravel/homestead' is up to date... ==> homestead-7: Clearing any previously set forwarded ports... ==> homestead-7: Clearing any previously set network interfaces... ==> homestead-7: Preparing network interfaces based on configuration... homestead-7: Adapter 1: nat homestead-7: Adapter 2: hostonly ==> homestead-7: Forwarding ports... homestead-7: 80 (guest) => 8000 (host) (adapter 1) homestead-7: 443 (guest) => 44300 (host) (adapter 1) homestead-7: 3306 (guest) => 33060 (host) (adapter 1) homestead-7: 5432 (guest) => 54320 (host) (adapter 1) homestead-7: 27017 (guest) => 27017 (host) (adapter 1) homestead-7: 22 (guest) => 2222 (host) (adapter 1) ==> homestead-7: Running 'pre-boot' VM customizations... ==> homestead-7: Booting VM... ==> homestead-7: Waiting for machine to boot. This may take a few minutes... homestead-7: SSH address: 127.0.0.1:2222 homestead-7: SSH username: vagrant homestead-7: SSH auth method: private key Timed out while waiting for the machine to boot. This means that Vagrant was unable to communicate with the guest machine within the configured ("config.vm.boot_timeout" value) time period. If you look above, you should be able to see the error(s) that Vagrant had when attempting to connect to the machine. These errors are usually good hints as to what may be wrong. If you're using a custom box, make sure that networking is properly working and you're able to connect to the machine. It is a common problem that networking isn't setup properly in these boxes. Verify that authentication configurations are also setup properly, as well. If the box appears to be booting properly, you may want to increase the timeout ("config.vm.boot_timeout") value.
- ゲストPCをVirtualbox GUIから直接コンソールをShowすると、以下で止まっている
A start job is running for raise network interfaces (5 mins 8 sec)
Why?
- Virtualboxの
Network > Adapter 1(NAT) > Advanced > Cable Connected
のチェックが外れていただけだった。。
Solution?
Virtualboxの
Network > Adapter 1(NAT) > Advanced > Cable Connected
のチェックを入れて、再度vagrant upもし再度vagrant upして止まったら一度destroyしてから、再度up
判別手順
- Vagrantではエラーが出ずに止まる
- VirtualboxのGUIからShowしてコンソールを確認
A start job is running for raise network interfaces
ネットワークの何かを延々と待っているっぽい
- VirtualboxのGUIからShowしてコンソールを確認
- Virtualboxにそもそも普通にssh接続できるのか
ssh vagrant@192.168.10.10 -p 2222
がタイムアウトする- Virtualboxまでネットワークが到達してない
- Virtualboxのネットワーク設定がおかしいのか
- NATとHost-only Adapterは設定されてる?
- されてる
- Port Forwardingは設定されてる?
- ssh Host 2222 -> Guest 22 に設定されてる
- Virtualbox上でネットワーク設定で通常と違うところは?
- Cable Connectedが。。。チェックが外れてる。。。
- NATとHost-only Adapterは設定されてる?
- Vagrantではエラーが出ずに止まる
無駄に頑張ったこと
- (GUIから見る前)Virtualboxが立ち上がる前に死んだと思った
- VirtualboxにGUIから入ってShowすると普通に生きてた
- ssh認証設定が間違っていると思っていた
~/.homestead/Homestead.yaml
のssh keyの所在を確認したが、問題ない- そもそもauthorisationエラーが出てない
- Hostsの設定が間違っていると思っていた
~/.homestead/Homestead.yaml
のIP設定がおかしいと思った- そもそも立ち上げる前なので関係ない
- ここが間違っているとIPorドメインでアクセスしようとしたときに死ぬはず
- Vagrant, Virtualbox のバージョンが古いと思った
- バージョンが古ければエラーが出るはずだけど、一応念を入れて最新にした