テキストエリアに文字を入力して送信を押すと、どんな文字が入力されたのかテキストエリアの上に表示されるシンプルなformを試しに書いてみたのですが、
hi.blade.php内のフォームとroutes.phpはpostでデータを渡すように設定しているだけなのですが、
http://localhost:8000/hiにアクセスすると
MethodNotAllowedHttpException in RouteCollection.php line 218:
というエラーが表示されてしまいます。
どのようにしたらpostで値の受け渡しができるようになるでしょうか?
アドバイスとご指導のほどお願いします。
php
1app/Http/routes.php 2 3<?php 4 5/* 6|-------------------------------------------------------------------------- 7| Application Routes 8|-------------------------------------------------------------------------- 9| 10| Here is where you can register all of the routes for an application. 11| It's a breeze. Simply tell Laravel the URIs it should respond to 12| and give it the controller to call when that URI is requested. 13| 14*/ 15 16Route::get('/', function () { 17 #return view('welcome'); 18 return '<html><body><h1>Hello World</h1></body></html>'; 19}); 20 21Route::post('/hi', 'HeloController@hi'); 22
php
1 2app/Http/Controllers/HeloController.php 3 4<?php 5 6namespace App\Http\Controllers; 7 8use Illuminate\Http\Request; 9 10use App\Http\Requests; 11use App\Http\Controllers\Controller; 12 13class HeloController extends Controller 14{ 15 /** 16 * Display a listing of the resource. 17 * 18 * @return \Illuminate\Http\Response 19 */ 20 public function index(Request $request) 21 { 22 23 $data = MyTable::all(); 24 return view('helo', ['message' => 'MyTable List','data' => $data]); 25 } 26 27 public function hi(Request $request) 28 { 29 $res = "you typed: " . $request->input('str'); 30 return view('hi', ['message' => $res]); 31 } 32}
php
1hi.blade.php 2 3<!doctype html> 4<html> 5<head> 6 <title>Sample</title> 7 <style> 8 body { color:gray; } 9 h1 { font-size:18pt; font-weight:bold; } 10 </style> 11</head> 12<body> 13 <h1>Sample</h1> 14 <p><?php echo $message; ?></p> 15 <form method="post" action="/hi"> 16 {{ csrf_field() }} 17 <input type="text" name="str"> 18 <input type="submit"> 19 </form> 20</body>

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/11/23 11:53 編集