Build a CMS using Codeigniter part-1

Hi guys! We’re back on the track again. Now what we’re going to do here is create a simple Content Management System(CMS) using Codeigniter(CI). Now it’s a big tutorial that’s why i’ve divided the whole CMS into smaller part so that everyone can understand the whole thing.

For those who don’t know anything about this amazing framework please do visit the Codeigniter catagory in this blog. You can find the catagory in here.

Now lets start building a CMS using CI. As for the first part we are going to learn installing & configuring CI. Go to the download center of Codeigniter website & download a fresh copy of it. Now what we’re going to do is learn installing & configuring CI for the CMS.

Learn installing & configuring CI:

See the picture below of the file structure of CI. You’ve to put the fresh copy of your downloaded CI into the local server like WAMP or XAMPP or LAMP as you like. You can see the file structure of the CI  below picture.

file_struct

Now it’s time to change the index.php in the structure you see. First off all we should keep the current date in the region that’s why just copy these code in the beginning of index.php


date_default_timezone_set('Asia/Dhaka');

Now  then we want to have the root file path so just paste the following code before the line no 25 where it says “ define(‘ENVIRONMENT’, ‘development’); “.


$root = dirname(__FILE__);

var_dump($root);

define('ENVIRONMENT', 'development');

Now run the file in localhost & you’ll get a name of your root path like the image below.

file_path2

If you get the file path now it’s time to clean up the code as following:


switch (dirname(__FILE__)) {

case 'D:\wamp\www\ci':

define('ENVIRONMENT', 'development');

break;

 

default:

define('ENVIRONMENT', 'production');

break;

}

What we’ve done in upper side is that we may be run our project in different situation like in development mode or production mode. For that we dynamically just set the “ENVIRONMENT” variable into the switch case statement whether it is in development mode or production mode. For what purpose we should need this is the working on different database like we’ll get a different database in development rather than production both of them are not equal that’s why it’s important to set the  “ENVIRONMENT“.

Now for the example just see the code below you should also edit your original code with these code from the line no 41 to 57


if (defined('ENVIRONMENT'))

{

switch (ENVIRONMENT)

{

case 'development':

error_reporting(E_ALL | E_STRICT);

break;

case 'testing':

case 'production':

error_reporting(0);

break;

 

default:

exit('The application environment is not set correctly.');

}

}

Here what we can see is when the “ENVIRONMENT” is on development mode the error reporting  is E_ALL|E_STRICT & if it’s on production mode than the error reporting is 0.

Now go to the config.php you’ll find it on “application->config->config.php“. Set your base url to your current directory on localhost like the code below:


$config['base_url']          = 'http://localhost/ci';

Now for the database just create two folder in the “application/config” directory named development & production & cut the database.php file from the config folder to development & production folder. In the development folder open the database.php file & change the code below:


$db['default']['hostname'] = 'localhost';

$db['default']['username'] = 'root';

$db['default']['password'] = '';

$db['default']['database'] = 'cms';

$db['default']['dbdriver'] = 'mysql';

$db['default']['dbprefix'] = '';

$db['default']['pconnect'] = FALSE;

$db['default']['db_debug'] = TRUE;

$db['default']['cache_on'] = FALSE;

$db['default']['cachedir'] = '';

$db['default']['char_set'] = 'utf8';

$db['default']['dbcollat'] = 'utf8_general_ci';

$db['default']['swap_pre'] = '';

$db['default']['autoinit'] = TRUE;

$db['default']['stricton'] = TRUE;

Here we can see that we set pconnect from TRUE to FALSE that’s because we’re in development mode nothing else.

Now go to the production folder open database.php paste the code below:


$db['default']['hostname'] = 'localhost';

$db['default']['username'] = 'CHANGME';

$db['default']['password'] = 'CHANGME';

$db['default']['database'] = 'CHANGME';

$db['default']['dbdriver'] = 'mysql';

$db['default']['dbprefix'] = '';

$db['default']['pconnect'] = FALSE;

$db['default']['db_debug'] = FALSE;

$db['default']['cache_on'] = FALSE;

$db['default']['cachedir'] = '';

$db['default']['char_set'] = 'utf8';

$db['default']['dbcollat'] = 'utf8_general_ci';

$db['default']['swap_pre'] = '';

$db['default']['autoinit'] = TRUE;

$db['default']['stricton'] = FALSE;

As we can see up there the username is not set because we don’t know right now what to do with that it’ll come later & put the pconnect to FALSE & db_debug to FALSE as we don’t want any debugging in production mode.

Now we might want to do some custom configuration in here so just go to the config folder & create a file named cms_config.php & paste the code below:


$config['site_name'] = "My Awesome CMS";

In above code in the file we’ll start adding a site name so that we could do that dynamically. And for that go to the autoload.php in config folder and add the code below in 83 no line:


$autoload['config'] = array('cms_config');

As we are now in autoload.php file let’s do some other works also just like autoload the database we should change the code in line no 55 with the code below:


$autoload['libraries'] = array('database');

Let’s go back & check if anything broken if not then your customization is correct. That’s it for the installing & configuring part of the CI. Hope that we’ll come out with the next part as soon as possible so stay tuned for the next part of our CMS learning. The source code for today’s code is given below so feel free to download the source code & view or change it on your own.

An enthusiastic of work with over 2+ years of experience. Extensively experienced on wordpress, Codeigniter, Laravel.

##Visit my github page (https://github.com/ashawkat)
##Visit my blogging page where I spread out my knowledges around all other people (http://w3programmers.com/author/tanim)

If you want to hire me then you can contact me via email or skype.

Skype: ashawkat89
Email: [email protected]

Print Friendly
6 comments on “Build a CMS using Codeigniter part-1
    • there is no sql file as the db is creating using migration. read the full article thoroughly to understand how the db works !!

Leave a Reply

Your email address will not be published. Required fields are marked *

*