0
votes

When I showed it in a browser when upgrading to symfony3, I had a title error (line 25).
I had questions on similar mistakes in StackOverflow, but I think it's a different trend.
I want to know how it can be improved.

Stack Trace

[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

Code
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);
        }
    }

Version
Cent OS 6.7
PHP 5.6
Symfony3.0.9

1
Well, the error is obvious, it's not an array, it's an object. Maybe the earlier versions returned an array. Dump $_controller and see how you can get what you need in the new version. With all that being said, that's one seriously outdated system... I hope you plan to gradually upgrade to the latest versions of both PHP and Symfony. - El_Vanja
This $_controller = $event->getRequest(); seems dubious... You probably want $_controller = $event->getController(); - Yoshi
I see you are still trying to get this to work. Get yourself an IDE that shows when you have syntax errors. Will make your life much easier. And then as mentioned on your previous question, start with the example in the docs. - Cerad
What have you tried to debug the problem? - Nico Haase
@El_Vanja Does that mean that the type returned in the previous version is different? I will dump it and check it. This error is an error that occurred during the symfony update. - youplus

1 Answers

0
votes

The error was resolved when I fixed it as follows.

    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);
                }
        }
        }
    }