Jump to content

Ahhh! How to assign active class to menu item

Recommended Posts

I have my menu in an include file called menu.php and I want to assign a class called 'active' to the <li> to the page that I am on. How can I do this in PHP?

<!-- navigation -->
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<div class="collapse navbar-collapse" id="collapse">
<ul class="nav navbar-nav navbar-right">
<li><a href="index.php">Home</a></li>
<li><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Insect Control <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="insect-control.php">Insect Control</a></li>
<li><a href="ant-cockroach-control.php">Ant & Cockroach Control</a></li>
<li><a href="bed-bug-treatment.php">Bed Bug Treatments</a></li>
<li><a href="fly-control.php">Fly Control</a></li>
<li><a href="wasp-hornets-bee-control.php">Wasp, Hornet & Bee Control</a></li>
<li><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Rodent & Vermin <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="rodent-vermin.php">Rodent & Vermin</a></li>
<li><a href="rats-mice-infestation.php">Rats & Mice Infestation</a></li>
<li><a href="squirrel-control.php">Squirrel Control</a></li>
<li><a href="#">Contact</a></li>
<!-- end navigation -->
Link to post
Share on other sites

This seemingly simple question actually calls into question a number of things you are doing that aren't great from a security or SEO standpoint.


While Jacques1 handed you a technique to identify the currently requested parent script in a web server environment, your code reveals some things you are doing that could be better handled explicit with a better architecture pattern.  There are many out there, but the most typically use are front-controller/mvc.


For example, if you had a front controller script, you would already know where you were in the control structure all the time, and that state could be passed into the menu.  


In most apps these days, people are moving to a richer client experience with more javascript client code/ ajax etc., and less dependency on page requests.  



It is also much better to obfuscate your underlying architecture for SEO purposes, not to mention the fact it lessens what others know about your hosting environment.


Google likes this:




Much better than yoursite.com/fly-control.php


Your site is easier to maintain as well if you are using a front controller that all requests are going through. With the proliferation of small easy to use micro frameworks like slim, silex, lumen etc. or even symfony3 configured as a microkernel, the answer to your question could be very different if you improved your underlying site architecture.


For reference: http://symfony.com/blog/new-in-symfony-2-8-symfony-as-a-microframework

  • Like 1
Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.