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

コメント