Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
Url Class Reference
Inheritance diagram for Url:
DataObject

Public Member Functions

 __construct (\Magento\Framework\UrlFactory $urlFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Filter\FilterManager $filter, \Magento\Framework\Session\SidResolverInterface $sidResolver, UrlFinderInterface $urlFinder, array $data=[])
 
 getUrlInStore (\Magento\Catalog\Model\Product $product, $params=[])
 
 getProductUrl ($product, $useSid=null)
 
 formatUrlKey ($str)
 
 getUrl (\Magento\Catalog\Model\Product $product, $params=[])
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 

Protected Attributes

 $urlFactory
 
 $filter
 
 $storeManager
 
 $sidResolver
 
 $urlFinder
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 
- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Product Url model

@api

Since
100.0.2

Definition at line 17 of file Url.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\UrlFactory  $urlFactory,
\Magento\Store\Model\StoreManagerInterface  $storeManager,
\Magento\Framework\Filter\FilterManager  $filter,
\Magento\Framework\Session\SidResolverInterface  $sidResolver,
UrlFinderInterface  $urlFinder,
array  $data = [] 
)
Parameters
\Magento\Framework\UrlFactory$urlFactory
\Magento\Store\Model\StoreManagerInterface$storeManager
\Magento\Framework\Filter\FilterManager$filter
\Magento\Framework\Session\SidResolverInterface$sidResolver
UrlFinderInterface$urlFinder
array$data

Definition at line 56 of file Url.php.

63  {
64  parent::__construct($data);
65  $this->urlFactory = $urlFactory;
66  $this->storeManager = $storeManager;
67  $this->filter = $filter;
68  $this->sidResolver = $sidResolver;
69  $this->urlFinder = $urlFinder;
70  }

Member Function Documentation

◆ formatUrlKey()

formatUrlKey (   $str)

Format Key for URL

Parameters
string$str
Returns
string

Definition at line 122 of file Url.php.

123  {
124  return $this->filter->translitUrl($str);
125  }

◆ getProductUrl()

getProductUrl (   $product,
  $useSid = null 
)

Retrieve Product URL

Parameters
\Magento\Catalog\Model\Product$product
bool$useSidforced SID mode
Returns
string

Definition at line 102 of file Url.php.

103  {
104  if ($useSid === null) {
105  $useSid = $this->sidResolver->getUseSessionInUrl();
106  }
107 
108  $params = [];
109  if (!$useSid) {
110  $params['_nosid'] = true;
111  }
112 
113  return $this->getUrl($product, $params);
114  }
getUrl(\Magento\Catalog\Model\Product $product, $params=[])
Definition: Url.php:136
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ getUrl()

getUrl ( \Magento\Catalog\Model\Product  $product,
  $params = [] 
)

Retrieve Product URL using UrlDataObject

Parameters
\Magento\Catalog\Model\Product$product
array$params
Returns
string @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Definition at line 136 of file Url.php.

137  {
138  $routePath = '';
139  $routeParams = $params;
140 
141  $storeId = $product->getStoreId();
142 
143  $categoryId = null;
144 
145  if (!isset($params['_ignore_category']) && $product->getCategoryId() && !$product->getDoNotUseCategoryId()) {
146  $categoryId = $product->getCategoryId();
147  }
148 
149  if ($product->hasUrlDataObject()) {
150  $requestPath = $product->getUrlDataObject()->getUrlRewrite();
151  $routeParams['_scope'] = $product->getUrlDataObject()->getStoreId();
152  } else {
153  $requestPath = $product->getRequestPath();
154  if (empty($requestPath) && $requestPath !== false) {
155  $filterData = [
156  UrlRewrite::ENTITY_ID => $product->getId(),
159  ];
160  if ($categoryId) {
161  $filterData[UrlRewrite::METADATA]['category_id'] = $categoryId;
162  }
163  $rewrite = $this->urlFinder->findOneByData($filterData);
164  if ($rewrite) {
165  $requestPath = $rewrite->getRequestPath();
166  $product->setRequestPath($requestPath);
167  } else {
168  $product->setRequestPath(false);
169  }
170  }
171  }
172 
173  if (isset($routeParams['_scope'])) {
174  $storeId = $this->storeManager->getStore($routeParams['_scope'])->getId();
175  }
176 
177  if ($storeId != $this->storeManager->getStore()->getId()) {
178  $routeParams['_scope_to_url'] = true;
179  }
180 
181  if (!empty($requestPath)) {
182  $routeParams['_direct'] = $requestPath;
183  } else {
184  $routePath = 'catalog/product/view';
185  $routeParams['id'] = $product->getId();
186  $routeParams['s'] = $product->getUrlKey();
187  if ($categoryId) {
188  $routeParams['category'] = $categoryId;
189  }
190  }
191 
192  // reset cached URL instance GET query params
193  if (!isset($routeParams['_query'])) {
194  $routeParams['_query'] = [];
195  }
196 
197  return $this->getUrlInstance()->setScope($storeId)->getUrl($routePath, $routeParams);
198  }
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ getUrlInStore()

getUrlInStore ( \Magento\Catalog\Model\Product  $product,
  $params = [] 
)

Retrieve URL in current store

Parameters
\Magento\Catalog\Model\Product$product
array$paramsthe URL route params
Returns
string

Definition at line 89 of file Url.php.

90  {
91  $params['_scope_to_url'] = true;
92  return $this->getUrl($product, $params);
93  }
getUrl(\Magento\Catalog\Model\Product $product, $params=[])
Definition: Url.php:136
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

Field Documentation

◆ $filter

$filter
protected

Definition at line 29 of file Url.php.

◆ $sidResolver

$sidResolver
protected

Definition at line 41 of file Url.php.

◆ $storeManager

$storeManager
protected

Definition at line 36 of file Url.php.

◆ $urlFactory

$urlFactory
protected

Definition at line 24 of file Url.php.

◆ $urlFinder

$urlFinder
protected

Definition at line 46 of file Url.php.


The documentation for this class was generated from the following file: