diff --git a/src/MenuItem.php b/src/MenuItem.php index 2c7ed53..0a011ef 100644 --- a/src/MenuItem.php +++ b/src/MenuItem.php @@ -59,6 +59,13 @@ class MenuItem implements ArrayableContract */ protected $hideWhen; + /** + * Prefix URL. + * + * @var string|null + */ + protected $prefixUrl; + /** * Constructor. * @@ -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. * @@ -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. * @@ -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')); }