Phil Sturgeon

Web developer, kayaker, outdoors madman and part-time alcoholic.


Upgrading to CodeIgniter 2.0

Posted CodeIgniter at May 05, 2010

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 his 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:

Noticed that your models are broked?

CodeIgniter Models used to use the following class definitions:

class Blog_model extends Model

Now in CodeIgniter 2.0 you write:

class Blog_model extends CI_Model

Let's not argue about why, what, who, where, when as this may still change. For now, 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. 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

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.

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

User comments
  • Gravatar http://philsturgeon.co.uk/

    Sep 01, 2010

    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 Paull

    Aug 31, 2010

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

  • Gravatar Dan Horrigan

    Jul 08, 2010

    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 Jelmer Schreuder

    Jul 04, 2010

    Big thank you for the write-up!

  • Gravatar Prasanna

    Jul 03, 2010

    Great job phil. Thanks.

  • Gravatar http://philsturgeon.co.uk/

    Jun 30, 2010

    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 Calebe Aires

    Jun 29, 2010

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

  • Gravatar Fabian

    Jun 15, 2010

    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 Jeremy Schaffer

    Jun 06, 2010

    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 Jeremy Schaffer

    Jun 06, 2010

    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 David

    May 18, 2010

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

  • Gravatar Brennan Novak

    May 16, 2010

    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 Santiago

    May 14, 2010

    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 Joseph Taylor

    May 13, 2010

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

  • Gravatar Phil Sturgeon

    May 11, 2010

    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 Kumar Chetan Sharma

    May 11, 2010

    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 Tim

    May 10, 2010

    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 Phil Sturgeon

    May 10, 2010

    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 Marco

    May 07, 2010

    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 William

    May 07, 2010

    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 John_betong

    May 07, 2010

    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 Phil Sturgeon

    May 06, 2010

    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 Udi

    May 06, 2010

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

  • Gravatar Phil Sturgeon

    May 06, 2010

    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 Mred

    May 06, 2010

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

  • Gravatar Marco

    May 06, 2010

    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

Post a comment