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

Public Member Functions

 __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, \Magento\Backend\Helper\Data $backendHelper, \Magento\Backend\Model\Menu\Config $menuConfig, \Magento\Framework\App\CacheInterface $cache, \Magento\Backend\Model\Auth\Session $authSession, \Magento\Framework\Encryption\EncryptorInterface $encryptor, \Magento\Store\Model\StoreFactory $storeFactory, \Magento\Framework\Data\Form\FormKey $formKey, array $data=[], HostChecker $hostChecker=null, Json $serializer=null)
 
 getUrl ($routePath=null, $routeParams=null)
 
 getSecretKey ($routeName=null, $controller=null, $action=null)
 
 useSecretKey ()
 
 turnOnSecretKey ()
 
 turnOffSecretKey ()
 
 renewSecretUrls ()
 
 getStartupPageUrl ()
 
 findFirstAvailableMenu ()
 
 setSession (\Magento\Backend\Model\Auth\Session $session)
 
 getAreaFrontName ()
 
- Public Member Functions inherited from Url
 __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=[], HostChecker $hostChecker=null, Json $serializer=null)
 
 setUseSession ($useSession)
 
 getUseSession ()
 
 getConfigData ($key, $prefix=null)
 
 setRequest (\Magento\Framework\App\RequestInterface $request)
 
 setScope ($params)
 
 getBaseUrl ($params=[])
 
 getRouteUrl ($routePath=null, $routeParams=null)
 
 addSessionParam ()
 
 addQueryParams (array $data)
 
 setQueryParam ($key, $data)
 
 getUrl ($routePath=null, $routeParams=null)
 
 getRebuiltUrl ($url)
 
 escape ($value)
 
 getDirectUrl ($url, $params=[])
 
 sessionUrlVar ($html)
 
 useSessionIdForUrl ($secure=false)
 
 isOwnOriginUrl ()
 
 getRedirectUrl ($url)
 
 getCurrentUrl ()
 
- 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)
 

Data Fields

const XML_PATH_USE_SECURE_KEY = 'admin/security/use_form_key'
 
- Data Fields inherited from UrlInterface
const URL_TYPE_LINK = 'link'
 
const URL_TYPE_DIRECT_LINK = 'direct_link'
 
const URL_TYPE_WEB = 'web'
 
const URL_TYPE_MEDIA = 'media'
 
const URL_TYPE_STATIC = 'static'
 
const URL_TYPE_JS = 'js'
 
const DEFAULT_URL_TYPE = 'link'
 
const DEFAULT_CONTROLLER_NAME = 'index'
 
const DEFAULT_ACTION_NAME = 'index'
 
const REWRITE_REQUEST_PATH_ALIAS = 'rewrite_request_path'
 
const SESSION_NAMESPACE = 'frontend'
 
- Data Fields inherited from UrlInterface
const SECRET_KEY_PARAM_NAME = 'key'
 
const XML_PATH_STARTUP_MENU_ITEM = 'admin/startup/menu_item_id'
 

Protected Member Functions

 _isSecure ()
 
 _setRouteParams (array $data, $unsetOldParams=true)
 
 _getMenu ()
 
 _getSession ()
 
 _getActionPath ()
 
 _getScope ()
 
 _getConfigCacheId ($path)
 
 _getConfig ($path)
 
- Protected Member Functions inherited from Url
 _parseUrl ($url)
 
 _getConfigCacheId ($path)
 
 _getConfig ($path)
 
 _getRequest ()
 
 _getType ()
 
 _isSecure ()
 
 _getScope ()
 
 _setRoutePath ($data)
 
 _getActionPath ()
 
 _getRoutePath ($routeParams=[])
 
 _setRouteName ($data)
 
 _getRouteFrontName ()
 
 _getRouteName ($default=null)
 
 _setControllerName ($data)
 
 _getControllerName ($default=null)
 
 _setActionName ($data)
 
 _getActionName ($default=null)
 
 _setRouteParams (array $data, $unsetOldParams=true)
 
 _getRouteParams ()
 
 _setQuery ($data)
 
 _getQuery ($escape=false)
 
 _getFragment ()
 
 _prepareSessionUrl ($url)
 
 getRouteParamsResolver ()
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $_session
 
 $_menu
 
 $_startupMenuItemId
 
 $_backendHelper
 
 $_menuConfig
 
 $_cache
 
 $_encryptor
 
 $_storeFactory
 
 $formKey
 
 $_scope
 
- Protected Attributes inherited from Url
 $_reservedRouteParams
 
 $_scopeType
 
 $_request
 
 $_useSession
 
 $_urlSecurityInfo
 
 $_session
 
 $_sidResolver
 
 $_routeConfig
 
 $_scopeResolver
 
 $_queryParamsResolver
 
 $_scopeConfig
 
 $routeParamsPreprocessor
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Protected Attributes inherited from Url
static $_configDataCache
 
- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Class \Magento\Backend\Model\UrlInterface

@SuppressWarnings(PHPMD.CouplingBetweenObjects) @api

Since
100.0.2

Definition at line 19 of file Url.php.

Constructor & Destructor Documentation

◆ __construct()

__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,
\Magento\Backend\Helper\Data  $backendHelper,
\Magento\Backend\Model\Menu\Config  $menuConfig,
\Magento\Framework\App\CacheInterface  $cache,
\Magento\Backend\Model\Auth\Session  $authSession,
\Magento\Framework\Encryption\EncryptorInterface  $encryptor,
\Magento\Store\Model\StoreFactory  $storeFactory,
\Magento\Framework\Data\Form\FormKey  $formKey,
array  $data = [],
HostChecker  $hostChecker = null,
Json  $serializer = null 
)

Constructor

Parameters
\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
string$scopeType
\Magento\Backend\Helper\Data$backendHelper
Menu\Config$menuConfig
\Magento\Framework\App\CacheInterface$cache
Auth\Session$authSession
\Magento\Framework\Encryption\EncryptorInterface$encryptor
\Magento\Store\Model\StoreFactory$storeFactory
\Magento\Framework\Data\Form\FormKey$formKey
array$data
HostChecker | null$hostChecker
Json$serializer@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 110 of file Url.php.

132  {
133  $this->_encryptor = $encryptor;
134  $hostChecker = $hostChecker ?: ObjectManager::getInstance()->get(HostChecker::class);
135  parent::__construct(
136  $routeConfig,
137  $request,
138  $urlSecurityInfo,
139  $scopeResolver,
140  $session,
141  $sidResolver,
142  $routeParamsResolverFactory,
143  $queryParamsResolver,
144  $scopeConfig,
146  $scopeType,
147  $data,
148  $hostChecker,
149  $serializer
150  );
151  $this->_backendHelper = $backendHelper;
152  $this->_menuConfig = $menuConfig;
153  $this->_cache = $cache;
154  $this->_session = $authSession;
155  $this->formKey = $formKey;
156  $this->_storeFactory = $storeFactory;
157  }

Member Function Documentation

◆ _getActionPath()

_getActionPath ( )
protected

Retrieve action path. Add backend area front name as a prefix to action path

Returns
string

Definition at line 410 of file Url.php.

411  {
412  $path = parent::_getActionPath();
413  if ($path) {
414  if ($this->getAreaFrontName()) {
415  $path = $this->getAreaFrontName() . '/' . $path;
416  }
417  }
418  return $path;
419  }

◆ _getConfig()

_getConfig (   $path)
protected

Get config data by path Use only global config values for backend

Parameters
string$path
Returns
null|string

Definition at line 457 of file Url.php.

458  {
459  return $this->_scopeConfig->getValue($path);
460  }

◆ _getConfigCacheId()

_getConfigCacheId (   $path)
protected

Get cache id for config path

Parameters
string$path
Returns
string

Definition at line 445 of file Url.php.

446  {
447  return 'admin/' . $path;
448  }

◆ _getMenu()

_getMenu ( )
protected

Get Menu model

Returns
\Magento\Backend\Model\Menu

Definition at line 361 of file Url.php.

362  {
363  if ($this->_menu === null) {
364  $this->_menu = $this->_menuConfig->getMenu();
365  }
366  return $this->_menu;
367  }

◆ _getScope()

_getScope ( )
protected

Get scope for the url instance

Returns
\Magento\Store\Model\Store

Definition at line 426 of file Url.php.

427  {
428  if (!$this->_scope) {
429  $this->_scope = $this->_storeFactory->create(
430  [
431  'url' => $this,
432  'data' => ['code' => 'admin', 'force_disable_rewrites' => false, 'disable_store_in_url' => true],
433  ]
434  );
435  }
436  return $this->_scope;
437  }

◆ _getSession()

_getSession ( )
protected

Retrieve auth session

Returns
\Magento\Backend\Model\Auth\Session

Definition at line 386 of file Url.php.

387  {
388  return $this->_session;
389  }

◆ _isSecure()

_isSecure ( )
protected

Retrieve is secure mode for ULR logic

Returns
bool

Definition at line 164 of file Url.php.

165  {
166  if ($this->hasData('secure_is_forced')) {
167  return $this->getData('secure');
168  }
169  return $this->_scopeConfig->isSetFlag('web/secure/use_in_adminhtml');
170  }
getData($key='', $index=null)
Definition: DataObject.php:119

◆ _setRouteParams()

_setRouteParams ( array  $data,
  $unsetOldParams = true 
)
protected

Force strip secret key param if _nosecret param specified

Parameters
array$data
bool$unsetOldParams
Returns
$this

Definition at line 179 of file Url.php.

180  {
181  if (isset($data['_nosecret'])) {
182  $this->setNoSecret(true);
183  unset($data['_nosecret']);
184  } else {
185  $this->setNoSecret(false);
186  }
187  unset($data['_scope_to_url']);
188  return parent::_setRouteParams($data, $unsetOldParams);
189  }

◆ findFirstAvailableMenu()

findFirstAvailableMenu ( )

Find first menu item that user is able to access

Returns
string

Implements UrlInterface.

Definition at line 340 of file Url.php.

341  {
342  /* @var $menu \Magento\Backend\Model\Menu\Item */
343  $menu = $this->_getMenu();
344  $item = $menu->getFirstAvailable();
345  $action = $item ? $item->getAction() : null;
346  if (!$item) {
347  $user = $this->_getSession()->getUser();
348  if ($user) {
349  $user->setHasAvailableResources(false);
350  }
351  $action = '*/*/denied';
352  }
353  return $action;
354  }
$user
Definition: dummy_user.php:13

◆ getAreaFrontName()

getAreaFrontName ( )

Return backend area front name, defined in configuration

Returns
string

Implements UrlInterface.

Definition at line 396 of file Url.php.

397  {
398  if (!$this->_getData('area_front_name')) {
399  $this->setData('area_front_name', $this->_backendHelper->getAreaFrontName());
400  }
401  return $this->_getData('area_front_name');
402  }
setData($key, $value=null)
Definition: DataObject.php:72

◆ getSecretKey()

getSecretKey (   $routeName = null,
  $controller = null,
  $action = null 
)

Generate secret key for controller and action based on form key

Parameters
string$routeName
string$controllerController name
string$actionAction name
Returns
string

Implements UrlInterface.

Definition at line 246 of file Url.php.

247  {
248  $salt = $this->formKey->getFormKey();
249  $request = $this->_getRequest();
250  if (!$routeName) {
251  if ($request->getBeforeForwardInfo('route_name') !== null) {
252  $routeName = $request->getBeforeForwardInfo('route_name');
253  } else {
254  $routeName = $request->getRouteName();
255  }
256  }
257  if (!$controller) {
258  if ($request->getBeforeForwardInfo('controller_name') !== null) {
259  $controller = $request->getBeforeForwardInfo('controller_name');
260  } else {
261  $controller = $request->getControllerName();
262  }
263  }
264  if (!$action) {
265  if ($request->getBeforeForwardInfo('action_name') !== null) {
266  $action = $request->getBeforeForwardInfo('action_name');
267  } else {
268  $action = $request->getActionName();
269  }
270  }
271  $secret = $routeName . $controller . $action . $salt;
272  return $this->_encryptor->getHash($secret);
273  }
$controller
Definition: info.phtml:14

◆ getStartupPageUrl()

getStartupPageUrl ( )

Find admin start page url

Returns
string

Implements UrlInterface.

Definition at line 322 of file Url.php.

323  {
324  $menuItem = $this->_getMenu()->get(
325  $this->_scopeConfig->getValue(self::XML_PATH_STARTUP_MENU_ITEM, $this->_scopeType)
326  );
327  if ($menuItem !== null) {
328  if ($menuItem->isAllowed() && $menuItem->getAction()) {
329  return $menuItem->getAction();
330  }
331  }
332  return $this->findFirstAvailableMenu();
333  }

◆ getUrl()

getUrl (   $routePath = null,
  $routeParams = null 
)

Custom logic to retrieve Urls

Parameters
string$routePath
array$routeParams
Returns
string

Implements UrlInterface.

Definition at line 198 of file Url.php.

199  {
200  if (filter_var($routePath, FILTER_VALIDATE_URL)) {
201  return $routePath;
202  }
203 
204  $cacheSecretKey = false;
205  if (isset($routeParams['_cache_secret_key'])) {
206  unset($routeParams['_cache_secret_key']);
207  $cacheSecretKey = true;
208  }
209  $result = parent::getUrl($routePath, $routeParams);
210  if (!$this->useSecretKey()) {
211  return $result;
212  }
213 
214  $this->getRouteParamsResolver()->unsetData('route_params');
215  $this->_setRoutePath($routePath);
216  $extraParams = $this->getRouteParamsResolver()->getRouteParams();
217  $routeName = $this->_getRouteName('*');
218  $controllerName = $this->_getControllerName(self::DEFAULT_CONTROLLER_NAME);
219  $actionName = $this->_getActionName(self::DEFAULT_ACTION_NAME);
220 
221  if (!isset($routeParams[self::SECRET_KEY_PARAM_NAME])) {
222  if (!is_array($routeParams)) {
223  $routeParams = [];
224  }
225  $secretKey = $cacheSecretKey
226  ? "\${$routeName}/{$controllerName}/{$actionName}\$"
227  : $this->getSecretKey($routeName, $controllerName, $actionName);
228  $routeParams[self::SECRET_KEY_PARAM_NAME] = $secretKey;
229  }
230 
231  if (!empty($extraParams)) {
232  $routeParams = array_merge($extraParams, $routeParams);
233  }
234 
235  return parent::getUrl("{$routeName}/{$controllerName}/{$actionName}", $routeParams);
236  }
getSecretKey($routeName=null, $controller=null, $action=null)
Definition: Url.php:246
_getActionName($default=null)
Definition: Url.php:701
_setRoutePath($data)
Definition: Url.php:498
_getRouteName($default=null)
Definition: Url.php:643
_getControllerName($default=null)
Definition: Url.php:672

◆ renewSecretUrls()

renewSecretUrls ( )

Refresh admin menu cache etc.

Returns
void

Implements UrlInterface.

Definition at line 312 of file Url.php.

313  {
314  $this->_cache->clean([\Magento\Backend\Block\Menu::CACHE_TAGS]);
315  }

◆ setSession()

setSession ( \Magento\Backend\Model\Auth\Session  $session)

Set custom auth session

Parameters
\Magento\Backend\Model\Auth\Session$session
Returns
$this

Implements UrlInterface.

Definition at line 375 of file Url.php.

376  {
377  $this->_session = $session;
378  return $this;
379  }

◆ turnOffSecretKey()

turnOffSecretKey ( )

Disable secret key using

Returns
$this

Implements UrlInterface.

Definition at line 301 of file Url.php.

302  {
303  $this->setNoSecret(true);
304  return $this;
305  }

◆ turnOnSecretKey()

turnOnSecretKey ( )

Enable secret key using

Returns
$this

Implements UrlInterface.

Definition at line 290 of file Url.php.

291  {
292  $this->setNoSecret(false);
293  return $this;
294  }

◆ useSecretKey()

useSecretKey ( )

Return secret key settings flag

Returns
bool

Implements UrlInterface.

Definition at line 280 of file Url.php.

281  {
282  return $this->_scopeConfig->isSetFlag(self::XML_PATH_USE_SECURE_KEY) && !$this->getNoSecret();
283  }

Field Documentation

◆ $_backendHelper

$_backendHelper
protected

Definition at line 50 of file Url.php.

◆ $_cache

$_cache
protected

Definition at line 62 of file Url.php.

◆ $_encryptor

$_encryptor
protected

Definition at line 67 of file Url.php.

◆ $_menu

$_menu
protected

Definition at line 38 of file Url.php.

◆ $_menuConfig

$_menuConfig
protected

Definition at line 57 of file Url.php.

◆ $_scope

$_scope
protected

Definition at line 82 of file Url.php.

◆ $_session

$_session
protected

Definition at line 33 of file Url.php.

◆ $_startupMenuItemId

$_startupMenuItemId
protected

Definition at line 45 of file Url.php.

◆ $_storeFactory

$_storeFactory
protected

Definition at line 72 of file Url.php.

◆ $formKey

$formKey
protected

Definition at line 77 of file Url.php.

◆ XML_PATH_USE_SECURE_KEY

const XML_PATH_USE_SECURE_KEY = 'admin/security/use_form_key'

Whether to use a security key in the backend

Bug:
Currently, this constant is slightly misleading: it says "form key", but in fact it is used by URLs, too

Definition at line 26 of file Url.php.


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