How I start a new CodeIgniter Project

Every other day, I develop new websites from scratch for various clients and sometimes for some of my own projects. Unless the project is really complex, I prefer to develop in CodeIgniter, a super-flexible (MVC) PHP framework which lets me develop super-quick !

It is thus wise to have a standard pattern to follow each time I start a new project. This is what I do -

1. First, the usuals -

2. Making http://www.myProject.com point to MyProjectFolder -

  • Edit the /private/etc/hosts file, add these lines -

# XYZ Project
127.0.0.1 www.MyProject.com
127.0.0.1 MyProject.com

  •  Edit the .htaccess file in Apache DocumentRoot, add these lines -

# for http://www.MyProject.com
RewriteCond %{HTTP_HOST} ^(www\.)?MyProject\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/MyProjectFolder
RewriteRule ^(.*)$ /MyProjectFolder/$1 [L]

2. Navigate into MyProject and,

  • Delete user-guide (un-necessary clutter since it is available online anyway)
  • Add readme.md (short project description)

3.  Create a folder named static/ within MyProject. This will include all the stylesheets, images, fonts, javascripts etc.

4. Create a .htaccess file within MyProjectFolder/

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt|static)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

This will enable serving static content and also enable clean URLs.

(www.MyProject.com/welcome instead of http://www.MyProject.com/index.php/welcome)

5. Create 3 folders within application/config/ -

  • development
  • testing
  • production

Make sure that ‘ENVIRONMENT’ is set to ‘development’ in MyProjectFolder/index.php

Copy database.php and config.php inside these folders and delete the original one. Then, inside the development folder, setup the development environment -

  •  Create a mysql database for the project and make changes in config/development/databases.php accordingly
  • Create a folder db_scripts inside config folder and keep a record of all sql queries (create table scripts) executed on the database inside this folder.
  • Set base_url, encryption key, index_page etc. in config/development/config.php and

6. Edit config/routes.php file, set default controller to home (or whatever you wish)

7. Setup logging – Use third party library ci_log4php (This integrates Log4PHP with CodeIgniter)

  • Refer my blogpost here for instructions on how to set it up

9. Setup Email (Optional) - Use the default Email helper for internal error-mails, notifications etc. and setup Sendgrid for emails sent to users. (I ll probably cover this in detail in another post)

  • UPDATE : Here is the post.

10. Setup Auth – Use a third party library tank_auth [Download]

  • Just follow the above link to download tank_auth and simply replace or add all the files and folders it contains.
  • The config file is well documented and its easy to customize with a lot of options to set up a logging system exactly as you need

11. Push code to git

  • Create a new repo on git and get the url git://github.com/youruser/somename.git
  • Create the .gitignore file and select the codeigniter template
  • Locally, at the root directory of your source, git init
  • git remote add origin <URL From Step 1>
  • git pull origin master
  • git add .
  • git commit -m “First commit”
  • git push origin master

Typically the entire process should take around 30-40 minutes :)

About these ads

16 comments

  1. Once you’ve done the steps once, you could save time by cloning the base repo for each new project.

  2. One thing though, it’s “CodeIgniter”, no hyphen. The community is actually picky about that, believe it or not. :)

  3. @Casey – True, that would save steps 5 to 10. However, you wont get the latest version of CodeIgniter that way. Gives a good feeling when one downloads the latest one each time :D
    @Jack : Point noted, Thank you :)

  4. @Mayank Going on @Casey’s comment, you can link the CodeIgniter repo using the git submodule feature http://git-scm.com/docs/git-submodule so your base project That way you can either have the latest, or a specific branch.

  5. @Rushi : Thanks, this looks interesting, will give it a shot !

  6. Only the system folder changes, so you could just pull that from the git repo each time

    1. whoops! missed Rushi’s comment

  7. Reblogged this on Sam's PHP How-To and commented:
    This is a bit over my head but could come in handy!

  8. [...] promised in my previous post, here are the steps to set up email on CI using SendGrid [...]

  9. Muhammad Zubair · · Reply

    although CI is a good MVC framework of php
    but there is a framework more powerful than CI called Yii (Yes it is); the more reliable and secure

  10. I think that is among the so much important information
    for me. And i’m satisfied studying your article. However want to remark on some basic issues, The website taste is ideal, the articles is actually nice : D. Just right job, cheers

  11. It’s an remarkable article in favor of all the online people; they will get benefit from it I am sure.

  12. We are a gaggle of volunteers and starting a new scheme in our community.

    Your website provided us with useful information to work on.
    You’ve done an impressive task and our entire neighborhood might be thankful to you.

  13. Howdy are using WordPress for your site platform? I’m new to the blog world but I’m trying to get started and create my
    own. Do you need any html coding expertise to make your own blog?
    Any help would be greatly appreciated!

  14. You’ve got great information on this site.

  15. Again, movie-intensive work or gambling involves more than normal amounts of Memory probably 2-4GB (gigabytes).
    At the end of time, you have to consider a phone and
    choose what’s best for you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: