Laravelのインストール(Mac)
PHP、Xdebug、Composerをインストールする。
- PHPのインストール
brew install php@7.2
echo 'export PATH="/usr/local/opt/php@7.2/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/usr/local/opt/php@7.2/sbin:$PATH"' >> ~/.bash_profile
# ターミナルを再起動後
php -v
- Xdebug、Composerのインストール
pecl install xdebug
brew install composer
composer -V
Laravelをインストールする。
composer global require "Laravel/installer=~1.1"
- 環境変数の設定
echo 'export PATH="~/.composer/vendor/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
php.iniを修正する。
vi /usr/local/etc/php/7.2/php.ini
- php.iniの上部に以下を追記する。
zend_extension="xdebug.so"
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_port="9001"
xdebug.profiler_enable=0
xdebug.profiler_output_dir="/tmp"
xdebug.max_nesting_level=1000
xdebug.idekey = "PHPSTORM"
[PHP]
- phpを再起動する。
- php -vで、バージョンと共にXdebugなんちゃらと出ればOK
brew services restart php@7.2
php -v
プロジェクトの新規作成
laravel new laravelapp
アプリケーションの実行
cd laravelapp
php artisan serve
- http://localhost:8000/ にアクセスする。
Intellij IDEAでリモートデバッグする
chromeプラグインXdebug helperを入れる。
- chrome拡張アイコン右クリック→オプションで、
PHPSTORM
にして、saveする。
- 上記アイコンを、Debugにしておく(緑色になる)
Intellij IDEAにPHPプラグインを入れる。
Intellij IDEAでリモートデバッグ
- PHPのデバッグポートの設定。設定画面→PHP→Debugより、9000→9001に書き換える。
- リモートデバッグの設定。Run→Debug...→Edit Configurations→+→PHP Remote Debug
- 上記のServerの設定
- デバッガが動いていることを確認。`
ルート
- routes/web.phpに書く。
- ルート(
http://localhost:8000/
)は、resources/views/welcome.blade.php
を表示しろという例。
routes/web.php
Route::get('/', function () {
return view('welcome');
});
- ビューに値を渡す
-
http://localhost:8000/hello/my_message_string
にアクセスすると、hello.blade.php
を表示する例。 - パラメータは
hello.blade.php
内の{{$msg}}
でmy_message_string
を表示できる。
-
routes/web.php
Route::get('hello/{msg?}', function ($msg='no message.') {
return view('hello');
});
HelloWorldの作成
- ルートの設定を追加。
-
http://localhost:8000/hello
にアクセスした時、HelloControllerのindexメソッドを呼ぶ。
routes/web.php
Route::get('hello/{name?}', 'HelloController@index');
コントローラーの作成
php artisan make:controller HelloController
コントローラーにindexアクションの追加
- indexメソッドを追加する。
- パラメータは連想配列で渡す。
- テンプレートは、
フォルダ名
.ファイル名
で指定する。
Http/Controllers/HelloController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HelloController extends Controller
{
public function index($name = '名無し')
{
$data = ['msg' => 'こんにちは世界😃', 'name' => $name];
return view('hello.index', $data);
}
}
- テンプレートの作成。
resources/views/hello/index.blade.php
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Hello/Index</title>
</head>
<body>
<h1>Index</h1>
<p>{{$name}} さん {{$msg}}</p>
</body>
</html>
-
http://localhost:8000/hello/taro
にアクセスした時、$name='taro'が使われる。
-
http://localhost:8000/hello
にアクセスした時、msgは空なのでデフォルトが使われる。