1

I have come a cross this class vendor/magento/framework/Url.php.

these are the snippet code :

public function __construct(
        \Magento\Framework\App\Route\ConfigInterface $routeConfig,
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Framework\Url\SecurityInfoInterface $urlSecurityInfo,
        \Magento\Framework\Url\ScopeResolverInterface $scopeResolver,
        \Magento\Framework\Session\Generic $session,
        \Magento\Framework\Session\SidResolverInterface $sidResolver,
        \Magento\Framework\Url\RouteParamsResolverFactory $routeParamsResolverFactory,
        \Magento\Framework\Url\QueryParamsResolverInterface $queryParamsResolver,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Framework\Url\RouteParamsPreprocessorInterface $routeParamsPreprocessor,
        $scopeType,
        array $data = []
    ) {
        $this->_request = $request;
        $this->_routeConfig = $routeConfig;
        $this->_urlSecurityInfo = $urlSecurityInfo;
        $this->_scopeResolver = $scopeResolver;
        $this->_session = $session;
        $this->_sidResolver = $sidResolver;
        $this->_routeParamsResolverFactory = $routeParamsResolverFactory;
        $this->_queryParamsResolver = $queryParamsResolver;
        $this->_scopeConfig = $scopeConfig;
        $this->routeParamsPreprocessor = $routeParamsPreprocessor;
        $this->_scopeType = $scopeType;
        parent::__construct($data);
    }

/**
     * Build url by requested path and parameters
     *
     * @param   string|null $routePath
     * @param   array|null $routeParams
     * @return  string
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    private function createUrl($routePath = null, array $routeParams = null)
    {
        $escapeQuery = false;

        /**
         * All system params should be unset before we call getRouteUrl
         * this method has condition for adding default controller and action names
         * in case when we have params
         */
        $this->getRouteParamsResolver()->unsetData('secure');
        $fragment = null;
        if (isset($routeParams['_fragment'])) {
            $fragment = $routeParams['_fragment'];
            unset($routeParams['_fragment']);
        }

        if (isset($routeParams['_escape'])) {
            $escapeQuery = $routeParams['_escape'];
            unset($routeParams['_escape']);
        }

        $query = null;
        if (isset($routeParams['_query'])) {
            $this->_queryParamsResolver->setQueryParams([]);
            $query = $routeParams['_query'];
            unset($routeParams['_query']);
        }

        $noSid = null;
        if (isset($routeParams['_nosid'])) {
            $noSid = (bool)$routeParams['_nosid'];
            unset($routeParams['_nosid']);
        }

        $url = $this->getRouteUrl($routePath, $routeParams);
        /**
         * Apply query params, need call after getRouteUrl for rewrite _current values
         */
        if ($query !== null) {
            if (is_string($query)) {
                $this->_setQuery($query);
            } elseif (is_array($query)) {
                $this->addQueryParams($query, !empty($routeParams['_current']));
            }
            if ($query === false) {
                $this->addQueryParams([]);
            }
        }

        if ($noSid !== true) {
            $this->_prepareSessionUrl($url);
        }

        $query = $this->_getQuery($escapeQuery);
        if ($query) {
            $mark = strpos($url, '?') === false ? '?' : ($escapeQuery ? '&' : '&');
            $url .= $mark . $query;
            $this->_queryParamsResolver->unsetData('query');
            $this->_queryParamsResolver->unsetData('query_params');
        }

        if (!is_null($fragment)) {
            $url .= '#' . $fragment;
        }
        $this->getRouteParamsResolver()->unsetData('secure');

        return $this->escape($url);
    }

I would like to add a prefix to to all product and category url. I'm pretty sure that this is the right class to do it, please correct me if I'm wrong here.

My question here is :

  • How to check if the $routePath and $routParam is coming from the product or category ? is it possible to do that in createUrl() function ?

  • Is it possible to get it using $this->_request ? but I don't know how.

Please help and give me guidance. Any help would be really appreciated.

1 Answer 1

0

I found my own mistake here. I don't think the vendor/magento/framework/Url.php is the right place to modify and add the prefix here.

When change the behavior always loook for the module first instead of the core framework. Here is the class that give the url for every product : vendor/module-catalog/Model/Product.php

here is the code snippet I added the ip/ for additional code :

   /**
     * Returns request path
     *
     * @return string
     */
    public function getRequestPath()
    {
       return 'ip/'.$this->_getData('request_path');

    }

We can use plugin or type for changing this behavior of this class. hope it helps to everyone else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.