Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Url.php
Go to the documentation of this file.
1 <?php
7 
10 
18 {
24  protected $urlFactory;
25 
29  protected $filter;
30 
36  protected $storeManager;
37 
41  protected $sidResolver;
42 
46  protected $urlFinder;
47 
56  public function __construct(
57  \Magento\Framework\UrlFactory $urlFactory,
59  \Magento\Framework\Filter\FilterManager $filter,
60  \Magento\Framework\Session\SidResolverInterface $sidResolver,
62  array $data = []
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  }
71 
77  private function getUrlInstance()
78  {
79  return $this->urlFactory->create();
80  }
81 
89  public function getUrlInStore(\Magento\Catalog\Model\Product $product, $params = [])
90  {
91  $params['_scope_to_url'] = true;
92  return $this->getUrl($product, $params);
93  }
94 
102  public function getProductUrl($product, $useSid = null)
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  }
115 
122  public function formatUrlKey($str)
123  {
124  return $this->filter->translitUrl($str);
125  }
126 
136  public function getUrl(\Magento\Catalog\Model\Product $product, $params = [])
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  }
199 }
getUrl(\Magento\Catalog\Model\Product $product, $params=[])
Definition: Url.php:136
getUrlInStore(\Magento\Catalog\Model\Product $product, $params=[])
Definition: Url.php:89
getProductUrl($product, $useSid=null)
Definition: Url.php:102
__construct(\Magento\Framework\UrlFactory $urlFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Filter\FilterManager $filter, \Magento\Framework\Session\SidResolverInterface $sidResolver, UrlFinderInterface $urlFinder, array $data=[])
Definition: Url.php:56
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18