Monthly Archives: December 2011

You are browsing the site archives by month.

Setting up a Fresh Install of CodeIgniter 2.1

My preference for an MVC is CodeIgniter. It’s a nice light-weight MVC PHP framework with a great community and excellent documentation. This blog post is going to be a quick how-to regarding the setup of CodeIgniter from a fresh install.

Step 1. Get CodeIgniter 2.1

Go ahead and grab the CodeIgniter 2.1 framework from the CI website: http://codeigniter.com/downloads/

Once you have the framework unzip it into your web root. If you are running a LAMP server then your web root is likely /var/www. I use WAMPServer as a test instance on my Windows 7 machine. The web root for the default WAMP install is C:\wamp\www.

You don’t actually need all of the files in the zip folder, just the application folder, system folder, and index.php file.

Step 2. Remove the index.php from the URI with .htaccess and mod_rewrite

All traffic on your CodeIgniter site will go through the index.php file that you copied over into your web root. This leaves an ugly little artifact behind in your website’s URI. If left unchanged, your URI will be set up like this: {domain}/index.php/{segments}. I always like to remove this index.php from the URI. This isn’t really necessary, but if you want your CodeIgniter powered site to look professional, I would suggest doing this.

The first thing you’ll need to do in order to bypass the index.php segment of your URI is to make sure that the mod_rewrite module for Apache is enabled. I believe most LAMP installs have this set up by default. All of the LAMP servers that I have paid hosting on have had this module set up. If you would like to set this up in WAMP right click the WAMP icon in your system tray, hover over Apache from the resulting context menu, Apache Modules after that, and click the rewrite_module option to put a check mark beside it. WAMP will restart and mod_rewrite will be enabled.

Next create a file named .htaccess in your web root. Put the following code in this file and save it. You can read more about this file from the CI wiki page where the code was found: http://codeigniter.com/wiki/mod_rewrite.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
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.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php
</IfModule> 

Open your application/config/config.php file and change:

$config['index_page'] = 'index.php';

to

$config['index_page'] = '';

Step 3. Setup the config.php, database.php and routes.php Files

In the application/config/config.php file set $config['base_url'] to reflect your domain (e.g. www.someurl.com or localhost).

There are lots of other settings that you may want to set regarding sessions, encryption, xss filtering, logging, csrf, cookies, and more. I encourage you to read through this thoroughly and set up the values to best suit your site.

If you are going to connect to a database you will need to set up the application/config/database.php file. The array values are pretty self explanatory. Just read through the comment block at the head of the file and you will easily be able to determine what information is needed, where.

In the application/config/routes.php file you will see an array value that looks like this:

$route['default_controller'] = "welcome";

I hate using welcome.php as my default controller. Generally, I would suggest that you change this value to index. However, index.php won’t work in CodeIgniter. Because of the way CodeIgniter is built, if you do use index.php and you have an index( ) function (which is the default function for each controller) the index( ) function will get called twice. So, while you don’t have to change this value I personally prefer home to welcome because it makes more sense.

Viola! You’ve Done It!

That’s it. You have a working CodeIgniter framework. Test it a bit to make sure that everything is working as it should.

Of course you will likely want to do much more customization of your framework if you’re running more than a test site. These changes will become apparent as your site begins to flesh out and the scope of your project becomes clearer.

Good luck!

facebooktwittergoogle_plusredditpinterestlinkedinmail

Why You Should Implement a PHP Framework

If you are coding PHP, you should definitely consider implementing a framework. Why? Well, let’s start at the beginning.

What is a framework?

A framework is a family of code that provides a basic structure, a common environment in which code can be produced in a standardized and efficient manner. Effectively, a framework provides a backbone of common functionality for developers.

But a framework is more than a group of reusable library files. In order to be considered a framework, the structure has to display 3 characteristics: 1) Must provide inversion of Control; 2) Must provide integrated functionality based on patterns fitted to an application’s domain; 3) Must be a semi-complete program [1.]

1) Must provide inversion of control [1.] – Inversion of control is kind of like it sounds. Generally an application contains its own logic that controls the flow of it. Inversion of control is a concept has an application act contrarily to this by instead of containing its driving logic it must accept its logic from calling subroutines. To think of it from a developers perspective, when you create an object that calls and directs other, external objects [2.], such as with an API.

2) Must provide integrated functionality based on patterns fitted to an application’s domain [1.] – Basically, what this means is that a framework must provide a layer of abstraction of the application’s domain [1.] (e.g. a PHP environment); a basic structure to support the application’s environment.

3) Must be a semi-complete program [1.] – This is pretty self-explanatory.

What are the benefits?

These are just a few of the wonderful benefits that I found in my studies and/or my own experience of frameworks (in no particular order):

1. Code reuse [1.] – What is not to love about code reuse? Hey, any code that I don’t have to write twice is good code in my opinion. With frameworks, the concept of code reuse is built in to the design. The ability to call basic framework functionality over and over again from any point in your code is the entire point. You’ll never have to re-write that logging functionality again!

2. Abstraction – How much easier is it to understand the functionality of a piece code if it is pulled away from the rest the surrounding code, and the context generally? That is basically what abstraction is and what a framework does for you. Do you want to examine some validation functionality? Pull up that validation library.

3. Modularity – Nix the monolithic code [1.]! Why carve your house out of one piece of wood? Let’s take a second and appreciate Mr. Whitney’s contribution to programming: interchangeable parts! By breaking up an application into self-contained modules we gain reuse, standardization, abstraction, and extensibility.

4. Scalability and extensibility - Today building with the understanding that your application must be able to grow in scope and change in time is a given. Frameworks accomplish both of these things. Frameworks add extensibility by allowing the implementation of plugins and through modularity (making it easy to integrate new functionality quickly). They add scalability via modularity (allowing users to update components one at a time) and through framework code base updates.

5. Super-quick turnaround – How much faster would you code if you didn’t have to rewrite all of those security functions for the umpteenth time? Now imagine how much faster projects would get done if most of the common functionality was built for you. That is exactly what a framework offers, allowing teams to turn out code and ever increasing speeds.

6. Organization! - Having a touch of OCD, this feature means a lot to me. Frameworks provide a standardized way to organize the 10 million files that make up your application.

7. Model View Controller (MVC) – Most contemporary frameworks are built around the concept of MVC. This is a design pattern that separates the data (model) from the UI (view) from the driving logic (controller). This is a widely adopted design pattern and considered best practice.

8. Built-in security – Not so sure about your security routines? No problem. Most frameworks offer their own security suites with primers on proper implementation.

9. Community buy-in/support – Frameworks are widely accepted in the coding community. They are also usually well supported by active communities which provide user docs, forums, and wikis to help get you and keep you on track.

10. Because it’s marketable – Go to your favorite employment website and search for PHP jobs. How many of them require or would like someone with skills in CodeIgniter, CakePHP, or any one of a number of other frameworks out there. The number is growing everyday as employers finally start becoming more technologically sophisticated. In a competitive market like the one we find ourselves in today, having mastered a framework might be what makes you stand out.

References

  1. Schmidt, D. C., Gokhale, A., & Natarajan, B. (n.d.). Frameworks: Why they are important and how to apply them effectively. Retrieved from http://www.dre.vanderbilt.edu/~schmidt/PDF/Queue-04.pdf
  2. Eini, O. (2006, November). Inversion of control and dependency injection: Working with windsor container. Retrieved from http://msdn.microsoft.com/en-us/library/aa973811.aspx
facebooktwittergoogle_plusredditpinterestlinkedinmail