theorythree/navprompt

An easy-to-use navigation helper for your Laravel projects.

v1.1.0 2018-01-09 14:55 UTC

This package is auto-updated.

Last update: 2024-12-18 04:03:44 UTC


README

Latest Version on Packagist Software License Build Status Total Downloads

Description

A simple solution for a simple problem. This package marks HTML elements with a CSS is-active class based on the current URI. Although this package was originally conceived to be used on navigation, it can be used anywhere you need to mark elements with an active class name.

How to Install

Step 1: Via Composer

$ composer require theorythree/navprompt

Step 2: Define the service provider and alias in your Laravel project

This package takes advantage of the new auto-discovery feature found in Laravel 5.5+. If you're using that version of Laravel, then you may skip this step.

Add NavPrompt Service Provider to config/app.php.

'providers' => [

  /*
   * Application Service Providers...
   */

  TheoryThree\NavPrompt\NavPromptServiceProvider::class,
];

Define the NavPrompt Alias to config/app.php.

'aliases' => [

  /*
   * Aliases
   */
  'Nav' => TheoryThree\NavPrompt\NavPromptFacade::class,
];

Step 3. Publish the plugin config file (Optional)

Publish the configuration file to override the package defaults. The package is setup by default to work with Bulma - which uses an active class name of is-active, however if your project is using Bootstrap, you'll want to override the default active class name to active. To do this, publish the config file used by this plugin and set the override for the active_class value to active.

$ php artisan vendor:publish --tag=navprompt This command will generate a config file in config/navprompt.php.

return [

    'active_class' => 'is-active',  // change to 'active' for Bootstrap
];

Usage

This package uses the alias Nav:: to access the plugin class in your project template files. You can use any of the following methods to check your routes against:

  1. routeIsNamed() // checks URI against Laravel named route
  2. routeIs() // checks URI against full URI provided
  3. routeContains() // checks URI to see if it contains provided slug

routeIsNamed()

String routeIsNamed( String $route, String $active=NULL )

Description

Accepts a string of the desired Laravel named route for NavPrompt to check against and returns an empty string (no match) or a string containing the name of the active class (match).

Parameters

$route

String: The Laravel route name to test against

$active

String | NULL (OPTIONAL): The name of the CSS class that should be returned if a URI match occurs

Example

// Route 1: Route::get('/about', 'AboutController@index')->name('about');
// Current URL: http://www.mycompany.com/about

<a href="/about" class="{{ Nav::routeIsNamed('about') }}">About Us</a>
// returns 'is-active' string

// result:
<a href="/about" class="is-active">About Us</a>

routeIs()

String routeIs( String $route, String $active=NULL )

Description

This method is designed to be used in cases when testing the full URI path is needed. For example, if you have several, similar links that you need to test.

Accepts a string of the full URI for NavPrompt to check against and returns an empty string (no match) or a string containing the name of the active class (match). This method is designed to be used in cases where

Parameters

$route

String: The full URI route to test against

$active

String | NULL (OPTIONAL): The name of the CSS class that should be returned if a URI match occurs

Example

// Current URL: http://www.mycompany.com/about/team/dan-merfeld/

<a href="/about/team/dan" class="{{ Nav::routeIs('/about/team/dan') }}">About Dan</a>
// returns 'is-active' string

// result:
<a href="/about/team/dan" class="is-active">About Dan</a>

routeContains()

String routeContains( String|Array $slugs, Int|Array $positions, String $active=NULL )

Description

This method is designed to be used in cases when you need to check for the existence of a specific URI slug within the full URI path.

Accepts a string (or array of strings) of the URI slug(s), and an optional position integer (or array of integers), for NavPrompt to check against and returns an empty string (no match) or a string containing the name of the active class (match). This method is designed to be used in cases where

Parameters

$slugs

String|Array: The URI slug(s) to test against

$positions

Int | Array (OPTIONAL): The position(s) of the URI slug in relation to the full URI.

$active

String | NULL (OPTIONAL): The name of the CSS class that should be returned if a URI match occurs

Example

Basic Example:

// Current URL: http://www.mycompany.com/about/team/dan/

<a href="/about/team/dan" class="{{ Nav::routeContains('team') }}">About Dan</a>
// returns 'is-active' string

// result:
<a href="/about/team/dan" class="is-active">About Dan</a>

Basic Position Example:

// Current URL: http://www.mycompany.com/about/team/dan/

// position 1 = "about"
// position 2 = "team"
// position 3 = "dan"

<a href="/about/team/dan" class="{{ Nav::routeContains('team',1) }}">About Dan</a>
// returns '' string

// result:
<a href="/about/team/dan" class="">About Dan</a>

Advanced Position Example:

// Current URL: http://www.mycompany.com/sports/contact/football/

// position 1 = "sports"
// position 2 = "contact"
// position 3 = "football"

<a href="/contact" class="{{ Nav::routeContains('contact',1) }}">Contact Us</a>
// returns '' string
// Result: <a href="/contact" class="">Contact Us</a>

<a href="/sports/contact" class="{{ Nav::routeContains('sports',[1,2]) }}">List all Contact Sports</a>
// returns 'is_active' string
// Result: <a href="/sports/contact" class="is_active">List all Contact Sports</a>

<a href="/contact/sports" class="{{ Nav::routeContains('sports',[1,2]) }}">List all Contact Sports</a>
// returns 'is_active' string
// Result: <a href="/contact/sports" class="is_active">List all Contact Sports</a>

Change log

Please see CHANGELOG for more information on what has changed recently.

Contributing

All contributors welcome. If you would like to contribute to this package please feel to submit a pull request, submit an issue, or request a feature.

See our CONTRIBUTING and CODE_OF_CONDUCT for more details.

Credits

Contact

Want to get in touch to discuss this package, or another one you'd like us to build? Feel free to reach out to the maintainer of this package by emailing me at dan@theorythree.com, follow or @ me on Twitter @dmerfeld. I'd really like to hear from you. Honest.

License

The MIT License (MIT). Please see License File for more information.