Convert from CodeIgniter 1.7.3 to 2.0+

Hi and thanks for reading.

I'll go right away: I have a website project that I created using CodeIgniter 1.7.3, which I really liked, but I was considering upgrading to CI 2.0 +.

I tried a direct copy, just moved the folders for controllers, models, and views to CI 2.0, but when I tried to view my pages, I got a 500 server error.

After some investigation, I found that all your controllers should now use "CI_Controller" as their parent class. I also noticed that if you want to include a constructor in your controller class, it should use the syntax "function __construct ()" as its name and parent class. It seems that CI 2.0+ no longer supports the use of a constructor with the same name as the class name, for example. "class Blogs extends CI_controller {function Blogs () {parent :: __ construct ();}}" is no longer supported? I read the CI change log, but all I see are bug fixes and new features, nothing about compatibility issues with older versions of CI? Does anyone else know of any other secret little pitfalls?

Thanks,

N

+4
source share
2 answers

CI 2.x removed all compatibility with PHP4, and also updated a number of standards that will comply with PHP 5.3 in the future. One of them is the constructor problem you encountered. Starting with PHP 5.3, the ClassName() function is no longer a constructor for a class, it is just another function. You must explicitly declare the __construct function to perform any tasks that you must complete when creating a new instance of the class. Given this, you should see that it no longer makes sense to call parent::ClassName() in your child constructor, since this function will no longer be the parent constructor.

Another error that I recently encountered is how the $_GET array is now processed. In versions 1.x, you can use query strings to convey additional information and still use URI segments to route to controllers and functions. This is especially useful for AJAX calls, where you cannot always find out all the parameters sent to and from the server in a specific request. In versions 2.x, the config.php file contains the new option $config['allow_get_array'] . This value must be set to TRUE if you want to use query strings, otherwise the input class will clear the $_GET array as part of the CI initialization procedure for each request.

Something that is not an error, but may prove useful, is the new parameters in config / autoload.php, which allow you to add new application directories to the project. If you work with several different projects with CI and want to keep any useful libraries that you write in one place, now you can add this location to $autoload['packages'] . CI expects that any path in this array will contain the subdirectories "controllers", "models", "libraries" and "helpers". He will not complain if you do not have these directories, but at least you will need them for everything that you are going to load, that is, libraries will live in / library, as in the main folder of the application.

+7
source

Have you read the official upgrade guide from 1.7.x to 2.x ?

so shorter

  • Update models and controllers to the extension CI_Model and CI_Controller
  • Update parent constructor calls

    Wow class extends CI_Controller {

      function __construct() { parent::__construct(); //your stuff } function index() { // just for example } } 
+6
source

All Articles