laravelにおいて、postした際のページの遷移について、お聞きしたいことがございます。
以下は、コントローラー部分のHelloController.phpです。
php
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use Illuminate\Http\Response; 7use App\Http\Requests\HelloRequest; 8use Validator; 9use Illuminate\Support\Facades\DB; 10 11 12class HelloController extends Controller{ 13 14 public function index(Request $request) 15 { 16 17 18 $items = DB::select('select * from people'); 19 return view('hello.index',['items' => $items]); 20 21 } 22 23 public function post(Request $request) 24 25 { 26 $items = DB::select('select * from people'); 27 28 return view('hello.index',['items' => $items]); 29 30 31 } 32 public function add(Request $request) 33 { 34 return view('hello.add'); 35 } 36 37 public function create(Request $request) 38 { 39 $param = [ 40 'name' => $request->name, 41 'mail' => $request->mail, 42 'age' => $request->age, 43 ]; 44 45 DB::insert('insert into people (name, mail, age) values 46 (:name, :mail,:age)',$param); 47 return redirect('/hello'); 48 } 49 50 51 52 53} 54 55
以下は、ルート情報を記述したweb.phpです
php
1<?php 2 3/* 4|-------------------------------------------------------------------------- 5| Web Routes 6|-------------------------------------------------------------------------- 7| 8| Here is where you can register web routes for your application. These 9| routes are loaded by the RouteServiceProvider within a group which 10| contains the "web" middleware group. Now create something great! 11| 12*/ 13 14 15// Route::post('hello','HelloContoroller@post'); 16 17//use App\Http\Middleware\HelloMiddleware; 18 19Route::get('hello/add','HelloController@add'); 20Route::post('hello/add','HelloController@create'); 21 22Route::get('hello','HelloController@index'); 23
以下は、ビュー部分であるadd.blade.phpです
php
1@extends('layouts.helloapp') 2@section('title','Add') 3@section('menubar') 4 @parent 5 新規作成ページ 6@endsection 7 8@section('content') 9 <table> 10 <form action="/laravelapp/public/hello/add" method="post"> 11 {{ csrf_field() }} 12 <tr><th>name:</th><td><input type="text" name="name"></td></tr> 13 <tr><th>mail:</th><td><input type="text"></td></tr> 14 <tr><th>age: </th><td><input type="text" name="age"></td></tr> 15 <tr><th></th><td><input type="submit" value="send"></td></tr> 16 </form> 17 </table> 18 @endsection 19 20 @section('footer') 21 copyright 2017 tuyano. 22 @endsection
お聞きしたい部分なのですが、add.blade.phpからpost送信された場合、web.phpのRoute::post('hello/add','HelloController@create');こちらの処理が呼び出されるといった解釈であっていますでしょうか?
Route::postメソッドの第一引数である、「'hello/add'」はadd.blade.phpのフォームのactionで指定している、「/laravelapp/public/hello/add」ここと対になっており、add.blade.phpから送信された場合、Route::postメソッドが呼び出されるといった解釈であっていますでしょうか?
※現在の状況なのですが、add.blade.phpからpostするとタイムアウトとなってしまっている状況です。
