Laravel 5.1 尝试 post 数据到控制器,但收到 MethodNotAllowedHttpException 错误
Laravel 5.1 Trying to post data to controller but geting MethodNotAllowedHttpException error
我正在尝试 POST
向我的控制器发送数据,但我收到
MethodNotAllowedHttpException in RouteCollection.php line 219:
错误信息,这是我的文件。
我的路线文件
<?php
Route::get('/', function () {
return view('welcome');
});
// Authentication routes
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');
// Registration routes
Route::get('register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');
Route::controllers(['password' => 'Auth\PasswordController',]);
Route::get('/home', 'HomeController@index');
// Using A Route Closure
Route::get('profile', ['middleware' => 'auth', function() {
// Only authenticated users may enter...
Route::auth();
}]);
// practicing using forms for sending data to the DB & populating form fields with DB data
Route::get('profile', 'ProfileController@index');
Route::post('profile/update', 'ProfileController@updateProfile');
profile.blade.php
<form method="POST" action="/profile/update/">
<div class="form-group hidden">
<input type="hidden" name="id" value="<?php echo $users[0]->id;?>">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input name="_method" type="hidden" value="PATCH">
</div>
<div class="form-group">
<label for="email"><b>Name:</b></label>
<input type="text" name="name" placeholder="Please enter your email here" class="form-control"/>
</div>
<div class="form-group">
<label for="email"><b>Email:</b></label>
<input type="text" name="email" placeholder="Please enter your email here" class="form-control"/>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default"> Submit </button>
</div>
</form>
&我的ProfileController.php
<?php
namespace App\Http\Controllers;
use Auth;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ProfileController extends Controller
{
/**
* Update user profile & make backend push to DB
**/
public function index()
{
if(Auth::check()) {
// connecting to the DB and accessing
$users = User::all();
//var_dump($users);
return view('profile', compact('users'));
}
return view('auth/login');
}
public function updateProfile(Requests $request) {
return $request->all();
}
}
不确定是什么问题。感谢大家的帮助
我们在这里设法解决了几个问题:
过度使用 HTTP 动词
在您看来,您有:<form method="POST"
,还有 <input name="_method" type="hidden" value="PATCH">
,这可能在 POST
和 PATCH
之间发生冲突。由于您的 routes.php
仅声明了 POST
,让我们删除 patch
定义。
路由错误
在您看来,您的操作指向 action="/profile/update/"
,而您的路线定义为 Route::post('profile/update')
,请注意表单末尾的额外 /
。那个斜杠不应该在那里。
控制器请求
您在此处:use App\Http\Requests;
可能不正确,因为它是 Laravel 中的一个文件夹,而不是 class。让我们删除它并暂时保留 use Illuminate\Http\Request;
。在不久的将来,您将学习如何创建自己的表单请求,并且您可能需要一个 UpdateProfileRequest.php
。
我正在尝试 POST
向我的控制器发送数据,但我收到
MethodNotAllowedHttpException in RouteCollection.php line 219:
错误信息,这是我的文件。
我的路线文件
<?php
Route::get('/', function () {
return view('welcome');
});
// Authentication routes
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');
// Registration routes
Route::get('register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');
Route::controllers(['password' => 'Auth\PasswordController',]);
Route::get('/home', 'HomeController@index');
// Using A Route Closure
Route::get('profile', ['middleware' => 'auth', function() {
// Only authenticated users may enter...
Route::auth();
}]);
// practicing using forms for sending data to the DB & populating form fields with DB data
Route::get('profile', 'ProfileController@index');
Route::post('profile/update', 'ProfileController@updateProfile');
profile.blade.php
<form method="POST" action="/profile/update/">
<div class="form-group hidden">
<input type="hidden" name="id" value="<?php echo $users[0]->id;?>">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input name="_method" type="hidden" value="PATCH">
</div>
<div class="form-group">
<label for="email"><b>Name:</b></label>
<input type="text" name="name" placeholder="Please enter your email here" class="form-control"/>
</div>
<div class="form-group">
<label for="email"><b>Email:</b></label>
<input type="text" name="email" placeholder="Please enter your email here" class="form-control"/>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default"> Submit </button>
</div>
</form>
&我的ProfileController.php
<?php
namespace App\Http\Controllers;
use Auth;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ProfileController extends Controller
{
/**
* Update user profile & make backend push to DB
**/
public function index()
{
if(Auth::check()) {
// connecting to the DB and accessing
$users = User::all();
//var_dump($users);
return view('profile', compact('users'));
}
return view('auth/login');
}
public function updateProfile(Requests $request) {
return $request->all();
}
}
不确定是什么问题。感谢大家的帮助
我们在这里设法解决了几个问题:
过度使用 HTTP 动词
在您看来,您有:<form method="POST"
,还有 <input name="_method" type="hidden" value="PATCH">
,这可能在 POST
和 PATCH
之间发生冲突。由于您的 routes.php
仅声明了 POST
,让我们删除 patch
定义。
路由错误
在您看来,您的操作指向 action="/profile/update/"
,而您的路线定义为 Route::post('profile/update')
,请注意表单末尾的额外 /
。那个斜杠不应该在那里。
控制器请求
您在此处:use App\Http\Requests;
可能不正确,因为它是 Laravel 中的一个文件夹,而不是 class。让我们删除它并暂时保留 use Illuminate\Http\Request;
。在不久的将来,您将学习如何创建自己的表单请求,并且您可能需要一个 UpdateProfileRequest.php
。