Home > Article > PHP Framework > How to use navigation menu in ThinkPHP6
With the development of the Internet, websites are becoming more and more complex, their functions are becoming more and more abundant, and users’ needs are becoming more and more diversified. In order to facilitate users to quickly locate the required functions, the navigation menu has become indispensable. element. How to use the navigation menu in ThinkPHP6? This article will introduce you step by step.
1. Create a navigation menu data table
In ThinkPHP6, we can use a database to store navigation menu information. First, we need to create a navigation menu data table. The specific table structure is as follows:
CREATE TABLE `menu` ( `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL COMMENT '菜单名称', `url` varchar(255) NOT NULL DEFAULT '' COMMENT '菜单链接地址', `parent_id` bigint(20) UNSIGNED NOT NULL COMMENT '父级菜单ID', `order` int(11) NOT NULL DEFAULT '0' COMMENT '排序', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='导航菜单表';
2. Import data
After creating the navigation menu data table, we need to import the navigation menu data into the database. Generally, we can import data in the following ways:
3. Create menu model
In ThinkPHP6, we can use the model to connect to the database and obtain the data in the database. Therefore, we need to create a menu model and define the table name, primary key, automatic timestamp and other attributes in the model. The specific code is as follows:
<?php namespace appmodel; use thinkModel; class Menu extends Model { protected $table = 'menu'; protected $pk = 'id'; protected $autoWriteTimestamp = true; }
4. Create a menu controller
Menu control The processor is responsible for processing all logic related to the menu, including how to obtain menu data, how to render the menu view, etc. In ThinkPHP6, we can use command line tools to quickly generate controllers. The specific command is as follows:
php think make:controller Menu
After the command execution is completed, we can find the generated Menu.php
file in the appcontroller
directory. Then we can define a method to get the navigation menu in the controller. The specific code is as follows:
<?php namespace appcontroller; use appmodelMenu; use thinkacadeView; class MenuController { public function index() { // 获取一级菜单 $menus = Menu::where('parent_id', 0)->order('order')->select(); // 获取当前选中的菜单ID $selectedId = input('get.id', ''); View::assign([ 'menus' => $menus, 'selectedId' => $selectedId, ]); return View::fetch('index'); } }
In the controller, we use the where
method of the Menu
model To get the first-level menu, then assign the menu data and the currently selected menu ID to the view, and finally render the view.
5. Create a menu view
The menu view is used to display the navigation menu. We can display the navigation menu at the head or left side of the page. The specific location can be determined according to actual needs. In the view, we can use the foreach
loop to traverse the menu data and then output the corresponding HTML code.
<nav> <ul> <?php foreach ($menus as $menu): ?> <li class="<?php if ($selectedId == $menu['id']) echo 'active' ?>"> <a href="<?php echo $menu['url'] ?>"><?php echo $menu['title'] ?></a> </li> <?php endforeach; ?> </ul> </nav>
6. Create a route
In ThinkPHP6, we need to create a route to access the index
method of the menu controller. The specific code is as follows:
use thinkacadeRoute; Route::get('menu/index', 'MenuController@index');
After the route is defined, we can access the menu page through http://localhost/menu/index
.
Summary
The above is the complete process of using navigation menu in ThinkPHP6, including creating navigation menu data table, importing data, creating menu model, creating menu controller, creating menu view and creating routing. Through this example, I believe that everyone has mastered the skills of how to use the navigation menu in ThinkPHP6, and can quickly apply it in the project.
The above is the detailed content of How to use navigation menu in ThinkPHP6. For more information, please follow other related articles on the PHP Chinese website!