【问题标题】:Can't see why error is MethodNotAllowedHttpException看不到为什么错误是 MethodNotAllowedHttpException
【发布时间】:2017-06-20 17:29:30
【问题描述】:

我正在尝试在 Laravel 4.2 + Sentry 中制作登录表单。问题是,当我提交表单时,我收到了不允许该方法的错误。

当我在源代码中检查我的表单时,它有method="POST",并且在我写的路径中post。可能是什么问题?

MethodNotAllowedHttpException

但不明白为什么?这是表格

        {{ Form::open(array('route' => 'check-auth')) }}
            <div class="body bg-gray">
                {{-- Display flash message --}}
                @if (Session::has('flash_message'))
                    <span style="margin-left:18%; color: #ff0000">{{ Session::get('flash_message') }}</span>
                @endif

                <div class="form-group">
                    <input type="text" name="email" class="form-control" placeholder="User email"/>
                    @if($errors->has('login_errors')) <span class="has-error">{{ $errors->first('email') }}</span> @endif
                </div>
                <div class="form-group">
                    <input type="password" name="password" class="form-control" placeholder="User password"/>
                </div>
            <button type="submit" name="submitbtn" class="btn bg-olive btn-block">Sign me in</button>  
            </div>
        {{ Form::close() }}

路线

Route::post('user-login', ['as'=>'check-auth', 'uses'=>'AuthenticationController@login']);

和控制器

public function login()
{
    try{
        $credentials = array(
            'email'     => Input::get('email'),
            'password'  => Input::get('password')

        );

        $user = Sentry::authenticate($credentials, false);
        if($user){
            return Redirect::to('dashboard');
        }

        return Redirect::to('/')->with('title','Login errors');
    }
    catch(Exception $e){
        echo $e->getMessage();
        Session::flash('flash_message', 'No access!');
        return Redirect::to('/')->with('title','Login errors');
    }
}

更新:错误

production.ERROR: Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException in /var/www/html/time/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:210

【问题讨论】:

  • 向我们展示异常可能会有所帮助。
  • @Chris 我已经更新了这个问题。这是我看到的唯一..
  • 鉴于您提供的代码,我没有发现任何问题。您必须发布更多代码,正如@Chris 所说,显示整个异常。
  • 您可能有,但您可以检查一下基础知识。正确的机器(我注意到错误说生产),任何嵌套到带有前缀的路由,尝试将路由重新定义为 GET 和 die(),并在控制器中路由操作的顶部使用一些消息。

标签: php laravel laravel-4


【解决方案1】:

您的路线是正确的,我唯一可以建议的是将类型附加到表单的开头:
{{ Form::open(['url' =&gt; 'check-auth', 'method' =&gt; 'post']) }}

【讨论】:

  • 谢谢,这行得通,但我不明白为什么这行得通,但其他的却不行……因为它们是一样的?
  • 我相信它在表单中添加了另一个隐藏字段,至少它在 L5 中使用包是这样做的,自从我使用 Laravel 4 以来已经有一段时间了。@VLS
【解决方案2】:

{{ Form::open(array('route' => 'check-auth')) }}

看,您正在使用路由检查身份验证,并且在路由文件中定义了不同的路由,即 user-login

Route::post('user-login', ['as'=>'check-auth', 'uses'=>'AuthenticationController@login']);

正确的路线再试一次就行了

【讨论】:

  • check-auth 是路线的名称,应该与现有的表格一起使用。
【解决方案3】:

使用 get 代替 post (它仍然可以发布内容,但您也可以检索)

【讨论】:

    猜你喜欢
    • 2020-04-22
    • 2021-03-16
    • 2019-09-03
    • 1970-01-01
    • 2020-12-30
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    相关资源
    最近更新 更多