Codeigniter Installation

This is a full tutorial of a suggested way to install Grocery CRUD Enterprise to your already existing project with Codeigniter framework.

Notice: Grocery CRUD Enterprise is a framework agnostic library. That simply means that it doesn’t matter which framework you are using and it doesn’t matter the architecture you are using. This tutorial is taking some architecture decisions basically for you. If you need to have the full freedom of what structure to choose we are suggesting to see the full installation guide here.

First of all, the most common usage of Grocery CRUD Enteprise is an upgrade from the community edition. That simply means the project is built with Codeigniter framework and it is very possible to already have installed Grocery CRUD Community edition. So at the below steps we are trying to have an installation guide without causing any conflicts to the already existing community version (so you can use both if you like).

Step 1. From the email that you’ve received or from the user’s page (you will get instructions at the email of how to access user’s page) download the file that say’s “Without composer”. Once you’ve downloaded it, unzip it!
The folder structure that you will get is the following:

├── examples
│   ├── config.php
│   ├── database.php
│   ├── example.php
│   └── view.php
├── libraries
│   ├── autoload.php
│   ├── bin
│   ├── composer
│   ├── ...
│   ├── phpunit
│   ├── symfony
│   └── zendframework
└── public
    └── grocery-crud
        ├── css
        ├── fonts
        ├── images
        └── js

Step 2. rename the folder libraries to GroceryCrudEnterprise and move it to your project at the following path application/libraries/GroceryCrudEnterprise with this structure we simply follow the “Codeigniter way” of adding libraries.
Step 3. go to the folder public and copy the folder grocery-crud to your assets folder of your Codeigniter project. If you don’t have one, then it will be good to create one!

Step4. We did currently installed Grocery CRUD Enterprise in our project and we need to create our configuration files in order to make it work. Rename the configuration files from examples: config.php to gcrud-enterprise.php and copy it to application/config folder at your project. Don’t forget to add at your config file the path for the folder at the file gcrud-enterprise.php . In your case it will be:

'assets_folder' => base_url() . 'assets/grocery-crud/',
So far your Codeigniter project should look like this:
├── application
│   ├── cache
│   ├── config
│   │   ├── autoload.php
│   │   ├── ...
│   │   ├── gcrud-enterprise.php
│   │   ├── ...
│   │   └── user_agents.php
│   ├── controllers
│   ├── core
│   ├── errors
│   ├── helpers
│   ├── hooks
│   ├── language
│   ├── libraries
│   │   ├── GroceryCrudEnterprise
│   │   │   ├── autoload.php
│   │   │   ├── bin
│   │   │   ├── composer
│   │   │   ├── ...
│   │   │   ├── phpunit
│   │   │   ├── symfony
│   │   │   └── zendframework
│   │   ├── ...
│   │   ├── ...
│   │   └── index.html
│   ├── logs
│   ├── models
│   ├── private
│   ├── third_party
│   └── views
├── assets
│   ├── ...
│   ├── grocery-crud
│   │   ├── css
│   │   ├── fonts
│   │   ├── images
│   │   └── js
│   └── ...
└── system

and your file application/config/gcrud-enteprise.php will look something like this:

<?php
return [
    // So far 34 languages including: Afrikaans, Arabic, Bengali, Bulgarian, Catalan, Chinese, Czech, Danish,
    // Dutch, English, French, German, Greek, Hindi, Hungarian, Indonesian, Italian, Japanese, Korean,
    // Lithuanian, Mongolian, Norwegian, Persian, Polish, Portuguese, Brazilian Portuguese, Romanian,
    // Russian, Slovak, Spanish, Thai, Turkish, Ukrainian, Vietnamese
    'default_language'	=> 'English',

    // This is the assets folder where all the JavaScript, CSS, images and font files are located
    'assets_folder' => base_url() . 'assets/grocery-crud/'
,

    // There are only three choices: "uk-date" (dd/mm/yyyy), "us-date" (mm/dd/yyyy) or "sql-date" (yyyy-mm-dd)
    'date_format' => 'uk-date',

    // The default per page when a user firstly see a list page
    'default_per_page'	=> 10,

    // Having some options at the list paging. This is the default one that all the websites are using.
    // Make sure that the number of grocery_crud_default_per_page variable is included to this array.
    'paging_options' => ['10', '25', '50', '100'],

    // The environment is important so we can have specific configurations for specific environments
    'environment' => 'development',

    // This is basically in order to have a php cache. Be aware that in case you disble the php cache
    // things will get too slow
    'backend_cache' => false,

    'xss_clean' => false,

    // The character limiter at the datagrid columns, zero(0) value if you don't want any character
    // limitation to the column
    'column_character_limiter' => 50,

    // For more read http://framework.zend.com/manual/current/en/modules/zend.cache.storage.adapter.html
    // If you are not sure about how to use it, you can just change the ttl value (time to live) and
    // the file path of the cache
    'cache' => [
        'adapter' => [
            'name'    => 'filesystem',
            'options' => [
                'namespace' => 'gcrud',
                'ttl' => 3600 * 24 * 30 * 6,
                'cache_dir' => realpath(__DIR__ . '/Cache/')
            ],
        ],
        'plugins' => [
            'exception_handler' => ['throw_exceptions' => false],
        ],
    ],
];

Important Notice: The most common mistake of the installation of grocery CRUD Enteprise is when the assets_folder has a wrong path. If you did follow all the steps and you see that your webpage looks like that:

wrong-codeigniter-installation

then make sure that you have configured your application/config/config.php file at $config[‘base_url’]. For example if your project URL looks like this:
http://localhost/my-test-project/index.php then your base_url should look like this:
$config['base_url'] = 'http://localhost/my-test-project/';

Step5. Now you are ready basically to use grocery CRUD Enterprise. You only need some small modifications. The easiest way to create two private methods to your controller that it will look like this:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// Add those two lines at the beginning of your controller
include(APPPATH . 'libraries/GroceryCrudEnterprise/autoload.php');
use GroceryCrud\Core\GroceryCrud;

...

private function _getDbData() {
        $db = [];
        include(APPPATH . 'config/database.php');
        return [
            'adapter' => [
                'driver' => 'Pdo_Mysql',
                'host'     => $db['default']['hostname'],
                'database' => $db['default']['database'],
                'username' => $db['default']['username'],
                'password' => $db['default']['password'],
                'charset' => 'utf8'
            ]
        ];
    }
private function _getGroceryCrudEnterprise($bootstrap = true, $jquery = true) {
        $db = $this->_getDbData();
        $config = include(APPPATH . 'config/gcrud-enterprise.php');
        $groceryCrud = new GroceryCrud($config, $db);
        return $groceryCrud;
}

And now when you want to use groceryCRUD enterprise you will simply do this:

// Your function at your controller
public function demo_set_model()
{
    $crud = $this->_getGroceryCrudEnterprise();
    $crud->setTable('customers');
    $crud->setSubject('Customer', 'Customers');
    $crud->columns(['customerName','country','state','addressLine1']);
    $output = $crud->render();
    $this->_example_output($output);
}

function _example_output($output = null) {
    if (isset($output->isJSONResponse) && $output->isJSONResponse) {
                header('Content-Type: application/json; charset=utf-8');
                echo $output->output;
                exit;
    }

    $this->load->view('example.php',$output);    
}

The example.php is located at: application/views/example.php and it is the exact same view that we were using as an example at Community edition. More specifically the view example.php contains the below code:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="utf-8" />
 
<?php 
foreach($css_files as $file): ?>
 <link type="text/css" rel="stylesheet" href="<?php echo $file; ?>" />
 
<?php endforeach; ?>
<?php foreach($js_files as $file): ?>
 
 <script src="<?php echo $file; ?>"></script>
<?php endforeach; ?>
 
<style type='text/css'>
body
{
 font-family: Arial;
 font-size: 14px;
}
a {
 color: blue;
 text-decoration: none;
 font-size: 14px;
}
a:hover
{
 text-decoration: underline;
}
</style>
</head>
<body>
<!-- Beginning header -->
 <div>
 <a href='<?php echo site_url('examples/offices_management')?>'>Offices</a> | 
 <a href='<?php echo site_url('examples/employees_management')?>'>Employees</a> |
 <a href='<?php echo site_url('examples/customers_management')?>'>Customers</a> |
 <a href='<?php echo site_url('examples/orders_management')?>'>Orders</a> |
 <a href='<?php echo site_url('examples/products_management')?>'>Products</a> | 
 <a href='<?php echo site_url('examples/film_management')?>'>Films</a>
 
 </div>
<!-- End of header-->
 <div style='height:20px;'></div> 
 <div>
<?php echo $output; ?>
 
 </div>
<!-- Beginning footer -->
<div>Footer</div>
<!-- End of Footer -->
</body>
</html>

Latest Update: As I had many requests to basically have a full example with Codeigniter into one place, you can find everything that I’ve just describe above with the latest Codeigniter version and with the latest Grocery CRUD Enterprise at this link: https://www.grocerycrud.com/users/enterprise_latest_version by downloading with Codeigniter 5.5. Although it is not required, it is fully suggested to read this document first as it useful for you to understand better the file structure.