Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter if controller

First of all sorry if its a noob question.

But is it posibble to do this in codeingiter, like if i have a sidebar but i only want to load it in 2 pages

if(controller == 'blog') {
   //load sidebar
}

just like in wordpress if is_page

like image 968
noobman Avatar asked Sep 15 '11 08:09

noobman


People also ask

What is CI_Controller in CodeIgniter?

The pages class is extending the CI_Controller class. This means that the new pages class can access the methods and variables defined in the CI_Controller class ( system/core/Controller. php ). The controller is what will become the center of every request to your web application.

What is $this in CodeIgniter?

To actually answer your question, $this actually represents the singleton Codeigniter instance (which is actually the controller object). For example when you load libraries/models, you're attaching them to this instance so you can reference them as a property of this instance.

How do you call a constructor in CI?

Class Constructors If you intend to use a constructor in any of your Controllers, you MUST place the following line of code in it: parent::__construct(); The reason this line is necessary is because your local constructor will be overriding the one in the parent controller class so we need to manually call it.

What is hook CI?

In CodeIgniter, hooks are events which can be called before and after the execution of a program. It allows executing a script with specific path in the CodeIgniter execution process without modifying the core files.


1 Answers

Use $this->router->fetch_class()

if($this->router->fetch_class() == 'blog') {
   //load sidebar
}

Also $this->uri->segment(2) will work in most cases, but in some cases like mod_rewrite or when using subfolder or route it may fail.

like image 106
Muhammad Usman Avatar answered Sep 21 '22 14:09

Muhammad Usman