diff --git a/README.md b/README.md index 125a30b..12ebc00 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ __For Laravel 4.x, check [version 1.5.0](https://github.com/lavary/laravel-menu/ * [Active Item](#active-item) - [RESTful URLs](#restful-urls) - [URL Wildcards](#url-wildcards) + - [Disable activation](#disable-activation) * [Inserting a Separator](#inserting-a-separator) * [Append and Prepend](#append-and-prepend) * [Before and After](#before-and-after) @@ -775,6 +776,15 @@ $menu->add('Articles', 'articles')->active('this-is-another-url/*'); So `this-is-another-url`, `this-is-another-url/and-another` will both activate `Articles` item. +#### Disable activation +Sometimes you may need to disable auto activation for single items. +You can pass **disableActivationByURL** in options like this: +```php +$menu->add('Anchor', ['disableActivationByURL' => true, 'url' => '#']); +``` +This prevents auto activation by matching URL. +But activation for items with active children keeps working. + ## Inserting a Separator You can insert a separator after each item using `divide()` method: diff --git a/src/Lavary/Menu/Item.php b/src/Lavary/Menu/Item.php index 72a36c1..082e874 100644 --- a/src/Lavary/Menu/Item.php +++ b/src/Lavary/Menu/Item.php @@ -99,6 +99,14 @@ class Item */ public $isActive = false; + /** + * If true this prevents auto activation by matching URL + * Activation by active children keeps working. + * + * @var bool + */ + private $disableActivationByURL = false; + /** * Creates a new Item instance. * @@ -125,6 +133,9 @@ public function __construct($builder, $id, $title, $options) } else { $path = Arr::only($options, array('url', 'route', 'action', 'secure')); } + if (isset($options['disableActivationByURL']) && true == $options['disableActivationByURL']) { + $this->disableActivationByURL = true; + } if (!is_null($path)) { $path['prefix'] = $this->builder->getLastGroupPrefix(); @@ -141,7 +152,7 @@ public function __construct($builder, $id, $title, $options) /** * Creates a sub Item. * - * @param string $title + * @param string $title * @param string|array $options * @return Item */ @@ -352,6 +363,9 @@ public function all() */ public function checkActivationStatus() { + if (true === $this->disableActivationByURL) { + return; + } if (true == $this->builder->conf['restful']) { $path = ltrim(parse_url($this->url(), PHP_URL_PATH), '/'); $rpath = ltrim(parse_url(Request::path(), PHP_URL_PATH), '/');