Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ class MenuItem implements ArrayableContract
*/
protected $hideWhen;

/**
* Prefix URL.
*
* @var string|null
*/
protected $prefixUrl;

/**
* Constructor.
*
Expand Down Expand Up @@ -91,6 +98,20 @@ protected static function setIconAttribute(array $properties)
return $properties;
}

/**
* Set Prefix URL.
*
* @param string $prefixUrl
*
* @return $this
*/
public function setPrefixUrl($prefixUrl)
{
$this->prefixUrl = $prefixUrl;

return $this;
}

/**
* Get random name.
*
Expand Down Expand Up @@ -202,6 +223,20 @@ public function route($route, $title, $parameters = array(), $order = 0, $attrib
return $this->add(compact('route', 'title', 'order', 'attributes'));
}

/**
* Format URL.
*
* @param string $url
*
* @return string
*/
protected function formatUrl($url)
{
$uri = !is_null($this->prefixUrl) ? $this->prefixUrl . $url : $url;

return $uri == '/' ? '/' : ltrim(rtrim($uri, '/'), '/');
}

/**
* Create new menu item and set the action to url.
*
Expand All @@ -217,12 +252,14 @@ public function url($url, $title, $order = 0, $attributes = array())
$arguments = func_get_args();

return $this->add([
'url' => array_get($arguments, 0),
'url' => $this->formatUrl(array_get($arguments, 0)),
'title' => array_get($arguments, 1),
'attributes' => array_get($arguments, 2),
]);
}

$url = $this->formatUrl($url);

return $this->add(compact('url', 'title', 'order', 'attributes'));
}

Expand Down