2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravelのインストール(Mac)、Intellijでのデバッグ、HelloWorldまで

Last updated at Posted at 2019-03-06

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

Intellij IDEAでリモートデバッグする

chromeプラグインXdebug helperを入れる。

image.png

  • chrome拡張アイコン右クリック→オプションで、PHPSTORMにして、saveする。

image.png

  • 上記アイコンを、Debugにしておく(緑色になる)

Intellij IDEAにPHPプラグインを入れる。

image.png

Intellij IDEAでリモートデバッグ

  • PHPのデバッグポートの設定。設定画面→PHP→Debugより、9000→9001に書き換える。

image.png

  • リモートデバッグの設定。Run→Debug...→Edit Configurations→+→PHP Remote Debug

image.png

  • 上記のServerの設定

image.png

  • デバッガが動いていることを確認。`

image.png

ルート

  • 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'が使われる。

image.png

  • http://localhost:8000/hello にアクセスした時、msgは空なのでデフォルトが使われる。

image.png

2
2
0

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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?