About Lesson
The URI Class provides methods that help you retrieve information from your URI strings. CodeIgniter 4 presents an object-oriented solution for working with URIs in your application.
Creating a URI instance:
- Creating a URI instance as a new class instance:
$uri = new CodeIgniterHTTPURI();
- Using
service()
function:$uri = service('uri');
Simple HTML Code for Menu
<ul class="sidebar-menu" data-widget="tree"> <li> <a href="<?php echo base_url("dashboard") ?>"> <i class="fa fa-dashboard"></i> Dashboard </a> </li> <li class="treeview "> <a href="#"> <i class="fa fa-users"></i> <span>Clients</span> <span class="pull-right-container"> <i class="fa fa-angle-left pull-right"></i> </span> </a> <ul class="treeview-menu"> <li> <a href="<?php echo base_url('client/add'); ?>"> <i class="fa fa-circle-o"></i> Add Client </a> </li> <li> <a href="<?php echo base_url('client/list'); ?>"> <i class="fa fa-circle-o"></i> List of Clients </a> </li> </ul> </li> </ul>
URI Segment Code for Menu
<?php // Creating URI instance in View $uri = new CodeIgniter\HTTP\URI(current_url()); $uri_one = ''; if($uri->getTotalSegments() > 0 && $uri->getSegment(1)) $uri_one = $uri->getSegment(1); if($uri->getTotalSegments() > 1 && $uri->getSegment(2)) $uri_two = $uri->getSegment(2); // // Dashboard $dashboard = ''; if($uri_one == '' or $uri_one == 'dashboard') { $dashboard = 'class="active"'; } // Client $client = $client_add = $client_list = ''; if($uri_one == "client") { $client = 'active'; if($url == 'add') { $client_add = 'class="active"'; } if($url == 'list') { $client_list = 'class="active"'; } } ?>
Example:
<?php // Creating URI instance in View $uri = new CodeIgniter\HTTP\URI(current_url()); $uri_one = ''; if($uri->getTotalSegments() > 0 && $uri->getSegment(1)) $uri_one = $uri->getSegment(1); if($uri->getTotalSegments() > 1 && $uri->getSegment(2)) $uri_two = $uri->getSegment(2); // // Dashboard $dashboard = ''; if($uri_one == '' or $uri_one == 'dashboard') { $dashboard = 'class="active"'; } // Client $client = $client_add = $client_list = ''; if($uri_one == "client") { $client = 'active'; if($url == 'add') { $client_add = 'class="active"'; } if($url == 'list') { $client_list = 'class="active"'; } } ?> <ul class="sidebar-menu" data-widget="tree"> <li> <a href="<?php echo base_url("dashboard") ?>"> <i class="fa fa-dashboard"></i> Dashboard </a> </li> <li class="treeview "> <a href="#"> <i class="fa fa-users"></i> <span>Clients</span> <span class="pull-right-container"> <i class="fa fa-angle-left pull-right"></i> </span> </a> <ul class="treeview-menu"> <li> <a href="<?php echo base_url('client/add'); ?>"> <i class="fa fa-circle-o"></i> Add Client </a> </li> <li> <a href="<?php echo base_url('client/list'); ?>"> <i class="fa fa-circle-o"></i> List of Clients </a> </li> </ul> </li> </ul>