Menus

Monday, 12 August 2019

Codelgniter - Controllers


Controllers

Controllers are the heart of your application, as they determine how HTTP requests should be handled.


A Controller is simply a class file that is named in a way that can be associated with a URI.
Consider this URI:
example.com/index.php/blog/
Controller name as Blog.php
The file must be called ‘Blog.php’, with a capital ‘B’.
Class names must start with an uppercase letter.

* index function works by default when the controller is loaded.


Let’s try it: Hello World!


Let’s create a simple controller so you can see it in action. Using your text editor, create a file called Blog.php, and put the following code in it:
<?php
class Blog extends CI_Controller {

        public function index()
        {
                echo 'Hello World!';
        }
}
Then save the file to your application/controllers/ directory.
Now visit the your site using a URL similar to this:
example.com/index.php/blog/

Methods

In the above example the method name is index(). The “index” method is always loaded by default if the second segment of the URI is empty. Another way to show your “Hello World” message would be this:
example.com/index.php/blog/index/


Add a new method to your controller:
<?php
class Blog extends CI_Controller {

 public function index()
 {
  echo "Hello World!";  
 }

 public function test()
 {
  echo "Test method";
 }
}
Now load the following URL to see the comment method:
example.com/index.php/blog/test/

Call one method to anther

<?php
class Blog extends CI_Controller {

   public function index()
 {
  echo "Hello World!"; 
  $this->test(); 
 }

 public function test()
 {
  echo "Test method";
 }
}


Passing URI Segments to your methods

If your URI contains more than two segments they will be passed to your method as parameters.
For example, let’s say you have a URI like this:
example.com/index.php/blog/index/32/Shaneesh
Your method will be passed URI segments 3 and 4 (“sandals” and “123”):
<?php

class Blog extends CI_Controller {

 public function index($id,$name)
 {
  echo $id ." - " ,$name;
 } 

}

?>

Defining a Default Controller

To specify a default controller, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'blog';
by default loaded index method in blog controller.

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.
Example:
<?php
class Blog extends CI_Controller {

        public function __construct()
        {
                parent::__construct();
                // Your own constructor code
        }
}

Private methods

private function _utility()
{
        // some code
}

Processing Output

CodeIgniter has an output class that takes care of sending your final rendered data to the web browser automatically.
public function _output($output)
{
        echo $output;
}

Remapping Method Calls

CodeIgniter permits you to override this behavior through the use of the _remap() method:
public function _remap()
{
        // Some code here...
}

public function _remap($method)
{
        if ($method === 'some_method')
        {
                $this->$method();
        }
        else
        {
                $this->default_method();
        }
}


More details


No comments:

Post a Comment