When developing a website, it is often necessary to add a navigation bar to facilitate users to access different pages of the website. When developing using the PHP framework ThinkPHP, how to determine which navigation bar the current page belongs to? This article will introduce to you how to implement the ThinkPHP navigation bar and how to determine the current page.
1. ThinkPHP navigation bar implementation method
In ThinkPHP, the public template method is usually used to implement the navigation bar. The specific steps are as follows:
1. First create a public template file, such as header.html, to store the navigation bar code of the website. In this file, you can customize the style and layout of the navigation bar according to your needs.
2. Reference the public template file in each sub-page and embed the navigation bar code into the sub-page. In the ThinkPHP framework, use the following code to reference the public template file:
<?php include(THEME_PATH . 'header.html'); ?>
Where THEME_PATH is the user-defined theme path.
3. In the public template file, you need to use PHP code to determine the navigation bar to which the current page belongs. Usually, the $_SERVER['REQUEST_URI'] variable is used to obtain the URL address of the current page, and then the if statement is used to determine the navigation bar to which the current page belongs.
For example, the following code can determine whether the current page belongs to the homepage navigation bar:
<?php if(strpos($_SERVER['REQUEST_URI'], 'index/index') !== false) {?> <li class="active"><a href="/index.php/Home/Index/">首页</a></li> <?php } else { ?> <li><a href="/index.php/Home/Index/">首页</a></li> <?php } ?>
In the above code, the strpos() function is used to determine whether the URL address of the current page contains 'index/index' String, if there is one, it means that the current page belongs to the homepage navigation bar, and then add the selected state to the current navigation bar by setting the active attribute of the
2. Method to determine the navigation bar to which the current page belongs
In the previous step, we have introduced how to use the if statement to determine the navigation bar to which the current page belongs. However, in actual development, this method may cause some problems, such as: if the URL address contains parameters, how to determine the navigation bar to which the current page belongs? At this point, you need to use more intelligent methods to solve the problem.
In the ThinkPHP framework, you can intelligently determine the navigation bar to which the current page belongs by defining the navigation bar array and determining the current controller and operation method. The specific steps are as follows:
1. Define the navigation bar array in the configuration file. For example:
//定义导航栏数组 'NAV_LIST' => array( '首页' => array('Index', 'index'), '新闻' => array('News', 'index'), '产品' => array('Product', 'index'), '联系我们' => array('Contact', 'index'), ),
In the above array, the key represents the name of the navigation bar, and the value is an array containing the names of the controller and operation methods.
2. In the public template file, use a foreach loop to traverse the navigation bar array, and use an if statement to determine whether the current controller and operation method are the same as the values in the navigation bar array. If they are the same, add the active attribute to the navigation bar to indicate that the current page belongs to it.
For example, the following code can intelligently determine the navigation bar to which the current page belongs:
<?php foreach(C('NAV_LIST') as $key=>$vo){ ?> <?php $active = strtolower(CONTROLLER_NAME) == strtolower($vo[0]) && strtolower(ACTION_NAME) == strtolower($vo[1]) ? 'class=active' : ''; ?> <li <?php echo $active;?>><a href="<?php echo U($vo[0].'/'.$vo[1]);?>"><?php echo $key;?></a></li> <?php } ?>
In the above code, a foreach loop is used to traverse the navigation bar array. Then, use an if statement to determine whether the current controller and operation method are the same as the values in the navigation bar array. If they are the same, add the active attribute to the
To sum up, whether you are using an if statement to determine which navigation bar the current page belongs to, or using an intelligent judgment method, you need to clarify the relevant knowledge of the ThinkPHP framework. I hope the introduction in this article can be helpful to everyone when developing websites using the ThinkPHP framework.
The above is the detailed content of How to determine the current page in thinkphp navigation bar. For more information, please follow other related articles on the PHP Chinese website!