Laravel 5.1尝试将数据发布到控制器,但是发现MethodNotAllowedHttpException错误

时间:2016-07-07 18:55:23

标签: php routes laravel-5.1

我正在尝试将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();

    }
}

不确定问题是什么。感谢所有人的帮助

1 个答案:

答案 0 :(得分:1)

我们在此处解决了几个问题:

过度使用HTTP动词

根据您的观点,您有:<form method="POST",但<input name="_method" type="hidden" value="PATCH">可能会在POSTPATCH之间发生冲突。由于您的routes.php仅声明POST,因此请删除patch定义。

路由错误

仍然在您看来,当您的路线定义为action="/profile/update/"时,您的操作指向Route::post('profile/update'),请注意表单末尾的额外/。那个斜线不应该在那里。

控制器请求

你有一个:use App\Http\Requests;可能不正确,因为它是Laravel中的一个文件夹,而不是一个类。让我们删除它并暂时保留use Illuminate\Http\Request;。在不久的将来,您将学习如何创建自己的表单请求,并且您可能需要UpdateProfileRequest.php