codeigniter_logo

How to install Codeigniter in Xampp localhost

1. Download Latest Version 3.0.0 : Download Zip
2. Unzip it at your server in folder “codeigniter”. for example :xampp/htaccess/codeigniter

3. Now go to – application/config/config.php Here add base url ie. Base path of your application directory.

For example :

4. Now to go the : application/config/database.php Add database settings here :

For Example :

5 . (Optional) : Remove Index.php from url

Create a .htaccess file at the root of the application for example at :xampp/htaccess/codeigniter – add here .htaccess file

Add the following code in the .htaccess file :

6. You are done : Now your application is ready : Hit the url : http://localhost/codeigniter you will see default welcome message

Setup Maintenance Mode Using Hooks in Codeigniter

image1 (1)

There are so many ways to achieve maintenance mode in codeigniter. Codeigniter itself doesn`t provide such kind of functionality as other framework supports. This post supports how to setup maintenance mode using hooks.

Following are the steps to achieve this:

  1. Enable Hooks in the config.php file
  2. Create a custom config file for maintenance settings i.e maintenance_config.php
  3. Create an html called maintenance_view.php page under views or in root folder
  4. Create a Hook file called maintenance.php
  5. In the hooks config setup your hooks call to run on post_controller

application/views/maintenance_view.php

<!DOCTYPE html>
<html>
  <head>
    <title>Maintenance</title>
    <style>Style your page</style>
  </head>
  <body>
    <p>We apologize but our site is currently undergoing maintenance at this time.</p>
    <p>Please check back later.</p>
  </body>
</html>

application/config/maintenance_config.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['site_under_maintenance'] = TRUE;
?>

application/hooks/maintenance.php

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Maintenance {
  var $CI;
  public function maintenance() {
     $this->CI = & get_instance();
     $this->CI->config->load('maintenance_config'); // Load custom config file
     if ($this->CI->config->item("site_under_maintenance")) {
        include(APPPATH . '/views/maintenance_view.php');
        die();
     }
  }
}
?>

application/config/hooks.php

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
$hook['post_controller'][] = array(
  'class' => 'maintenance',
  'function' => 'maintenance',
  'filename' => 'maintenance.php',
  'filepath' => 'hooks',
  'params' => array()
);
?>

Output

Maintenance

 

Hooks in Codeigniter

CodeIgniter  have gain lot of popularity for developing robust application as it  provides lots of rich features for rapid application development. One of the main feature we will discuss in this article is Hooks.

Hooks allows us to execute a script with specific path within the CodeIgniter execution process without modifying the core files of CodeIgniter

CodeIgniter follows the specific process but also allows the developer to  write their own code within that execution process. In simple language if you need to execute a code that should run every time after Constructor of a controller is loaded, you specify that script  (obviously a  function of a controller) path in hooks.

Hooks is basically a file located in following directory

application/config/hooks.php

There are different kind of hooks point which are distinguished by time when they are called in an execution process . The different types of hooks points  are as follows,

  • post_controller_constructor
  • pre_controller
  • pre_system
  • post_system
  • cache_override
  • display_override
  • post_controller

Initially when you download a fresh copy of CodeIgniter this file is empty except some comments specified. Basically the syntax to specify the hooks is as follows,

$hook[‘post_controller_constructor’] = array(
‘class’ => ‘App_auth’,
‘function’ => ‘index’,
‘filename’ => ‘App_auth.php’,
‘filepath’ => ‘controllers’,
‘params’ => ”
);

Let’s take an example of post_controller_constructor hook point with an example.Specifying  post_controller_constructor as a hook point means, calling the script  after the  contructor is  initiated, but before any function is called e.g. (user/userlist) etc.

As seen above we will write above code in a hook file. It means that callingindex() method of controller App_auth (located in application/controller folder) when any Class contructor is initiated. We can also specify parameters if required. Our App_auth controller will look as follows.

class Auth_auth
{
private $CI;

function Auth_auth()
{
$this->CI = &get_instance();
}

function index()
{
if ($this->CI->session->userdata(‘user_id’) == “” )  // If no session found redirect to login page.
{
redirect(site_url(“login”));
}
}

Now, if user does any activity and if valid session is missing from system, user will be redirected to login page.

In this way we can use different hooks point for different purposes and write our own authentication code or a code which should compulsorily executed if  a specific action occurs in a system.

Note : Hooks feature should be enable in config file,
$config[‘enable_hooks’] = TRUE;

🙂

Remove index.php from url in Codeigniter using .htaccess

codeigniter_logo

CodeIgniter is one of the best and popular php framework available today. Sometimes we need to remove the default index.php that appears in the url of codeigniter application, so that the url becomes more clean and SEO friendly. With the help of .htaccess file it’s very easy to do so.
Paste the following code into your .htaccess file which is on root folder of website, it will remove default index.php from the url:

 

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

 

Enjoy!!…

Setup HMVC with Codeigniter 3.0

codeigniter_logo

Follow below steps to setup HMVC in codeigniter(CI):

https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/downloads

1. Download zip from above URL and copy them into application folder of CI

2. Rewrite HTACCESS

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

3. Create folder called “modules” in application folder i.e. application/modules

4. Create a folder of particular module in modules folder. For e.g. application/modules/admin

5. Create controllers, models and views folder in particular module. For e.g.  in application/modules/admin module

6. Create controller file controllers folder of particular module. For e.g.  application/modules/admin/controllers/Admin.php

<?php

defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);

class Admin extends CI_Controller {

    public function index() {

        $this->load->view(‘admin/dashboard’);

    }

}

7. Following will be the structure:

hmvc-structure

That’s it…