Laravel

Laravelの設定とルーティングとblade記法とコントローラー

kurumin

起動

Dockerコンテナで環境構築を行う。
ssh-serber、php-mysqlnd、httpd、composerなどをインストール。
Laravelのstorage配下の所有者はapacheに変更する。

php artisan serve

設定

.env

APP_PORT=8456

ポートを追加。

config > app.php

'timezone' => 'Asia/Tokyo',
'locale' => 'ja',

時間と言語を変更。

ルーティング

routes/web.php

どこにアクセスすると、どのファイルを読み込むかを定義する

resources/views/●●.blade.php

web.phpで読み込むファイルをここに作っていく

blade記法

blade記法とは

Laravelに用意された、簡単にphpを書くための記法。
テンプレートファイルの名前に.bladeを書く必要がある。

変数

<?php
$test = 'てすとです'
?>

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <ul>
    <li>てすとです</li>
    <li><?php echo htmlspecialchars($test) ?></li>
    <li>{{ $test }}</li>
  </ul>
</body>
</html>

blade記法は{{ 変数 }}で出力ができる。
また、htmlspecialchars()関数を使用せずとも同じ動きになる。

If文

<?php $num = 2 ?>
@if ($num > 5)
  <p>こんにちは</p>
@elseif ($num > 3)
  <p>こんばんは</p>
@else
  <p>おはよう</p>
@endif

for文

@for ($i = 1; $i <= 10; $i++)
  <li>{{ $i }}回目です</li>
@endfor

while文

<?php $count = 1 ?>
<ul>
@while ($count <= 10)
  <li>{{ $count++ }}回目です</li>
@endwhile
</ul>

foreach文

<?php $items = ['バナナ', 'りんご', 'いちご'] ?>
<ul>
@foreach ($items as $item)
  <li>{{ $item }}</li>
@endforeach

empty

@empty($data)
<p>やっほー</p>
@endempty

データが空のときに処理をする。

foreachとemptyの組み合わせ

<?php $items = [] ?>
<ul>
@forelse ($items as $item)
  <li>{{ $item }}</li>
@empty
  <li>データがありません。</li>
@endforelse
</ul>

$itemsは宣言しておかないとエラーになる。

コントローラー

/routes/web.php

ルーティングの定義

/app/Http/contoller

変数の定義、テンプレートファイルの呼び出しなど

resources/views

テンプレート。実際に表示されるページ

コントローラーの作成

php artisan make:controller SampleController

wep.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SampleController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', [SampleController::class, 'index']);

Route::get('/sample', [SampleController::class, 'sample']);
Route::get(‘/’, [SampleController::class, ‘index’]);

SampleController::class
SampleControllerファイルのclassを呼び出せるようになる

‘index’
classを指定する

SampleController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SampleController extends Controller
{
    public function index () {
        return view('index');
    }

    public function sample () {
        $num = 2;
        return view('sample') ->with(['num' => $num]);
    }
}

変数の定義などはコントローラーで行うのが一般的。
変数はwith()で渡す。

sample.blade.php


<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  @if ($num > 5)
    <p>こんにちは</p>
  @elseif ($num > 3)
    <p>こんばんは</p>
  @else
    <p>おはよう</p>
  @endif
</body>
</html>

CSS

publicフォルダの中にcssフォルダを作成し、その中にstyle.cssを作成する。

読み込みはblade記法を使用する。

<link rel="stylesheet" href="{{ url('css/style.css') }}">

レイアウト

パターン1(コンポーネント)

resources/views/components/layout.blade.php

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>{{ $title }}</title>
</head>
<body>
  <div class="container">
    {{ $slot }}
  </div>
</body>
</html>

/resources/views/part1.blade.php

<x-layout>
  <x-slot name="title">part1</x-slot>
  テストあああ111
</x-layout>

パターン2

こっちの方がよく使う。

resources/views/components/layout.blade.php

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>@yield('title')</title>
</head>
<body>
  <div class="container">
    @yield('body')
  </div>
</body>
</html>

/resources/views/part2.blade.php

@extends('layouts.layout')

@section('title')
  part2
@endsection

@section('body')
  part2のページです。
@endsection
ABOUT ME
記事URLをコピーしました