NavbarHelper
extends Helper
in package
uses
ClassTrait, EasyIconTrait, StringTemplateTrait, UrlComparerTrait
Navbar helper library.
Automatic generation of Bootstrap HTML navbars.
Table of Contents
Properties
- $easyIcon : bool
- Set to false to disable easy icon processing.
- $Form : FormHelper
- $helpers : array<string|int, mixed>
- Other helpers used by NavbarHelper.
- $Html : HtmlHelper
- $Url : UrlHelper
- $_defaultConfig : array<string|int, mixed>
- Default configuration for the helper.
- $_level : int
- Menu level (0 = out of menu, 1 = main horizontal menu, 2 = dropdown menu).
- $_parts : array<string|int, mixed>
- Parts of the URL used for normalization.
- $_responsive : bool
- Indicates if the navbar is responsive or not.
Methods
- addClass() : array<string|int, mixed>
- Adds the given class to the element options.
- beginMenu() : string
- Start a new menu.
- compareUrls() : bool
- Check if first URL is a parent of the right URL, without regards to query parameters or hash.
- create() : string
- Create a new navbar.
- divider() : string
- Add a divider to an inner menu of the navbar.
- end() : string
- Close a navbar.
- endMenu() : string
- End a menu.
- header() : string
- Add a header to an inner menu of the navbar.
- link() : string
- Add a link to the navbar or to a menu.
- text() : string
- Add a text to the navbar.
- _addButtonClasses() : array<string|int, mixed>
- Add classes to options according to the default values of bootstrap-type and bootstrap-size for button (see configuration).
- _easyIconOption() : array<string|int, mixed>
- Remove the `easyIcon` option from the given array and return it together with the array.
- _hostname() : string|null
- Retrieve the hostname (if any).
- _injectIcon() : string
- Inject icon into the given string.
- _isAssociativeArray() : bool
- Check weither the specified array is associative or not.
- _makeIcon() : string
- Try to convert the specified string to a bootstrap icon. The string is converted if it matches a format `i:icon-name` (leading and trailing spaces or ignored) and if easy-icon is activated.
- _matchHost() : bool
- Checks if the given URL components match the current host.
- _matchRelative() : bool
- Checks if the given URL components match the current relative URL. This methods only works with full URL, and do not check the host.
- _normalize() : string|null
- Normalize an URL.
- _relative() : string
- Retrieve the relative path of the root URL from hostname.
- _removeRelative() : string
- Remove relative part an URL (if any).
Properties
$easyIcon
Set to false to disable easy icon processing.
public
bool
$easyIcon
= true
$Form
public
FormHelper
$Form
$helpers
Other helpers used by NavbarHelper.
public
array<string|int, mixed>
$helpers
= ["Html", "Url"]
$Html
public
HtmlHelper
$Html
$Url
public
UrlHelper
$Url
$_defaultConfig
Default configuration for the helper.
protected
array<string|int, mixed>
$_defaultConfig
= ["templates" => ["navbarStart" => '<nav class="navbar{{attrs.class}}"{{attrs}}>{{containerStart}}{{header}}{{responsiveStart}}', "navbarEnd" => "{{responsiveEnd}}{{containerEnd}}</nav>", "containerStart" => '<div class="container{{attrs.class}}"{{attrs}}>', "containerEnd" => "</div>", "responsiveStart" => '<div class="collapse navbar-collapse{{attrs.class}}" id="{{id}}"{{attrs}}>', "responsiveEnd" => "</div>", "header" => "{{brand}}{{toggleButton}}", "toggleButton" => '<button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#{{id}}" aria-controls="{{id}}" aria-label="{{label}}" aria-expanded="false">
<span class="navbar-toggler-icon"></span>
</button>', "brand" => '<a class="navbar-brand{{attrs.class}}" href="{{url}}"{{attrs}}>{{content}}</a>', "brandImage" => '<img alt="{{brandname}}" src="{{src}}"{{attrs}} />', "dropdownMenuStart" => '<div class="dropdown-menu{{attrs.class}}"{{attrs}}>', "dropdownMenuEnd" => "</div>", "dropdownLink" => '<a href="{{url}}" class="nav-link dropdown-toggle{{attrs.class}}" data-toggle="dropdown" role="button"
aria-haspopup="true" aria-expanded="false">{{content}}</a>', "innerMenuStart" => '<li class="nav-item dropdown{{attrs.class}}"{{attrs}}>{{dropdownLink}}{{dropdownMenuStart}}', "innerMenuEnd" => "{{dropdownMenuEnd}}</li>", "innerMenuItem" => "{{link}}", "innerMenuItemLink" => '<a href="{{url}}" class="dropdown-item{{attrs.class}}"{{attrs}}>{{content}}</a>', "innerMenuItemActive" => "{{link}}", "innerMenuItemLinkActive" => '<a href="{{url}}" class="dropdown-item active{{attrs.class}}"{{attrs}}>{{content}}</a>', "innerMenuItemDivider" => '<div role="separator" class="dropdown-divider{{attrs.class}}"{{attrs}}></div>', "innerMenuItemHeader" => '<h6 class="dropdown-header{{attrs.class}}"{{attrs}}>{{content}}</h6>', "outerMenuStart" => '<ul class="navbar-nav mr-auto{{attrs.class}}"{{attrs}}>', "outerMenuEnd" => "</ul>", "outerMenuItem" => '<li class="nav-item{{attrs.class}}"{{attrs}}>{{link}}</li>', "outerMenuItemLink" => '<a href="{{url}}" class="nav-link{{attrs.class}}"{{attrs}}>{{content}}</a>', "outerMenuItemActive" => '<li class="nav-item active{{attrs.class}}"{{attrs}}>{{link}}</li>', "outerMenuItemLinkActive" => '<a href="{{url}}" class="nav-link{{attrs.class}}"{{attrs}}>{{content}}</a>', "navbarText" => '<span class="navbar-text{{attrs.class}}"{{attrs}}>{{content}}</span>'], "templateClass" => "Bootstrap\\View\\EnhancedStringTemplate", "autoActiveLink" => true]
autoActiveLinkSet totrueto automatically addactiveclass when given URL for a link matches the current URL. Default istrue.autoButtonLinkSet to trueto automatically create buttons instead of links when outside a menu. Default istrue`.
$_level
Menu level (0 = out of menu, 1 = main horizontal menu, 2 = dropdown menu).
protected
int
$_level
= 0
$_parts
Parts of the URL used for normalization.
protected
array<string|int, mixed>
$_parts
= ["plugin", "prefix", "controller", "action", "pass"]
$_responsive
Indicates if the navbar is responsive or not.
protected
bool
$_responsive
= false
Methods
addClass()
Adds the given class to the element options.
public
addClass([array<string|int, mixed> $options = [] ][, string|array<string|int, mixed> $class = null ][, string $key = "class" ]) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed> = []
-
Array of options/attributes to add a class to.
- $class : string|array<string|int, mixed> = null
-
The class names to be added.
- $key : string = "class"
-
The key to use for class (default to
'class').
Return values
array<string|int, mixed> —Array of options with $key set or modified.
beginMenu()
Start a new menu.
public
beginMenu([string|array<string|int, mixed>|null $name = null ][, string|array<string|int, mixed> $url = null ][, array<string|int, mixed> $options = [] ][, array<string|int, mixed> $linkOptions = [] ][, array<string|int, mixed> $listOptions = [] ]) : string
Two types of menus exist:
- Horizontal hover menu in the navbar (level 0).
- Vertical dropdown menu (level 1). The menu level is determined automatically: A dropdown menu needs to be part of a hover menu. In the hover menu case, pass the options array as the first argument.
You can populate the menu with link(), divider(), and sub menus.
Use 'class' => 'navbar-right' option for flush right.
Note: The $linkOptions and $listOptions parameters are not used for menu
at level 0 (horizontal menu).
Options
templateVarsProvide template variables for the menu template.- Other attributes will be assigned to the menu element.
Link Options
- Other attributes will be assigned to the link element.
List Options
- Other attributes will be assigned to the list element.
Parameters
- $name : string|array<string|int, mixed>|null = null
-
Name of the menu.
- $url : string|array<string|int, mixed> = null
-
URL for the menu.
- $options : array<string|int, mixed> = []
-
Array of options for the wrapping element.
- $linkOptions : array<string|int, mixed> = []
-
Array of options for the link. See above.
- $listOptions : array<string|int, mixed> = []
-
Array of options for the openning
ulelements.
Return values
string —HTML elements to start a menu.
compareUrls()
Check if first URL is a parent of the right URL, without regards to query parameters or hash.
public
compareUrls(string|array<string|int, mixed> $lhs[, string|array<string|int, mixed> $rhs = null ][, array<string|int, mixed> $parts = [] ]) : bool
Parameters
- $lhs : string|array<string|int, mixed>
-
First URL to compare.
- $rhs : string|array<string|int, mixed> = null
-
Second URL to compare. Default is current URL (
Router::url()). - $parts : array<string|int, mixed> = []
-
Include pass parameters
Return values
bool —true if both URL match, false otherwise.
create()
Create a new navbar.
public
create([string|null $brand = null ][, array<string|int, mixed> $options = [] ]) : string
Options:
containerWrap the inner content inside a container. Default isfalse.fixedFixed navbar. Possible values are'top','bottom',false. Default isfalse.themeNavbar theme. Either a'light','dark'or an array with] 2 values corresponding to the theme and the background (e.g.['dark', 'primary']). Can also be set tofalseto disable automatic theming. Default is'light'.collapseSpecify when the navbar should collapse. Possible values arefalse(never),true(always) or a string ('lg','xl', ...). Default is'lg'.stickySticky navbar. Default isfalse.templateVarsProvide template variables for the template.- Other attributes will be assigned to the navbar element.
Parameters
- $brand : string|null = null
-
Brand name.
- $options : array<string|int, mixed> = []
-
Array of options. See above.
Return values
string —containing the HTML starting element of the navbar.
divider()
Add a divider to an inner menu of the navbar.
public
divider([array<string|int, mixed> $options = [] ]) : string
Options
templateVarsProvide template variables for the divider template.- Other attributes will be assigned to the divider element.
Parameters
- $options : array<string|int, mixed> = []
-
Array of options. See above.
Return values
string —A HTML dropdown divider tag.
end()
Close a navbar.
public
end() : string
Return values
string —HTML elements to close the navbar.
endMenu()
End a menu.
public
endMenu() : string
Return values
string —HTML elements to close a menu.
header()
Add a header to an inner menu of the navbar.
public
header(string $name[, array<string|int, mixed> $options = [] ]) : string
Options
templateVarsProvide template variables for the header template.- Other attributes will be assigned to the header element.
Parameters
- $name : string
-
Title of the header.
- $options : array<string|int, mixed> = []
-
Array of options for the wrapper tag.
Return values
string —A HTML header tag.
link()
Add a link to the navbar or to a menu.
public
link(string $name[, string|array<string|int, mixed> $url = "" ][, array<string|int, mixed> $options = [] ][, array<string|int, mixed> $linkOptions = [] ]) : string
Encapsulate links with beginMenu(), endMenu() to create
a horizontal hover menu in the navbar or a dropdown menu.
Options
activeIndicates if the link is the current one. Default is automatically deduced ifautoActiveLinkis on, otherwize default isfalse.templateVarsProvide template variables for the templates.- Other attributes will be assigned to the navbar link element.
Parameters
- $name : string
-
The link text.
- $url : string|array<string|int, mixed> = ""
-
The link URL (CakePHP way).
- $options : array<string|int, mixed> = []
-
Array of attributes for the wrapper tag (outer menu) or the link (inner menu).
- $linkOptions : array<string|int, mixed> = []
-
Array of attributes for the link in outer menu. Not used if in inner menu.
Return values
string —A HTML tag wrapping the link.
text()
Add a text to the navbar.
public
text(string $text[, array<string|int, mixed> $options = [] ]) : string
Options
templateVarsProvide template variables for the text template.- Other attributes will be assigned to the text element.
Parameters
- $text : string
-
The text message.
- $options : array<string|int, mixed> = []
-
Array attributes for the wrapper element.
Return values
string —A HTML element wrapping the text for the navbar.
_addButtonClasses()
Add classes to options according to the default values of bootstrap-type and bootstrap-size for button (see configuration).
protected
_addButtonClasses(array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed>
-
The initial options with btype and/or size, block values.
Return values
array<string|int, mixed> —The new options with class values (btn, and btn-* according to initial options).
_easyIconOption()
Remove the `easyIcon` option from the given array and return it together with the array.
protected
_easyIconOption(array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed>
-
Array of options from which the easy-icon option should be extracted.
Return values
array<string|int, mixed> —An array containing the options and the easy-icon option.
_hostname()
Retrieve the hostname (if any).
protected
_hostname() : string|null
Return values
string|null —The hostname, or null.
_injectIcon()
Inject icon into the given string.
protected
_injectIcon(string $title, bool $easyIcon) : string
Parameters
- $title : string
-
Input string where icon should be injected following the easy-icon process.
- $easyIcon : bool
-
Boolean indicating if the easy-icon process should be applied.
Return values
string_isAssociativeArray()
Check weither the specified array is associative or not.
protected
_isAssociativeArray(array<string|int, mixed> $array) : bool
Parameters
- $array : array<string|int, mixed>
-
The array to check.
Return values
bool —true if the array is associative, false otherwize.
_makeIcon()
Try to convert the specified string to a bootstrap icon. The string is converted if it matches a format `i:icon-name` (leading and trailing spaces or ignored) and if easy-icon is activated.
protected
_makeIcon(string $text[, bool &$converted = false ]) : string
Note: This function will currently fail if the Html helper associated with the view is not BootstrapHtmlHelper.
Parameters
- $text : string
-
The string to convert.
- $converted : bool = false
-
If specified, will contains
trueif the text was converted,falseotherwize.
Return values
string —The text after conversion.
_matchHost()
Checks if the given URL components match the current host.
protected
_matchHost(string $url) : bool
Parameters
- $url : string
-
URL to check.
Return values
bool —true if the URL matches, false otherwise.
_matchRelative()
Checks if the given URL components match the current relative URL. This methods only works with full URL, and do not check the host.
protected
_matchRelative(string $url) : bool
Parameters
- $url : string
-
URL to check.
Return values
bool —true if the URL matches, false otherwise.
_normalize()
Normalize an URL.
protected
_normalize(string|array<string|int, mixed> $url[, array<string|int, mixed> $parts = [] ]) : string|null
Parameters
- $url : string|array<string|int, mixed>
-
URL to normalize.
- $parts : array<string|int, mixed> = []
-
Include pass parameters.
Return values
string|null —Normalized URL.
_relative()
Retrieve the relative path of the root URL from hostname.
protected
_relative() : string
Return values
string —The relative path.
_removeRelative()
Remove relative part an URL (if any).
protected
_removeRelative(string $url) : string
Parameters
- $url : string
-
URL from which the relative part should be removed.
Return values
string —The new URL.