【问题标题】:POST method is not supported for this route. Supported methods: GET, HEAD (LARAVEL)此路由不支持 POST 方法。支持的方法:GET、HEAD (LARAVEL)
【发布时间】:2020-04-19 17:41:11
【问题描述】:

我将标准 php 代码从一个名为 test5.php 的文件中提取到一个名为 subscription.blade.php 的文件中。

代码在标准的 php 文件中运行良好并且几乎可以运行 在 Laravel 刀片视图中。提示和关联样式正确显示。但是,一旦将两个数组变量之一输入密码框,它只会给我一个 500 错误。当在标准文件中的刀片外部测试相同的代码时,它可以正常工作。我考虑了 .htaccess 问题,因为一个直接受到影响,而不是另一个,并且在故障排除尝试中空无一物。我当然是 Laravel 的新手。我尝试使用 @php 和 @endphp 指令直接在刀片中使用代码,但没有成功,所以我使用了包含。它似乎不喜欢最后一行感谢您对此的任何见解。

我使用 @include('test5') 将以下代码从 test5.php 拉入到 subscription.blade.php 中

 <?php
include('easy-protect.php');
$options = array(
        'skin'     => 6,   
    );
   protect(array('testing','123456'), $options);
?>

错误日志:

[2020-01-01 21:39:08] local.ERROR: Call to a member function send() on null {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function send() on null at /home/www/mywebsite.com/index.php:56)
[stacktrace]
#0 {main}
"}    

[2020-01-01 21:39:08] local.ERROR: Uncaught Error: Call to a member function send() on null in /home/www/mywebsite.com/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:123
    Stack trace:
    #0 /home/www/mywebsite.com/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(100): Illuminate\Foundation\Bootstrap\HandleExceptions->renderHttpResponse(Object(Symfony\Component\Debug\Exception\FatalThrowableError))
    #1 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleException(Object(Symfony\Component\Debug\Exception\FatalThrowableError))
    #2 {main}
      thrown {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalErrorException(code: 1): Uncaught Error: Call to a member function send() on null in /home/www/mywebsite.com/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:123
    Stack trace:
    #0 /home/www/mywebsite.com/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(100): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->renderHttpResponse(Object(Symfony\\Component\\Debug\\Exception\\FatalThrowableError))
    #1 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleException(Object(Symfony\\Component\\Debug\\Exception\\FatalThrowableError))
    #2 {main}
      thrown at /home/www/mywebsite.com/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:123)
    [stacktrace]
    #0 {main}
    "} 

index.php:56

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send(); (This is LINE 56)

$kernel->terminate($request, $response);

HandleExceptions.php:100

/**
 * Handle an uncaught exception from the application.
 *
 * Note: Most exceptions can be handled via the try / catch block in
 * the HTTP and Console kernels. But, fatal error exceptions must
 * be handled differently since they are not normal exceptions.
 *
 * @param  \Throwable  $e
 * @return void
 */
public function handleException($e)
{
    if (! $e instanceof Exception) {
        $e = new FatalThrowableError($e);
    }

    try {
        self::$reservedMemory = null;

        $this->getExceptionHandler()->report($e);
    } catch (Exception $e) {
        //
    }

    if ($this->app->runningInConsole()) {
        $this->renderForConsole($e);
    } else {
        $this->renderHttpResponse($e); (This is LINE 100)
    }
}

HandleExceptions.php:123

/**
 * Render an exception as an HTTP response and send it.
 *
 * @param  \Exception  $e
 * @return void
 */
protected function renderHttpResponse(Exception $e)
{
    $this->getExceptionHandler()->render($this->app['request'], $e)->send();(This is Line 123)
}

2020 年 1 月 5 日更新:迄今为止的调查结果:
1。 @adilbo 将代码放在刀片文件的最开头并没有做任何更改。
2。事实上,我从 subscription.blade.php 文件中删除了所有代码,只留下了具有完全相同结果的简单保护代码。这让我相信它是 Laravel 中的一个文件名关联,也许我可以在其中排除但在哪里?
3。我在同一目录中我自己的自定义命名 php 刀片视图中尝试了代码,它工作正常。在特定文件名中使用代码不起作用。我希望这是有道理的。

原因错误:Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException 此路由不支持 POST 方法。支持的方法:GET、HEAD。

【问题讨论】:

  • 仅供参考,通常有更多的数组选项,但为了简单起见,我在此处将其删减。
  • 你已经尝试过这种方式......我尝试使用&lt;?php and ?&gt;指令直接在刀片中使用代码。
  • 如果您使用的是 laravel 框架,请检查 storage/logs 文件夹中的日志,会有日志请检查该文件中的错误详细信息
  • 我已经尝试了
  • 我已经按照建议检查了日志。为了这个评论,我将压缩错误输出。 1st - /vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:123 。此行专门指此代码 /** * Render an exception as an HTTP response and send it. * * @param \Exception $e * @return void */ protected function renderHttpResponse(Exception $e) { $this-&gt;getExceptionHandler()-&gt;render($this-&gt;app['request'], $e)-&gt;send(); } @SagarSainkar

标签: php laravel


【解决方案1】:
  1. 我得到了一个随机的 500 错误,这是最糟糕的。为了更好地理解错误,我将以下内容添加到 /app/Exceptions/Handler.php 公共函数渲染($request, Exception $exception)
public function render($request, Exception $exception)  
    {  
          return parent::render($request, $exception);  
    }

这产生了一个错误,详细说明不允许使用 POST 方法。

  1. 在这种情况下不允许使用 POST 方法,因为在 /routes/web.php 中没有为其定义路由。就我而言,我有 GET 路由但没有定义的 POST 路由。据我了解,这是 Laravel 框架中的众多安全功能之一。使用 GET 或 POST 定义新路由类似于以下示例之一:
 /* mypage */

 Route::get('/mypage', 'CommonController@view_mypage');
 Route::post('/mypage', 'CommonController@view_mypage'); 
 /*mypage*/
  1. 成功添加路由后,500 错误现在将消失。但是,您现在有一个 419 PAGE EXPIRED 错误(我很幸运)。当 Laravel 无法在页面 HTML 正文中找到 CSRF_TOKEN 表单输入时,会生成此错误作为 Laravel 的另一项安全措施。它正在寻找这个,因为我已经通过在我的案例中使用包含的 easy-protect.php 文件引入了一个表单。你有两种选择可以让自己摆脱这个错误(我知道)。

选项 1(最不安全)- 通过将以下代码示例添加到 /app/Http/Middleware/VerifyCsrfToken.php

    protected $except = [     'https://mywebsite.com/mypage',
     ];

选项 2(最安全)- 让您新引入的表单/页面符合 CSRF 保护,无论是内联还是包含文件的一部分,具体取决于您的独特情况。你可以从 Laravel HERE 了解更多信息

重要提示:完成此操作并希望您取得了一些成功,不要忘记返回 /app/Exceptions/Handler.php并注释掉下面的代码行。否则,站点生成的任何错误都可以轻松地在报告中公开您的所有数据库凭据和 API 密钥。

//return parent::render($request, $exception);

这个答案对我的场景来说是独一无二的,但如果您尝试将其他 PHP 产品(例如表单)集成到 Laravel 框架中,这将是非常常见的。再次请注意,我不是 Laravel 专家,也不是知识渊博。我必须通过经历这个问题来学习所有这些。我分享这个详细的答案,希望能让其他迷失在正确道路上的人。

【讨论】:

    猜你喜欢
    • 2020-06-05
    • 2021-05-08
    • 2020-01-22
    • 2020-04-25
    • 1970-01-01
    • 2019-08-28
    • 2019-08-31
    • 2021-05-05
    相关资源
    最近更新 更多