【问题标题】:Call to undefined method Symfony\Component\HttpFoundation\Response::withCookie()调用未定义的方法 Symfony\Component\HttpFoundation\Response::withCookie()
【发布时间】:2020-05-15 15:17:36
【问题描述】:

Laravel:6.18.1 PHP:7.4

laravel 中的中间件产生了这个错误

{
    "message": "Call to undefined method Symfony\\Component\\HttpFoundation\\Response::withCookie()",
    "exception": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",

给我错误的代码

if (!$request->hasCookie('ppl') || ($request->hasCookie('ppl') && $ppl_cookie->ppl_id != $ppl->ppl_id)) {
            if (Auth::check()) {
                Event::dispatch('ppl.updated', [Auth::user(), $ppl]);
            }
            return $next($request)->withCookie(cookie()->forever('ppl', $ppl));
        }

我不明白问题。 cookie 没有存储在浏览器中

编辑

中间件类

<?php 
namespace App\Http\Middleware;

use Closure;
use App\System\Models\People;
use App;
use Event;
use Auth;
use Illuminate\Support\Facades\URL;
use Session;

class VerifyPeople
{

    protected $app;

    public function __construct()
    {
        $this->app = app();
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */

    public function handle($request, Closure $next)
    {
       $People_cookie = json_decode($request->cookie('People'));
        if (!empty($request->route()) && in_array($request->route()->uri(), $this->excepts)) {
            return $next($request);
        }

        if ($request->getHost()) {
            $domain_url = cleanUrl($request->getHost());
            $People = People::where('People_url', '=', $domain_url)->remember(LONG_TERM_CACHE_TIMEOUT)->cacheTags(TAG_LONGTERM_DATA)->first();
        } elseif ($request->hasCookie('People')) {
            $People = $People_cookie;
        }

        if (empty($People)) {
            $People = People::where("People_id", People::DEFAULT_People)->remember(LONG_TERM_CACHE_TIMEOUT)->cacheTags(TAG_LONGTERM_DATA)->first();
        }
        $this->app->singleton('People', function () use ($People) {
            return $People;
        });
        if (!$request->hasCookie('People') || ($request->hasCookie('People') && $People_cookie->People_id != $People->People_id)) {
            if (Auth::check()) {
                Event::dispatch('People.updated', [Auth::user(), $People]);
            }
            return $next($request)->withCookie(cookie()->forever('People', $People));
        }
        return $next($request);
    }
}

【问题讨论】:

  • 可以分享完整的中间件类吗?
  • @Ersoy 更新了!!
  • 当你 dd($next($request)); 它应该打印一个 Illuminate\Http\Response 的实例,它可以访问 ResponseTraitwithCookie 方法。在您的错误中,它说它是由 Illuminate\Http\Response 扩展的基类实例 (Symfony\Component\HttpFoundation\Response),它不使用 ResponseTrait
  • @Ersoy:没有得到你
  • @Ersoy:浏览器中没有设置 Cookie

标签: php laravel


【解决方案1】:

在中间件中,当您执行dd($next($request)); 时,它应该打印一个Illuminate\Http\Response 的实例,该实例可以访问特征中的ResponseTraitwithCookie 方法..

在您的情况下,Symfony\Component\HttpFoundation\Response 的实例给出了由Illuminate\Http\Response 扩展但不使用ResponseTrait 的错误。这就是没有找到withCookie 方法的原因。

可能有多种原因,例如,在执行此中间件之前 - 另一个中间件正在使用 Symfony 响应修改默认响应。

这里是withCookie 方法。

public function withCookie($cookie)
{
    if (is_string($cookie) && function_exists('cookie')) {
        $cookie = call_user_func_array('cookie', func_get_args());
    }

    $this->headers->setCookie($cookie);

    return $this;
}

它的作用是调用 headers 的 setCookie 方法。你可以做的是;

替换

return $next($request)->withCookie(cookie()->forever('People', $People));

$response = $next($request);
$response->headers->setCookie(cookie()->forever('People', $People));

return $response;

边看边看;我找到了similar case to yours

【讨论】:

  • @BhumiShah 更新了答案 - 您可以前往 replace 部分寻求解决方案
猜你喜欢
  • 2017-09-21
  • 2018-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-23
  • 2017-06-06
  • 1970-01-01
相关资源
最近更新 更多