Contents
流れを決める
firstフォルダにアクセスしたらFirstControllerのindexアクションを実行する
→というのを考えます。
ルーティングの設定
laravel/routes/web.phpに流れに対応したルーティングを設定します。
1 |
Route::get('first', 'FirstController@index'); |
コントローラの作成
1 2 |
C:\var\www\vred\laravel>php artisan make:controller FirstController Controller created successfully. |
C:\var\www\vred\laravel\app\Http\ControllersにFirstController.phpが作成されます。
1 2 3 4 5 6 7 8 9 10 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class FirstController extends Controller { // } |
アクションの追加
このファイルにindexアクションを記述します。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class FirstController extends Controller { public function index() { return view('first'); } } |
ビューの作成
indexアクションのview(”)はwelcomeと同じです。
first.blade.phpをlaravel/resources/viewへ作成・設置します。
1 2 3 4 5 6 7 8 9 10 |
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>First Page</title> </head> <body> <p>Hello FirstPage!!</p> </body> </html> |
出力
データの受け渡し
コントローラのアクションで、データを生成して、ビューに渡して表示させます。
コントローラのアクション変更
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public function index() { // 配列の初期化 $data = array(); // データ格納 $data['name'] = 'きゆかた'; $data['message'] = 'こんにちは'; // 現在日時 $today = date('Y年m月d日 H:i:s'); return view('first', ['data' => $data, 'today' => $today]); } |
ビューの変更
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>First Page</title> </head> <body> <p>Hello FirstPage!!</p> <p>{{$data['name']}}さん</p> <p>{{$data['message']}}</p> <p>現在は {{$today}} です</p> </body> </html> |
出力
参考にしたサイト
ここです。