Laravelでお問い合わせフォームの作成
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',時間と言語を変更。
どこにアクセスすると、どのファイルを読み込むかを定義する
web.phpで読み込むファイルをここに作っていく
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()関数を使用せずとも同じ動きになる。
<?php $num = 2 ?>
@if ($num > 5)
<p>こんにちは</p>
@elseif ($num > 3)
<p>こんばんは</p>
@else
<p>おはよう</p>
@endif@for ($i = 1; $i <= 10; $i++)
<li>{{ $i }}回目です</li>
@endfor<?php $count = 1 ?>
<ul>
@while ($count <= 10)
<li>{{ $count++ }}回目です</li>
@endwhile
</ul><?php $items = ['バナナ', 'りんご', 'いちご'] ?>
<ul>
@foreach ($items as $item)
<li>{{ $item }}</li>
@endforeach@empty($data)
<p>やっほー</p>
@endemptyデータが空のときに処理をする。
<?php $items = [] ?>
<ul>
@forelse ($items as $item)
<li>{{ $item }}</li>
@empty
<li>データがありません。</li>
@endforelse
</ul>$itemsは宣言しておかないとエラーになる。
ルーティングの定義
変数の定義、テンプレートファイルの呼び出しなど
テンプレート。実際に表示されるページ
コントローラーの作成
php artisan make:controller SampleController<?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']);SampleController::class
SampleControllerファイルのclassを呼び出せるようになる
‘index’
classを指定する
<?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()で渡す。
<!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>publicフォルダの中にcssフォルダを作成し、その中にstyle.cssを作成する。
読み込みはblade記法を使用する。
<link rel="stylesheet" href="{{ url('css/style.css') }}"><!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><x-layout>
<x-slot name="title">part1</x-slot>
テストあああ111
</x-layout>こっちの方がよく使う。
<!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>@extends('layouts.layout')
@section('title')
part2
@endsection
@section('body')
part2のページです。
@endsection