MVCで値が渡っていく流れがよく解らなかったので忘備録。
モデルは含まれません。
コントローラーからViewに渡す
アクセス
1 |
http://localhost:8093/hello/110 |
routes/web.php(Route)
1 |
Route::get('/hello','HelloController@index'); |
HelloController.php
1 2 3 4 5 6 |
$data = [ 'msg' => 'コントローラからのメッセージ', ]; // return view(テンプレート,配列); return view('hello.index',$data); |
hello.index.php(View)
1 |
<p>= $msg ?></p> |
ルートパラメーターからViewに渡す
アクセス
1 |
http://localhost:8093/hello/110 |
routes/web.php(Route)
1 |
Route::get('/hello/{id}','HelloController@index'); |
HelloController.php
1 2 3 4 5 6 7 8 9 10 |
publi funciton index($id) { $data = [ 'msg' => 'コントローラからのメッセージ', 'id' => $id ]; // return view(テンプレート,配列); return view('hello.index',$data); } |
hello.index.php(View)
1 |
<p>= $id ?></p> |
クエリー文字列からViewに渡す
アクセス
1 |
http://localhost:8093/hello?id=query |
routes/web.php(Route)
1 |
Route::get('/hello','HelloController@index'); |
HelloController.php
1 2 3 4 5 6 7 8 9 10 |
publi funciton index(Request $request) { $data = [ 'msg' => 'コントローラからのメッセージ', 'id' => $request->id, ]; // return view(テンプレート,配列); return view('hello.index',$data); } |
hello.index.php(View)
1 |
<p>= $id ?></p> |
フォームからViewへ
indexからindexへ
アクセス
1 |
http://localhost:8093/hello |
routes/web.php(Route)
1 2 |
Route::get('/hello','HelloController@index'); Route::post('/hello','HelloController@index'); |
HelloController.php
1 2 3 4 5 6 7 8 9 |
publi funciton index(Request $request) { $data = [ 'msg' => $request->msg, ]; // return view(テンプレート,配列); return view('hello.index',$data); } |
hello.index.php(View)
1 2 3 4 5 6 7 8 9 10 11 |
<h1>Blade/Index</h1> <p>{{$msg}}</p> <form action="/hello" method="POST"> {{ csrf_field() }} <input type="text" name="msg"> <input type="submit"> </form> |
indexからpostへ
アクセス
1 |
http://localhost:8093/hello |
routes/web.php(Route)
1 2 |
Route::get('/hello','HelloController@index'); Route::post('/hello','HelloController@post'); |
HelloController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public function index() { $data = [ 'msg' => 'メッセージをどうぞ', ]; return view('hello.index',$data); } public function post(Request $request){ $data = [ 'msg' => $request->msg, ]; return view('hello.index',$data); } |
hello.index.php(View)
1 2 3 4 5 6 7 8 9 10 11 |
<h1>Blade/Index</h1> <p>{{$msg}}</p> <form action="/hello" method="POST"> {{ csrf_field() }} <input type="text" name="msg"> <input type="submit"> </form> |
コメント