Laravel 5.2.36 RouteCollection.php 第 218 行中的 MethodNotAllowedHttpException:

Laravel 5.2.36 MethodNotAllowedHttpException in RouteCollection.php line 218:

您好,我是 Laravel 的新手,我正在尝试实现 post 对简单表单的请求。我一直在关注 YouTube 教程系列 (laravel 5 | Section 3 | Part 4 Routing POST Requests),但是在 5:46mins 中,有一条通知指出此方法仅适用于 Laravel 5.2.

之前的版本

我试图编辑 VerifyCsrfToken.php 方法 protected $except = ['api/'];但这没有区别。

我的 routes.php 代码片段:

Route::post('/form-handler', function(\Illuminate\Http\Request $request){
    if(isset($request['option']) && $request['firstName'])
    {
        if(strlen($request['firstName']) > 0){
            return view('forms.formResults', ['action' => $request['option'], 'name' => $request['firstName']]);
        }
        return redirect()->back();
    }
    return redirect()->back(); // Return user back to the page they came from
})->name('form-handler');

我的 welcome.blade.php 代码片段:

<div class="form-group">
    <form action="{{route('form-handler')}}" method="post" class="form-control">
        <label for="select-action">Select an option:</label>
        <select id="select-action" name="option">
            <option value="option1">Option1</option>
            <option value="option2">Option2</option>
            <option value="option3">Option3</option>
        </select>
        <input type="text" name="firstName">
        <button type="submit">Submit Details</button>
        <input type="hidden" value="{{Session::token()}}" name="_token"> <!-- Laravel Security Measure, Note name must always be '_token'-->
    </form>
</div>

我的完整错误信息:

MethodNotAllowedHttpException in RouteCollection.php line 218:
in RouteCollection.php line 218
at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 205
at RouteCollection->getRouteForMethods(object(Request), array('POST')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 821
at Router->findRoute(object(Request)) in Router.php line 691
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Debugbar.php line 49
at Debugbar->handle(object(Request), object(Closure))
at call_user_func_array(array(object(Debugbar), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 53

这种实现方式是完全无效还是只需要几个星期?

最好我想知道检索表单数据的最佳 post 方法是什么,同时仍然保护您的站点免受跨站点请求伪造。

任何修复或替代解决方案将不胜感激。

请随时询问我可能遗漏的任何资源。

Full routes.php(注意我也尝试将 post 方法添加到中间件路由组):

    <?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

/* Route::get('/', function () {
    return view('home.welcome');
}); */

Route::get('/', 'WelcomeController@index')->name('home');

Route::resource('lists', 'ListsController');

Route::get('/contact/{firstName?}', function($firstName = null)
{
    return view('about.contact', ['firstName' => $firstName]);
})->name('contact');

Route::get('/blog', function()
{
    return view('blog.blog');
})->name('blog');

Route::group(['middleware' => 'auth'], function () {
    Route::post('/form-handler', function(\Illuminate\Http\Request $request){
        if(isset($request['option']) && $request['firstName'])
        {
            if(strlen($request['firstName']) > 0){
            return view('forms.formResults', ['action' => $request['option'], 'name' => $request['firstName']]); // You create the view
        }
        return redirect()->back();
    }
    return redirect()->back(); // Return user back to the page they came from
    })->name('form-handler');
});

已编辑答案 --

我已经编辑了我的答案,使代码更清晰,完全符合您的需要。它不使用 isset,而是使用验证,如果未设置选项,则不会停止 运行 代码。这也会在 blade 视图中显示一条错误消息。

您的 routes.php 文件应包含以下内容

Route::post('/form-handler', 'formController@formHandle');

Blade 视图必须具有如下形式

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
<div class="form-group">
    <form action="{{route('form-handler')}}" method="post" class="form-control">
        <label for="select-action">Select an option:</label>
        <select id="select-action" name="option">
            <option value="option1">Option1</option>
            <option value="option2">Option2</option>
            <option value="option3">Option3</option>
        </select>
        <input type="text" name="firstName">
        <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
        <button type="submit">Submit Details</button>
        <input type="hidden" value="{{Session::token()}}" name="_token">
    </form>

表单控制器必须如下所示

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class FormController extends Controller
{
    /**
     * Logic Goes here.
     *
     * @param  Request  $request
     * @return Response
     */
    public function formHandle(Request $request)
    {   
        $this->validate($request, [
            'firstName' => 'required|min:1|max:255',
            'option' => 'required'
            ]);

            $data = $request;

            return view('forms.formResults')->compact($data);  
    }
}