Upgrading to CodeIgniter 2.0

Posted: 2010-05-05
Category: CodeIgniter

As soon as I saw the tweet from EllisLab that CodeIgniter 2.0-dev was available on BitBucket I instantly converted PyroCMS to run on it. I got it all working in a few hours after LOTS of headbanging, mouse throwing and blaspheming. All the bugs I found along the way have since been fixed in the 2.0 branch by the Ellis developers or myself.

While this does not mean everything is running perfectly, at the time of writing all the worrying bugs are squashed so it should be safe for us to have a play.

Grab those new files

Download the new files from BitBucket.

What goes where

Slight change in the folder structure for some of you, the application/ folder and the system/ folder now sit next to each other.

So move your application folder up one level to sit next to system/ then delete that system/ folder. That has killed off CodeIgniter 1.7.2. Bye!

Now you need to copy the new downloaded system/ folder to the empty gap where the old one was.

Sadly that is not entirely it. You must also copy the following files from the new download to your installation:

  • index.php
  • application/config/foreign_chars.php
  • application/config/profiler.php

Noticed that your controllers and models are broked?

CodeIgniter Controllers and Models used to be defined extend like this:

class Blog_model extends Model

Now in CodeIgniter 2.0 you write:

class Blog_model extends CI_Model

Update 10/11/2010: Remember to do this for Controllers too. Instead of Foo extends Controller, you must now write Foo extends CI_Controller and all parent::Controller() calls must be changed to parent::__construct()!

Let's not argue about why, what, who, where, when, the fact is you have to do this. It can be achieved with some simple Find & Replace over your folder if your IDE/Text editor will allow it. For most applications (especially ones built primarilry during the 1.7.x era) you are nearly there now. Unless you use plugins or the old Validation class...

Oh shit, I use plugins!

Well not any more you don't. Plugins are dead and gone so you need to convert them to be Helpers. Move them to the application/helpers and rename from "whatever_pi.php" to "whatever_helper.php". Theoretically there is more to what should be a plugin or helper than the name of the file but TBH they will work like this so who gives a damn at this stage?

Make sure you have moved any array values in application/config/autoload.php from $autoload['plugins'] to $autoload['helpers'] or you will notice stuff break.

Validation is no more

You all had fair warning, it has been deprecated for several versions and now it is gone completely. You can:

A.) Move to using the new Form Validation class.

B.) Grab the old Validation.php class and put it in your application/libraries/ folder.

C.) Whine like a little girl on the forums.

Strange change to $config['permitted_uri_chars']

I had to change "a-z 0-9~%.:_-" to "a-z 0-9~%.:_-" in my application/config/config.php. Notice the backslash () before the hyphen (-).

Modular Separation / Modular Extensions

Out of the box Modular Separation won't work with CodeIgniter 2.0. Good thing I picked up the project and released a patch then huh? Grab the latest version in the 2.x branch from the Modular Sepearation Wiki.

Update 12/10/2010: Modular Separation has been merged back into Modular Extensions so they are now the same project. I wrote a post explaining the merge which details the differences and explains how to upgrade.

MY_Controller and other extended libs

A new /system/core/ folder has been created for some libraries considered more core than others such as Router, Loader and Controller. If you extend any library that has now moved to /system/core/ you must now place it in /application/core/.

CI_Language has a new name

The core library CI_Language has been renamed to CI_Lang. For 80% of apps this will mean nothing at all. If you refference the library or extend it then you will of course need to rename this.

Deprecated DB methods removed

After being deprecated since 1.6.x we finally see the back of the DB methods orwhere, orlike, groupby, orhaving, orderby and getwhere. They should now be or_where, or_like, group_by, or_having, order_by and get_where which is just a few more find/replace changes to run on your projects.

Disable query strings or expect weirdness

This didn't happen in previous CodeIgniter versions but in 2.0 if you enable $config['enable_query_strings'] it will now generate links like http://example.php/index.php?/controller, or http://example.php/?/controller. You need to set enable_query_strings to false which will disable GET support. You can re-enable that with a MY_Controller or hook that runs:

parse_str($_SERVER['QUERY_STRING'], $_GET);

Summary

That should be it! Up and running on CodeIgniter 2.0. Most of you won't need to have bothered with all the steps but they are worth keeping in mind for the future. If you spot anything I have missed please pester me in one of the usual ways.

Comments

Gravatar
Marco

2010-05-06

Hi Phil,
I believe that the "query string" problem is such a big problem similar to the broken model declaration.
Is it the same for controllers? Do we have to declare something like "class XXX extends CI_Controller" ?

Thanks
Marco

Gravatar
Mred

2010-05-06

I'm just waiting for CI2.0 to be released!!!
Great post for advancing waht we would need for upgrading!

Gravatar

2010-05-06

Marco: CI_Controller vrs Controller is still in discussion, but for now you still extend from Controller. I'll keep this article up to date with any more changes that happen before it goes live, so if you update and something breaks just check back.

The query string things does seem like a big problem but this is a very quick fix and to be honest query strings should not be that widely used in an app unless you are interacting with Facebook, oAuth, PayPal, etc. In which case if you have no MY_Controller you can just put this GET code in the Controller construct for each controller you need query strings enabled.

Gravatar
Udi

2010-05-06

Also make sure you move your Core extend libraries to application/core.

Gravatar

2010-05-06

Udi: Wow thanks, I really should have remembered that seeing as I am the one who made that change! I'll update the post.

Gravatar
John_betong

2010-05-07

Many thanks for the "Idiot's Guide" - it worked like a dream.

Just upgraded my site and delighted that the MEMORY USAGE has dropped from: 3,282,920 bytes down to: 2,546,672 bytes.

Gravatar
William

2010-05-07

Hi Phil,

Quiet alot huh?, but thanks for the advance upgrade checklist! really appreciate it.

Question, do you have an idea when ellislab is going to release to new 2.0 version? any positive dates?

Thanks in advance!

Gravatar
Marco

2010-05-07

Sure Phil,
"query strings should not be that widely used in an app" that's true,but when using pagination with filters,the first time I(like many others i think) pass data through POST then if validation is true you are going to be redirected to the same controller with GET parameters,avoiding the big problem of page refresh.
Then you'll move using pagination passing filter parameters like GET data.
I think this is a good way to do things,and losing query strings is going to be messy according to me.
Anyway i like the way you write stuff,and i believe that you are everytime a step ahead of everyone else.

Best regards
Marco

Gravatar

2010-05-10

Marco: If you are POSTing then redirecting to a query string URL, why not redirect with /controller/method/key1/value1/key2/value2?

You can pick up these values with $get = $this->uri->uri_to_assoc() and use that for pagination.

William: No idea. EllisLab don't do road-maps!

Gravatar
Tim

2010-05-10

Phil, in the docs it suggests you start your Models with:

class Model_name extends CI_Model {

function Model_name()
{
parent::CI_Model();
}


Is there any reason and/or need for this?

Gravatar
Kumar Chetan Sharma

2010-05-11

I have yet not explored 1.7 fully. Still its good to see that CI 2.0 will be PHP5 compatible. I read in forums that if you are confused about some thing email Phil, he knows everything about CI.

Gravatar

2010-05-11

Tim: You can try to extend your Models however you like, but they will fatal error unless you do it that way. ;-)

Kumar Chetan Sharma: Dear god no! Who said that? Please don't email me!! :'(

Gravatar
Joseph Taylor

2010-05-13

This is why I love Phil - he's like my angry, drunken CodeIgniter elf that fixes shoes while I sleep at night!

Gravatar
Santiago

2010-05-14

Hi Phil, I was looking the great CI stuff you've developed. I'm intending to use a couple of your libraries.
I've downloaded the tip version of CI2 and I'm getting a fatal error when trying to create a model:
Class 'CI_Model' not found. Any idea?
Best Regards.

Gravatar
Brennan Novak

2010-05-16

Howdi Phil,

Thanks for this tutorial. I was able to get my app mostly ported over 2.0 mentioning the application/core folder may be a step you want to include in the post as I couldn't figure out why my MY_Controller wasn't working- but then everything worked once I learned to drop it in there.

I'm still a bit hung up the query string thing especially in regards to Twitter oAuth. I'm using Elliot's Library. I tried adding your code:

parse_str($_SERVER['QUERY_STRING'], $_GET);

to the controller/method that Twitter returns a user to

domain.com/connections/twitter/?oauth_token=UXrtdKuLS2N6TCUJdtQAB

But I keep getting a 404 page. Am I missing something really simple yet important?


Gravatar
David

2010-05-18

I was just checking out the application packages, is this complete or is it half done?

Gravatar
Jeremy Schaffer

2010-06-06

I noticed that you did not post any special instruction for upgrading the Modular Separation other then to download the latest version from the Modular Separation Wiki. The problem is the Wiki (as well as the forums) post absolutely no information in reference to the v2.x version. I installed the v2.1 version into my application directory (same as I would have using the 1.12 release using codeigniter 1.7.2) per instruction from the readme file. The problem that I'm having is that it's not detecting anything from the modules folder. Inside my modules folder I have a subfolder called admin. Within the admin folder is a controllers folder with an admin_controller.php file that only has an index function to load my default.php view ($this->load->view('default');) which is in the admin/views folder. Is there something that I'm missing here? All I get when loading the page in my browser is the CI 404 message.

Gravatar
Jeremy Schaffer

2010-06-06

For those of you trying to get Modular Separation 2.01 working with CodeIgniter 2.0-dev, you need to extract the MY_Router and MY_Loader files into your application/core directory. Modular Separation will NOT WORK on CI 2.0-dev if you extract it to the application/library folder as per the wiki and readme instructions.

Thanks to Brennan Novak for pointing this out as it is not documented in the post, the Modular Separation Wiki, nor the included readme file.

Gravatar
Fabian

2010-06-15

Hi all, another thing to change, if you where using xss_clean ( from input helper ) you need to add security library to your autoload libraries, as the xss_clean resides now in security library and not in input library. hope this helps

Great job here Phil

Gravatar
Calebe Aires

2010-06-29

People say they we gonna pay for CI 2.0. Is it true?

Gravatar

2010-06-30

Calebe Aires: Did you read the April Fools post by any chance? It's currently on BitBucket for free to download... how would they charge for it? This was a joke, CI is free.

Gravatar
Prasanna

2010-07-03

Great job phil. Thanks.

Gravatar
Jelmer Schreuder

2010-07-04

Big thank you for the write-up!

Gravatar
Dan Horrigan

2010-07-08

Just an FYI. The reason you had to change the permitted_uri_chars is outlined in the 2.0 changelog:
Escaped the '-' in the default 'permitted_uri_chars' config item, to prevent errors if developers just try to add additional characters to the end of the default expression.

Gravatar
Paull

2010-08-31

hmmm; did they remove the 2.0 version from the bitbucket? Can't see it available for download anywhere...

Gravatar

2010-09-01

Download the "tip" from the Downloads tab. 2.0 is not a final release so it has not been tagged as a specific version just yet.

Gravatar
Martin

2010-09-17

Awesome, thanks for the tips.

Gravatar
Ildar Samit

2010-09-25

Fantastic! Simpler upgrades is just one reason to go with CI as opposed to a CMS. Thanks for the writeup man.

Gravatar
Suba

2010-10-16

Dear Phil,

Thanks. Nice article & comments. When I use "search" functionality, query string has such a problem.
Anyway am waiting CI 2.0.
Thanks.

Gravatar
Joe Taylor

2010-10-21

Phil, You should add that "$this->db->orderby()" now has to change to "$this->db->order_by()". This took me a little while to figure out as it returns a blank for queries containing orderby().

Gravatar
Joe Taylor

2010-10-22

Phil, You should mention that getwhere() is now get_where() also. My CMS is now running happily on CI 2!!! First client project getting it tonight!

Gravatar
Samit

2010-10-24

Thank you for the tutorial

Gravatar
Wayne

2010-11-09

i've been trying to learn codeIgniter for a couple months but keep running into problems.... i have almost given up and moved to something else....

the latest problem is php 5.3. will cogeIgniter 2.0 and the above fixes work on php 5.3?
if not.. i have to move on... i will try again in a few months...

Gravatar
Jonathon

2010-11-11

Just thought I'd point out another change: You do have to extend CI_Controller as opposed to Controller now (at least with the most recent version on bitbucket)

Gravatar
Randy

2010-11-14

Hey there. Wanted to say thanks so much Phil!! I spent the last 30 minutes trying to figure out why my custom controllers weren't working on a new project of mine, come to find this on google and find out i needed them in core not libraries.

Also quick note, as Jonathon noted above it seems you need to extend CI_Controller for now.

Gravatar
Sanket

2010-11-16

Would CI 2.0 better to use for building mobile websites??

Gravatar

2010-11-17

Sanket: It would make no difference what-so-ever to a mobile website. Take a look at my Template library for an easy way to build in mobile layout support.

Gravatar
Jedd

2010-11-20

Phil - excellent article, thanks. One thing that bit me - when fixing up extends for Models and Controllers, I'd had this kind of thing in the constructor, and it generated some nasty errors:

parent::Controller();

Not sure if this will bite many people - perhaps anyone with a hybrid php4 / php5 background - but for anyone so bitten, you just need to change that to:

parent::__construct();

Gravatar

2010-11-21

Jedd: Last week they renamed the Controller class to CI_Controller and replaced all constructs, so you're exactly right: you need to change any parent::Controller() references to parent::__construct();

Gravatar
Nurettin

2010-11-28

Thanks i read it. i am coding for 2.0

Gravatar
Bill

2010-12-03

as always very informative and I can always count on Phil S. bringing home the 'news' bacon :) thanks, Phil

Gravatar
Gabi

2010-12-08

Does anybody knows where can I get last CI 2?
The link on your post points to 1.7.3.
thanks Phil

Gravatar
Flux

2010-12-08

yes, the link seems to point to 1.7.3 version... where can we download 2.0dev?

Gravatar

2010-12-09

The BitBucket repo is a little messed up right now after they had to rush out a 1.7.3 security release.

This link should be a little more specific:

http://bitbucket.org/ellislab/codeigniter/get/default.zip

Gravatar
Flux

2010-12-12

great... thanks phil ;)

Gravatar
Ed

2011-01-08

Thanks for this idiot's guide Phil.

Worked a treat for this particular idiot. :)

Much appreciated.

Gravatar
Vamp

2011-01-30

thx a lot 4 U!
1.7.3 -> 2.0.0 success =)

Gravatar
Cole

2011-02-10

Phil,

Correct me if I'm wrong but after looking through the 2.0 code base in a little more detail, I added:

$config['allow_get_array'] = TRUE;

to the global config file, and it seems to have allowed the use of query strings. Seems more efficient than having CI clear out the $_GET array and then re-setting it in a hook or MY_Controller. Seems to be working so far. Will be testing more soon.

Gravatar

2011-02-10

Cole: Ha, yes I wrote this post in 2010-05-05. Since then Reactor has become the v2.0 feature and $config['allow_get_array'] is a feature that Dan Horrigan provided and I merged in. Yes it is easier, that's why we added it. :)

Gravatar
Stefano

2011-02-16

A little but -maybe- useful suggestion regarding the user guide: I did the update from 1.7.3 to 2.0.0 (reactor) yesterday and it was quite smooth for me, being an experienced CI developer.

Maybe it would be good for novices and not-so-experienced CI developers, that the UPGRADING section of the user guide should mention the fact that MY_Controller needs be moved to application/core and that some of the application/config files need to be update :)

Gravatar
Mitchel Haas

2011-02-26

Great article, Phil! Saved me loads of time upgrading to 2.0.

Gravatar
Dave Alex

2011-02-28

Hi!
Really nice informative article. I am new to CI and usng its version 2.0. I am facing same problem as Novak faced in twitter url.

When I am redirected from Twitter, there is no page displayed. Following is sample url:
domain.com/connections/twitter/?oauth_token=UXrtdKuLS2N6TCUJdtQAB

If I remove "?" from url, webpage begin to display. Can some one guide me what I am doing wrong and how it can be rectified.

Gravatar
Steevenz

2011-02-28

hi phil.. i curious is codeigniter 2.0 more lighter and more quicker than 1.7.3 version. I still used 1.7.3 version. By the way great post. Thanks for sharing

Gravatar
Alex

2011-03-02

Wow. Thanks so much for this. I am a programming noob and watching the tutorial on the codeigniter website wasn't going smoothly. I googled it, found your site and then I realized that the method of extending the Controllers had changed and now it is working. Thanks for the help!

Gravatar
Nomkhonwaan Computerscience

2011-04-29

Thank, this helpful us.

Gravatar
Sofa Zainuddin

2011-05-09

first time I try to use CI 2.0, I thing it was broken, thanks for your tutorial, it help me a lot sir..

Gravatar
Ghprod

2011-05-19

Hi Phil,

i use $route['404_override'] = 'errors/error_404';

its works well to handle normal 404 to custom one .. but when i call show_404() from controller, its still display the old one/built in 404 from CI.

what should i do to display custom 404 on controller?

thanks

Gravatar
Joey

2011-05-30

Nice article. Could do without the cussing though, kinda lowers it's quality.

Gravatar
Juan

2011-08-20

Hi I am working with CodeIgniter's 2.?, doctrine 1.2.4, I am studying and I can not integrate, the guides on the internet there are older versions, I do not use the 2.0 version of doctrine that does not support php 2 .*.

first thanks to your help I know that there is no longer hize plugins directory and change now
shows me another error
in doctrine_helper.php

/ home / system / / libraries / Model.php) [function.require-once]: failed to open stream:
No such file or directory

BASEPATH require_once '. / libraries / Model.php', -> error does not exist this directory version 2 .* n = (

Gravatar
Ryan Battles

2012-02-11

I really appreciate the time that you took to put this together. I have been following along with the "CodeIgniter from Scratch" video tutorials on net.tutsplus, and didn't so much as get past my first controller without errors (the tutorials were based on CI 1). I discovered in this post that I needed to extend CI_Controller instead of Controller, and now have it bookmarked if I come across any other issues! Thanks Phil.

Gravatar
David

2012-03-12

Hi Phil, Can you help me, sample pagination with CI 2 + Doctrine 2. I want to integrating on my code http://bkuprojects.us/ci-mysql/index.php/companygroup/posts/listcompany_group

Gravatar
John

2012-04-11

Hi Phil. Thanks so much for taking the time to write this article. It really helped me a lot! I was upgrading from CI 1.7.2 to 2.1 and although I tried to follow the official instructions, I seemed to miss something. Your article set that straight. Have a good one! John

Gravatar
Jordan 11 Concord

2012-06-26

Jordan 11 Concord

Posting comments after three months has been disabled.