Social Icons

Pages

Friday, March 20, 2015

Configure CodeIgniter framework


Development Mode


Firstly we will look into configuring the development mode for the system. You can configure it in index.php file under Source Files. By default environment is set as development.
define('ENVIRONMENT', 'development');
When the system is implemented it can be changed into production environment. In production environment error reporting is disabled. Usually in production servers errors are disabled by default. But this configuration is worth to know.
define('ENVIRONMENT', 'production');

Mod rewriting

Mod rewriting is simply about redirecting the user to another file location without user realizing it. Advantage of it is that user would not be requested to remember hideous lengthy URLs. Instead user can use a shorter form which will be redirected to that lengthy URL. User may enter www.abc.com/welcome which really means www.abc.com/index.php/welcome. In mod rewriting, URL is rewritten at server level.
Mod rewriting can be done by defining mod rewriting rules in .htaccess file. These rules can be easily understood and written if you know regular expressions already. First a file should be created and named as .htaccess under Source Files. .htaccess file contains directives for Apache. Any directive in .htaccess file will be applied to the same directory it is saved and any sub directory. Thus these Mod Rewriting rules are written and saved in .htaccess file.
For this article series based on CodeIgniter I use below Mod Rewriting in my .htaccess file. If you wish to understand more you can read this article and many other articles around Internet.
In this code "RewriteBase" refers to the root folder of the project. You should change the RewriteBase according to your root folder name.

RewriteEngine On
RewriteBase /CodeIgniterTest

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
After that open application/config/config.php which is under Source Files. Change
$config['index_page'] = 'index.php';
$config['index_page'] = '';

To make this mod rewriting work, you have to turn on mod_rewrite module in Apache. To check the status of mod_rewrite module either you can follow given phpinfo() link in XAMPP or WAMP server. Otherwise write this simple coding <?php phpinfo(); ?> in a php file, save it in your apache root folder and view it through your web browser. If mod_rewrite module is enabled you will see
Otherwise simply enable it by editing httpd.conf file of Apache server. You just have to remove the "#" in front of the
LoadModule rewrite_module modules/mod_rewrite.so
save the file and restart the server. Your Mod Rewrite rules should be working now.

If you are working in a Linux environment you can simply enable the mod_rewrite module by using the following command

sudo a2enmod rewrite
Then restart the apache server. Your Mod Rewrite rules should be working now.

Set up CodeIgniter Framework Environment

CodeIgniter is one of the most powerful and popular php frameworks. You can view the popularity of php frameworks at phptrends.

What is CodeIgniter

CodeIgniter is an Application Development Framework - a toolkit - for people who build web sites using PHP. Its goal is to enable you to develop projects much faster than you could if you were writing code from scratch, by providing a rich set of libraries for commonly needed tasks, as well as a simple interface and logical structure to access these libraries. CodeIgniter lets you creatively focus on your project by minimizing the amount of code needed for a given task.




Tools I use for CodeIgniter

Install CodeIgniter

  • Firstly download CodeIgniter
  • Extract the downloaded zip file
  •  Copy extracted files into a folder created in htdocs folder of XAMPP or in www folder in case of WAMP
    ie : In my case I copied the files into
    XAMPP_installation_folder/htdocs/CodeIgniterTest/  
There are 3 folders and 2 files in the installation folder. Among them user_guide folder can be removed if you like to save some space as long as the content of that folder is accessible on line. System folder is the core of CodeIgniter. Application folder is the place where the real development is going to be happened.

Create CodeIgniter NetBeans Project

  • Create new project in NetBeans under PHP category and project type PHP Application with Existing Sources and click Next
  • Select CodeIgniter installation folder (folder which the CodeIgniter files are stored) as Sources folder
  • Give a relevant name to the project
  • Select any PHP version and click Finish

After a little bit tweaking and configuring You are Ready to Go!


 
Blogger Templates