【问题标题】:Error: Cannot use object of type Symfony\Component\HttpFoundation\Request as array错误:不能使用 Symfony\Component\HttpFoundation\Request 类型的对象作为数组
【发布时间】:2020-12-14 10:52:14
【问题描述】:

当我在升级到 symfony3 时在浏览器中显示它时,出现标题错误(第 25 行)。
我对 StackOverflow 中的类似错误有疑问,但我认为这是一个不同的趋势。
我想知道如何改进它。

堆栈跟踪

[1] Symfony\Component\Debug\Exception\FatalErrorException: Error: 
Cannot use object of type Symfony\Component\HttpFoundation\Request as array
    at n/a
        in /Symfony/.../Bundle/Listener/PortalListener.php line 25

代码
PortalListener.php

<?php
namespace Ahi\Sp\PublicBundle\Listener;

use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Ahi\Sp\PublicBundle\Controller\BaseController;
use Ahi\Sp\PublicBundle\Model\Service\AhiCookieService;
use Symfony\Component\HttpFoundation\Request;

/**
 */
class PortalListener
{
    /*
     * 
     * @param FilterControllerEvent $event
     */
    public function preControllerExecute(FilterControllerEvent $event, Request $request)
    {
        if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
            $_controller = $event->getRequest();
            if (isset($_controller[0])) {   //line25
                $controller = $_controller[0];
                if (method_exists($controller, 'preExecute')) {
                    $controller->preExecute($request);
                }
            }
        }
    }

BaseController.php

<?php
namespace Ahi\Sp\PublicBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;

/ **
 * Front controller.
 * /
abstract class BaseController extends Controller
{
    const MAX_ITEM_COUNT = 4; // Maximum number to get the item part number from the coordination. (You can get up to 4)

    protected $parameters = array();
    protected $response = null;
    protected $cookie = null;

    public function preExecute (Request $request) {

        $this->cookie = $this->get('public.ahiCookieService');
        // ---------------------------------------
        // Gender (The order is the member's gender → portal top gender button → brand → shop → MEN'S $LADIE'S button)
        $this->gender = $this->cookie->get('gender');

        // Get the brand and overwrite the gender of the brand
        $this->brand = null;
        $brandDir = null;
        $brandDirUrlParam = $request->attributes->get('brandDir');
        $brandDirGetParam = $request->query->get('brandDir');
        

        if ($brandDirUrlParam) {
            $brandDir = $brandDirUrlParam;
        } elseif ($brandDirGetParam) {
            $brandDir = $brandDirGetParam;
        }

        $brandService = $this->get('public.brandService');

        if ($brandDir) {
            $brand = $brandService->getBrand($brandDir);

            if (!$brand) {
                throw $this->createBrandNotFoundException();
            }

            if(!$this->isPreview() && !$brand->getDispFlg()) {
                throw $this->createBrandNotFoundException();
            }

            $brandSex = $brand->getBrandSex();
            if ($brandSex != 2) {
                $this->gender = $brandSex;
            }
            $this->brand = $brand;
        }

        // shop --------------------------------->
        $this->shop = null;
        $shopDir = null;
        $shopDirUrlParam = $request->attributes->get('shopDir');
        $shopDirGetParam = $request->query->get('shopDir');

        if ($shopDirUrlParam) {
            $shopDir = $shopDirUrlParam;
        } elseif ($shopDirGetParam) {
            $shopDir = $shopDirGetParam;
        }

        $this->shopDir = $shopDir;
        $shopService = $this->get('public.shopService');
        if ($shopDir) {
            $shop = $shopService->getShop($shopDir);

            if (!$shop) {
                throw $this->createShopNotFoundException();
            }

            if (!$this->isPreview() && !$shop->getDispFlg()) {
                if ($shop->getRedirectFlg() == true) {
                    $redirectUrl = $shop->getRedirectUrl();
                    if (empty($redirectUrl)) {
                        $redirectUrl = $this->generateUrl('ahi_sp_public_brand_top', array('brandDir' => $shop->getBrand()->getDirName()));
                    }
                    throw new ShopRedirectException($redirectUrl);
                } else {
                    throw $this->createShopNotFoundException();
                }
            }

            $shopSex = $shop->getShopSex();
            if ($shopSex != 2) {
                $this->gender = $shopSex;
            }

            $this->shop = $shop;
        }
        // shop <---------------------------------
        $gender = $request->query->get('gender');
        if ($gender !== null) {
            $this->gender = $gender;
        }
        if ($this->getRequest()->get('_route') !== 'ahi_sp_public_portal_top') {
            $gender = $request->query->get('gender');
            if ($gender !== null) {
                $this->gender = $gender;
            }
        }

        if ($this->gender !== null) { 
            $this->gender = intval($this->gender);
            $this->cookie->set('gender', $this->gender, 30);
            $this->cookie->set('ec_gender', $this->gender, 30);
        }

        if ($this->gender === 0 or $this->gender === 1) {
            $this->paramsSex = array('sex'=> array($this->gender, 2));
        } else {
            $this->paramsSex = array('sex'=> array(0, 1, 2));
        }

        $mid = $this->cookie->get('member_id');
        $session = $request->getSession();
        if ($mid && !$session->has('favoriteShops')) {
            $route =  $this->container->getParameter('ats_http');
            $list = $this->container->getParameter('favorite_shop_list');
            $url = $route . $list . $mid;
            $ahiStoreIdList = file_get_contents($url);
            $favoriteShops = array();
            if ($ahiStoreIdList !=='') {
                $ahiStoreIdArray = explode(",", $ahiStoreIdList);
                $shopService = $this->get("public.shopService");
                $sortKey = array();
                foreach ($ahiStoreIdArray as $key => $storeId) {
                    $id = explode(':', $storeId);
                    $shop = $shopService->getShopById($id[1]);
                    if ($shop) {
                        $favoriteShops[$key]['shopName'] = $shop->getShopName();
                        $favoriteShops[$key]['shopDir'] = $shop->getDirName();
                        $sortKey[$key] = $shop->getShopName();
                    }
                }
                array_multisort($sortKey, SORT_ASC, $favoriteShops);
            }
            $session->set('favoriteShops', $favoriteShops);
        }
        $trend_tag_list_limit = $this->container->getParameter("trend_tag_list_limit");
        if(!$brandDirUrlParam and !$shopDirUrlParam){
            $this->parameters['brandPrefectures'] = $this->service('coordinate')->getPrefExistBrand();
            $this->parameters['prefBrands'] = $this->service('coordinate')->getBrandPerPref();
            $this->parameters['shopBrandPrefectures'] = $this->service('brand')->getPrefExistBrand();
            $this->parameters['shopPrefBrands'] = $this->service('brand')->getBrandPerPref();
            $this->parameters['trendTags'] = $this->service('ecTrendTag','common')->getTrendTag($this->paramsSex,false,$trend_tag_list_limit);
        }elseif($brandDirUrlParam){
            $this->parameters['brandPrefectures'] = $this->service('coordinate')->getPrefExistBrand($brandDirUrlParam);
            $this->parameters['trendTags'] = $this->service('ecTrendTag','common')->getTrendTag($this->brand,false,$trend_tag_list_limit);
        }else{
            $this->parameters['coordinateCount'] = $this->service('coordinate')->getCountArticle(
                $this->brand,
                $this->shop
            );
            $this->parameters['trendTags'] = $this->service('ecTrendTag','common')->getTrendTag($this->shop->getBrand(),false,$trend_tag_list_limit);
        }
    }

版本
美分操作系统 6.7
PHP 5.6
Symfony3.0.9

【问题讨论】:

  • 嗯,错误很明显,它不是一个数组,它是一个对象。也许早期版本返回了一个数组。转储$_controller,看看如何在新版本中获得所需的内容。话虽如此,那是一个严重过时的系统......我希望你计划逐步升级到 PHP 和 Symfony 的最新版本。
  • 这个$_controller = $event-&gt;getRequest(); 看起来很可疑......你可能想要$_controller = $event-&gt;getController();
  • 我看到你仍在努力让它发挥作用。给自己一个 IDE,当你有语法错误时会显示出来。会让你的生活更轻松。然后正如您在上一个问题中提到的,从example in the docs 开始。
  • 你尝试过什么调试问题?
  • @El_Vanja 是不是意味着之前版本返回的类型不一样了?我会转储它并检查它。这个错误是symfony更新过程中发生的错误。

标签: php symfony


【解决方案1】:

当我按如下方式修复时,错误已解决。

    public function preControllerExecute(FilterControllerEvent $event)
    {
        if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
            $request = $event->getRequest();
            $_controller = $event->getController();

        if (isset($_controller[0])) {
            $controller = $_controller[0];
                if (method_exists($controller, 'preExecute')) {
                    $controller->preExecute($request);
                }
        }
        }
    }

【讨论】:

    猜你喜欢
    • 2016-07-22
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多