phpGrid and CodeIgniter Integration

phpgrid ci319

Introduction

CodeIgniter is a popular, open source PHP framework loosely based on the MVC development pattern. It is used to build dynamic web sites. Out of box, phpGrid is a ready-to-use PHP datagrid solution. Integrating phpGrid with CodeIgniter couldn’t be easier. Here’s how.

Install CodeIgniter

Download CodeIgniter here. To install CodeIgniter, follow these four steps:

  1. Unzip the package.
  2. Upload the CodeIgniter folders and files to your server. Normally the index.php file will be at the root.
  3. Open the “application/config/config.php” file with a text editor and set your base URL. If you intend to use encryption or sessions, set your encryption key.
  4. If you intend to use a database, open the “application/config/database.php” file with a text editor and set your database settings.

Install phpGrid

Download phpGrid here. To install phpGrid, follow these steps:

  1. Unzip the phpGrid download file.
  2. Upload the unzipped phpGrid folder to the “application/libraries” folder.
  3. Complete the installation by configuring the conf.php file. For instructions on how to do this, see setup phpGrid configuration.

CodeIgniter Controller

For the purpose of this tutorial, we will directly modify the default controller file “Welcome.php”. In practice, the changes can be made in any new or existing controller file. Notice that we did not initialize the class using the standard:

1
$this->load->library('someclass');

Where “someclass is” the file name, without the “.php” file extension. Instead we simply include the phpGrid configuration file. The “APPPATH” constant is the path to your application folder. Unlike with the Laravel framework, with CodeIgniter you can directly include PHP classes and working with them. phpGrid is a complex component and composed of multiple classes.

1
2
3
4
5
6
7
8
9
public function index()
{
    // $this->load->view('welcome_message');

    require_once(APPPATH. 'libraries/phpGrid_Lite/conf.php'); // APPPATH is path to application folder
    $data['phpgrid'] = new C_DataGrid("SELECT * FROM Orders", "orderNumber", "Orders"); //$this->ci_phpgrid->example_method(3);

    $this->load->view('show_grid',$data);
}

Note the last line is our new view we are about to create. The view is called “show_grid”.

phpGrid-Codeigniter-code

Create View

Creating a view is very simple.  All you have to do is to put  <?php $phpgrid->display(); ?> somewhere in the view. Learn more about creating a basic PHP grid here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Show Grid</title>
</head>
<body>

<div id="container">
    <h1>Welcome to CodeIgniter! Show me the grid!</h1>

    <div id="body">
        <?php $phpgrid->display(); ?>
    </div>

</div>

</body>
</html>

That’s it! phpGrid handles all the CRUD operations. We don’t need to create a Model for our PHP datagrid to run in CodeIgniter.

Screenshot

phpGrid-Codeigniter-outcome

CodeIgniter and PHP Session

We recommended that you stick to a native PHP session in CI. By default, CI stores session information in a cookie, which is neither secure nor efficient.

You can also use the “out of proc” session by storing the session info in a database. This method is more sophisticated but inevitably more complex. It is not recommended unless you need to develop a large eCommerce website needs a session persistent shopping cart. phpGrid also uses a native PHP session to store secure data such as database connections and SQL data.

If you are using a native PHP session, make sure set “save_path” value to php.ini in an existing folder with write permissions.

http://php.net/manual/en/session.configuration.php#ini.session.save-path

Update: CodeIgniter 3+ Permission Update

Since CodeIginter 3, application and system folder has a new .htaccess for security the URL rewrite. It’s recommended to replace .htaccess with the following in application folder.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<IfModule mod_rewrite.c>
    RewriteEngine On
    #RewriteBase /your_project/

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

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    ErrorDocument 404 /index.php
</IfModule>