phpGrid CodeIgniter 4 Integration

codeiginter 4 phpgrid

Introduction

Previously, we have covered phpGrid integration with CodeIgniter 3. CodeIgniter has since gone through some major iterations and many improvements. We will integrate phpGrid with CodeIgniter 4 and take advantages of its many new features and improvements.

Why Upgrade to CodeIgniter 4 from 3?

Created by EllisLab in 2006, now maintained by the British Columbia Institute of Technology, CodeIgniter is an open-source, lightweight PHP web application framework that follows the Model-View-Controller (MVC) architectural pattern. CI is known for its small footprint, high performance, and flexible features.

CodeIgniter 3 and CodeIgniter 4 have some key differences:

  • PHP version compatibility:
    • CodeIgniter 3 is compatible with PHP 5.2.4 or newer, while CodeIgniter 4 requires PHP 7.2 or newer.
  • Namespace support:
    • CodeIgniter 4 introduces support for namespaces, which makes it easier to organize and reuse code. CodeIgniter 3 does not have namespace support.
  • Modern MVC:
    • Both versions follow the MVC architectural pattern, but CodeIgniter 4 has a more modern implementation of the pattern with features like namespace support and PSR-4 autoloading.
  • Directory structure:
    • The directory structure of CodeIgniter 4 is different from CodeIgniter 3, with some new directories like app/Config and app/Views. The new directory structure provides a more organized and modular approach to application development.

Overall, while CodeIgniter 3 is still a solid framework for PHP web application development, CodeIgniter 4 offers several new features and improvements that make it a more modern and robust option for developers.

Install phpGrid

First, you need to download and install phpGrid on your server. You can download the latest version of phpGrid Lite. Extract the files into public folder.

codeigniter phpgrid folder structure

 

It is important to extract phpGrid into public folder, which is now recommended location for phpGrid.
X

phpGrid Configuration

Before using phpGrid, you need to specify database information in conf.php. conf.php is phpGrid configuration file in which we specify database connection parameters and path to the phpGrid. Please follow installation guide for configuration details.

Example 1: Insert phpGrid in CodeIgniter

For this tutorial, we add phpGrid in public\welcome_message.php, which is the default view in CodeIgniter 4. You can insert phpGrid in any other pages in public folder.

For free phpGrid Lite, namespace isn’t required

1
2
3
4
5
6
7
8
9
10
11
12
13
<section>

    <h1>phpGrid Demo</h1>

    <?php  
    require_once("../../phpGridx/conf.php");

    $dg = new C_DataGrid("SELECT * FROM orders", "orderNumber", "orders");
    $dg->enable_autowidth(true);
    $dg -> display();  
    ?>

</section>

For commercial version, be sure to add namespace

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<section>

    <h1>phpGrid Demo</h1>

    <?php  
    use phpCtrl\C_DataGrid;
    require_once("../../phpGridx/conf.php");

    $dg = new C_DataGrid("SELECT * FROM orders", "orderNumber", "orders");
    $dg->enable_autowidth(true);
    $dg -> display();  
    ?>

</section>

Example 2: Add phpGrid to Another Page

To add phpGrid to other pages, following CodeIgniter Route Rules in app/Config/Routes.php. For example:

Route

1
2
3
// file: app/Config/Routes.php
$routes->get('/', 'Home::index');
$routes->get('/grid', 'Home::grid');

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// file: app/Controller/home.php
namespace App\Controllers;

class Home extends BaseController
{
    public function index()
    {
        return view('welcome_message');
    }

    public function grid()
    {
        return view('grid');
    }
}

View

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Anther Grid</title>
</head>
<body>


<h1>Another grid</h1>

<?php  
use phpCtrl\C_DataGrid;
require_once("../../phpGridx/conf.php");

$dg = new C_DataGrid("SELECT * FROM orders", "orderNumber", "orders");
$dg->enable_autowidth(true);
$dg->enable_edit();
$dg -> display();  
?>

</body>
</html>