Laravel:禁止用户直接访问Post方法

时间:2018-08-25 10:16:04

标签: laravel laravel-5 laravel-routing laravel-exceptions

我有一个用Laravel开发的网站。

问题:

我有一个路由方法(POST)

Route::post('/profile/edit/save', 'ProfileController@save');

如果我输入此网址“ mywebsite.com/profile/edit/save”,则会收到错误消息      enter image description here     Symfony \组件\ HttpKernel \异常\ MethodNotAllowedHttpException

我的.env文件:

 APP_NAME=Laravel
 APP_ENV=production
 APP_DEBUG=false
 APP_LOG_LEVEL=debug
 APP_URL=http://localhost

我在服务器上运行此代码时遇到此错误,在Localhost中我没有任何错误。

我已清除缓存,但无法正常工作

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您无法像上述尝试那样访问发布网址 您只能通过提交这样的表格来访问...

<form action="route('post_insert')" method="post">
  {{csrf_field()}}
   <input type="text" />
<input type="submit"/>
</form>

您的路线必须包含此名称...

Route::post('/profile/save', 'ProfileController@save')->name('post_insert');

答案 1 :(得分:0)

我已通过在app \ Exceptions \ handler.php中添加它来解决了该问题

    public function render($request, Exception $exception)
        {   

             if($exception instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException){
                 return redirect('/');
             }

        return parent::render($request, $exception);
    }
相关问题