├── BootstrapNavGenerator.php ├── LICENSE └── README.md /BootstrapNavGenerator.php: -------------------------------------------------------------------------------- 1 | setActivePath(); 18 | } 19 | 20 | public function setBrand($name, $url = '#', $image = '') { 21 | $this->brandName = $name; 22 | $this->brandUrl = $url; 23 | $this->brandImage = $image; 24 | } 25 | 26 | public function setTheme($theme) { 27 | if (!in_array($theme, ['light', 'dark'])) { 28 | trigger_error("Invalid theme '{$theme}'. Defaulting to 'light'.", E_USER_WARNING); 29 | $theme = 'light'; 30 | } 31 | $this->theme = $theme; 32 | } 33 | 34 | public function setFixed($position) { 35 | $validPositions = ['top', 'bottom']; 36 | if (!in_array($position, $validPositions)) { 37 | trigger_error("Invalid fixed position '{$position}'. Navbar will not be fixed.", E_USER_WARNING); 38 | $position = ''; 39 | } 40 | $this->fixed = $position; 41 | } 42 | 43 | public function setContainer($type) { 44 | $validTypes = ['fluid', 'sm', 'md', 'lg', 'xl', 'xxl']; 45 | $this->containerClass = in_array($type, $validTypes) ? "container-{$type}" : 'container-fluid'; 46 | } 47 | 48 | public function setExpandPoint($breakpoint) { 49 | $validBreakpoints = ['sm', 'md', 'lg', 'xl', 'xxl']; 50 | $this->navbarClass = in_array($breakpoint, $validBreakpoints) ? "navbar-expand-{$breakpoint}" : 'navbar-expand-lg'; 51 | } 52 | 53 | public function addMenuItem($label, $url, $submenu = [], $icon = '', $customClass = '') { 54 | $this->menuItems[] = [ 55 | 'label' => $label, 56 | 'url' => $url, 57 | 'submenu' => $submenu, 58 | 'icon' => $icon, 59 | 'customClass' => $customClass 60 | ]; 61 | } 62 | 63 | public function addSearchForm($placeholder = 'Search', $buttonText = 'Search') { 64 | $this->searchForm = [ 65 | 'placeholder' => $placeholder, 66 | 'buttonText' => $buttonText 67 | ]; 68 | } 69 | 70 | public function addCustomClass($class) { 71 | $this->customClasses[] = $class; 72 | } 73 | 74 | public function generateMenu() { 75 | $navbarClasses = [ 76 | 'navbar', 77 | $this->navbarClass, 78 | "navbar-{$this->theme}", 79 | "bg-{$this->theme}", 80 | $this->fixed ? "fixed-{$this->fixed}" : '', 81 | ...$this->customClasses 82 | ]; 83 | 84 | $html = ''; 98 | 99 | return $html; 100 | } 101 | 102 | public function setActivePath($path = null) { 103 | if ($path === null) { 104 | // Automatically detect the current URL 105 | $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); 106 | } 107 | $this->activePath = $path; 108 | } 109 | 110 | private function isActive($url) { 111 | return $this->activePath === $url || 112 | ($url !== '/' && strpos($this->activePath, $url) === 0); 113 | } 114 | 115 | private function generateBrand() { 116 | if (empty($this->brandName) && empty($this->brandImage)) { 117 | return ''; 118 | } 119 | 120 | $brandContent = ''; 121 | if (!empty($this->brandImage)) { 122 | $brandContent .= '' . htmlspecialchars($this->brandName) . ''; 123 | } 124 | $brandContent .= htmlspecialchars($this->brandName); 125 | 126 | return '' . $brandContent . ''; 127 | } 128 | 129 | private function generateMenuItems($items = null) { 130 | $html = ''; 131 | $items = $items ?? $this->menuItems; 132 | 133 | foreach ($items as $item) { 134 | if (empty($item['submenu'])) { 135 | $html .= $this->generateMenuItem($item); 136 | } else { 137 | $html .= $this->generateDropdownItem($item); 138 | } 139 | } 140 | 141 | return $html; 142 | } 143 | 144 | private function generateMenuItem($item) { 145 | $icon = !empty($item['icon']) ? '' : ''; 146 | $customClass = !empty($item['customClass']) ? ' ' . htmlspecialchars($item['customClass']) : ''; 147 | $activeClass = $this->isActive($item['url']) ? ' active' : ''; 148 | 149 | return ''; 152 | } 153 | 154 | private function generateDropdownItem($item) { 155 | $icon = !empty($item['icon']) ? '' : ''; 156 | $customClass = !empty($item['customClass']) ? ' ' . htmlspecialchars($item['customClass']) : ''; 157 | $activeClass = $this->isActive($item['url']) ? ' active' : ''; 158 | 159 | $html = ''; 177 | 178 | return $html; 179 | } 180 | 181 | private function generateMultiLevelDropdownItem($item) { 182 | $icon = !empty($item['icon']) ? '' : ''; 183 | $html = ''; 201 | 202 | return $html; 203 | } 204 | 205 | private function generateSearchForm() { 206 | if (!$this->searchForm) { 207 | return ''; 208 | } 209 | 210 | return ''; 214 | } 215 | } 216 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bootstrap 5 Navigation Menu Generator 2 | 3 | ## Table of Contents 4 | 1. [Introduction](#introduction) 5 | 2. [Features](#features) 6 | 3. [Requirements](#requirements) 7 | 4. [Installation](#installation) 8 | 5. [Usage](#usage) 9 | 6. [Configuration Options](#configuration-options) 10 | 7. [Examples](#examples) 11 | 8. [Customization](#customization) 12 | 9. [Contributing](#contributing) 13 | 10. [License](#license) 14 | 15 | ## Introduction 16 | 17 | The Bootstrap 5 Navigation Menu Generator is a PHP class that simplifies the process of creating dynamic, responsive navigation menus for websites using Bootstrap 5. It provides a flexible and easy-to-use interface for generating customizable navigation bars with support for dropdown menus, search forms, and active item highlighting. 18 | 19 | ## Features 20 | 21 | - Dynamic generation of Bootstrap 5 compatible navigation menus 22 | - Support for dropdown menus (multi-level) 23 | - Customizable brand/logo section 24 | - Light and dark theme options 25 | - Fixed positioning (top or bottom) 26 | - Responsive design with customizable breakpoints 27 | - Search form integration 28 | - Icon support (compatible with Bootstrap Icons or other icon libraries) 29 | - Dynamic active item highlighting based on current URL 30 | - Custom CSS class support for additional styling 31 | 32 | ## Requirements 33 | 34 | - PHP 7.0 or higher 35 | - Bootstrap 5 CSS and JS files 36 | - (Optional) Bootstrap Icons or another icon library for menu item icons 37 | 38 | ## Installation 39 | 40 | 1. Download the `BootstrapNavGenerator.php` file. 41 | 2. Place it in your project directory where you keep your PHP classes. 42 | 3. Include the file in your PHP script: 43 | 44 | ```php 45 | require_once 'path/to/BootstrapNavGenerator.php'; 46 | ``` 47 | 48 | ## Usage 49 | 50 | Here's a basic example of how to use the Bootstrap 5 Navigation Menu Generator: 51 | 52 | ```php 53 | setBrand('My Website', '/'); 58 | $nav->addMenuItem('Home', '/'); 59 | $nav->addMenuItem('About', '/about'); 60 | $nav->addMenuItem('Services', '/services', [ 61 | ['label' => 'Web Design', 'url' => '/services/web-design'], 62 | ['label' => 'SEO', 'url' => '/services/seo'] 63 | ]); 64 | $nav->addMenuItem('Contact', '/contact'); 65 | 66 | echo $nav->generateMenu(); 67 | ?> 68 | ``` 69 | 70 | ## Configuration Options 71 | 72 | ### Setting the Brand 73 | ```php 74 | $nav->setBrand('My Website', '/', 'path/to/logo.png'); 75 | ``` 76 | 77 | ### Changing the Theme 78 | ```php 79 | $nav->setTheme('dark'); // or 'light' 80 | ``` 81 | 82 | ### Fixed Positioning 83 | ```php 84 | $nav->setFixed('top'); // or 'bottom' 85 | ``` 86 | 87 | ### Container Type 88 | ```php 89 | $nav->setContainer('lg'); // 'fluid', 'sm', 'md', 'lg', 'xl', 'xxl' 90 | ``` 91 | 92 | ### Expansion Breakpoint 93 | ```php 94 | $nav->setExpandPoint('md'); // 'sm', 'md', 'lg', 'xl', 'xxl' 95 | ``` 96 | 97 | ### Adding a Search Form 98 | ```php 99 | $nav->addSearchForm('Search our site...'); 100 | ``` 101 | 102 | ### Adding Custom Classes 103 | ```php 104 | $nav->addCustomClass('my-custom-navbar'); 105 | ``` 106 | 107 | ## Examples 108 | 109 | ### Full Example with All Features 110 | 111 | ```php 112 | setBrand('My Website', '/', 'path/to/logo.png'); 117 | $nav->setTheme('dark'); 118 | $nav->setFixed('top'); 119 | $nav->setContainer('lg'); 120 | $nav->setExpandPoint('md'); 121 | $nav->addMenuItem('Home', '/', [], 'bi bi-house'); 122 | $nav->addMenuItem('About', '/about', [], 'bi bi-info-circle'); 123 | $nav->addMenuItem('Services', '/services', [ 124 | ['label' => 'Web Design', 'url' => '/services/web-design', 'icon' => 'bi bi-brush'], 125 | ['label' => 'SEO', 'url' => '/services/seo', 'icon' => 'bi bi-search'] 126 | ], 'bi bi-gear'); 127 | $nav->addMenuItem('Contact', '/contact', [], 'bi bi-envelope'); 128 | $nav->addSearchForm('Search our site...'); 129 | $nav->addCustomClass('custom-navbar'); 130 | 131 | echo $nav->generateMenu(); 132 | ?> 133 | ``` 134 | 135 | ## Customization 136 | 137 | You can further customize the navigation menu by extending the `BootstrapNavGenerator` class or by modifying the existing methods to suit your specific needs. 138 | 139 | ## Contributing 140 | 141 | Contributions to improve the Bootstrap 5 Navigation Menu Generator are welcome. Here's how you can contribute: 142 | 143 | 1. **Reporting Issues**: If you find a bug or have a suggestion for improvement, please open an issue in the GitHub repository. Provide as much detail as possible, including steps to reproduce the issue if applicable. 144 | 145 | 2. **Submitting Pull Requests**: If you'd like to contribute code: 146 | - Fork the repository 147 | - Create a new branch for your feature or bug fix 148 | - Make your changes 149 | - Submit a pull request with a clear description of the changes 150 | 151 | 3. **Improving Documentation**: If you notice areas where the documentation could be improved or expanded, feel free to suggest changes. 152 | 153 | 4. **Sharing Ideas**: If you have ideas for new features or improvements, open an issue to discuss them. 154 | 155 | Before making significant changes, it's a good idea to open an issue to discuss the proposed changes with the maintainers. 156 | 157 | Please ensure that your contributions adhere to: 158 | - The existing code style 159 | - Best practices for PHP and Bootstrap 5 160 | - Proper documentation of new features or changes 161 | 162 | By contributing to this project, you acknowledge that your contributions will be released under The Unlicense, effectively placing them in the public domain. 163 | 164 | Thank you for helping to improve the Bootstrap 5 Navigation Menu Generator! 165 | 166 | ## License 167 | 168 | This project is licensed under The Unlicense - see the [UNLICENSE](UNLICENSE) file for details. 169 | 170 | This means that you are free to do whatever you want with this software. You can use it, modify it, distribute it, or sell it without any restrictions. The authors have released it into the public domain, dedicating all their rights to the work to the public domain worldwide. 171 | 172 | For more information about The Unlicense, visit [https://choosealicense.com/licenses/unlicense/](https://choosealicense.com/licenses/unlicense/). 173 | 174 | --- 175 | 176 | For more information or support, please open an issue in the GitHub repository. 177 | --------------------------------------------------------------------------------