user_guide_src/source/installation/upgrade_4xx.rst
######################### Upgrading from 3.x to 4.x #########################
CodeIgniter 4 is a rewrite of the framework and is not backwards compatible. It is more appropriate to think of converting your app, rather than upgrading it. Once you have done that, upgrading from one version of CodeIgniter 4 to the next will be straightforward.
The "lean, mean and simple" philosophy has been retained, but the implementation has a lot of differences, compared to CodeIgniter 3.
There is no 12-step checklist for upgrading. Instead, start with a copy
of CodeIgniter 4 in a new project folder,
:doc:however you wish to install and use it </installation/index>,
and then convert and integrate your app components.
We'll try to point out the most important considerations here.
To upgrade your project, we figured out two major tasks you have to work on. First of all, there are some general adjustments which are significant to every project and have to be handled. The second one are the libraries in which CodeIgniter is built up and contain some of the most important functions. These libraries operate separately from each other, so you have to look at them one by one.
Do read the user guide before embarking on a project conversion!
.. contents:: :local: :depth: 2
General Adjustments
ready-to-run zip or tarball <../installation/installing_manual>.Composer <../installation/installing_composer>... important:: index.php is no longer in the root of the project! It has been moved inside the public folder, for better security and separation of components.
This means that you should configure your web server to "point" to your project's
**public** folder, and not to the project root.
If you would use Shared Hosting, see :ref:`deployment-to-shared-hosting-services`.
defined('BASEPATH') OR exit('No direct script access allowed'); line is not necessary
because files outside the public folder are not accessible in the standard configuration.
And CI4 no longer defines the constant BASEPATH, so remove the line in all files.define all routes <defined-route-routing> by default.auto-routing-legacy.auto-routing-improved.namespace App\Models; along with use CodeIgniter\Model; right after the opening php tag.
The last step is to replace extends CI_Model with extends Model.$this->load->view('directory_name/file_name') to
echo view('directory_name/file_name');.namespace App\Controllers; after the opening php tag.
Lastly, replace extends CI_Controller with extends BaseController... toctree:: :titlesonly:
upgrade_models
upgrade_views
upgrade_controllers
Input <http://codeigniter.com/userguide3/libraries/input.html>_
corresponds to CI4's :doc:IncomingRequest </incoming/incomingrequest>.Output <http://codeigniter.com/userguide3/libraries/output.html>_
corresponds to CI4's :doc:Responses </outgoing/response>.../concepts/services.Autoloader <../concepts/autoloader> automatically handles PSR-4 style class locating,
within the App (app folder) and CodeIgniter (i.e., system folder) top level
namespaces; with Composer autoloading support.../concepts/factories that can load a class and share the
instance like $this->load in CI3.$this->load->library('x'); you can now use
$this->x = new \App\Libraries\X();, following namespaced conventions for
your component. Alternatively, you can use :doc:../concepts/factories:
$this->x = \CodeIgniter\Config\Factories::libraries('X');.Helpers <../general/helpers> are pretty much the same as before, though some have been simplified.CAPTCHA Helper <https://www.codeigniter.com/userguide3/helpers/captcha_helper.html>,
Email Helper <https://www.codeigniter.com/userguide3/helpers/email_helper.html>.
Path Helper <https://www.codeigniter.com/userguide3/helpers/path_helper.html>.
and Smiley Helper <https://www.codeigniter.com/userguide3/helpers/smiley_helper.html>.Download Helper <https://www.codeigniter.com/userguide3/helpers/download_helper.html>_
in CI3 was removed. You need to use Response object where you are using force_download().
See :ref:force-file-download.Language Helper <https://www.codeigniter.com/userguide3/helpers/language_helper.html>_
in CI3 was removed. But lang() is always available in CI4. See :php:func:lang().Typography Helper <https://www.codeigniter.com/userguide3/helpers/typography_helper.html>_
in CI3 wll be :doc:Typography Library <../libraries/typography> in CI4.Directory Helper <https://www.codeigniter.com/userguide3/helpers/directory_helper.html>_
and File Helper <https://www.codeigniter.com/userguide3/helpers/file_helper.html>_ in CI3
will be :doc:../helpers/filesystem_helper in CI4.String Helper <https://www.codeigniter.com/userguide3/helpers/string_helper.html>_ functions
in CI3 are included in :doc:../helpers/text_helper in CI4.redirect() is completely changed from CI3's.
redirect() Documentation CodeIgniter 3.x <https://codeigniter.com/userguide3/helpers/url_helper.html#redirect>_redirect() Documentation CodeIgniter 4.x <../general/common_functions.html#redirect>_redirect() returns a RedirectResponse instance instead of
redirecting and terminating script execution. You must return it from Controllers
or Controller Filters.redirect() are not automatically
carried over to a RedirectResponse. You need to call withCookies()
or withHeaders() manually if you want to send them.redirect('login/form') to return redirect()->to('login/form').Hooks <https://www.codeigniter.com/userguide3/general/hooks.html>_ have been
replaced by :doc:../extending/events.$hook['post_controller_constructor'] you now use
Events::on('post_controller_constructor', ['MyClass', 'MyFunction']);, with the namespace CodeIgniter\Events\Events;.pre_controller and post_controller have been removed.
Use :doc:../incoming/filters instead.display_override and cache_override have been removed.
Because the base methods have been removed.post_system has moved just before sending the final rendered
page.The behavior in CI4 has been slightly changed.
In CI3 the behavior is set in the index.php file:
error_reporting() are logged (but
depending on the log_threshold setting, they may not be written to
the log file).E_ERROR | E_PARSE | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR
stopped framework processing, regardless of the error level set in
error_reporting().In CI4, the behavior is set in the app/Config/Boot/{environment}.php file:
error_reporting() are logged (but
depending on the Config\Logger::$threshold setting, they may not be
written to the log file).error_reporting() will stop the
framework processing.MY_... framework
component extensions or replacements.MY_X classes inside your libraries folder
to extend or replace CI4 pieces.../extending/core_classes for details.Upgrading Libraries
$this->load->library('x'); you can now use
$this->x = new \App\Libraries\X();, following namespaced conventions for
your component. Alternatively, you can use :doc:../concepts/factories:
$this->x = \CodeIgniter\Config\Factories::libraries('X');.Calendaring <http://codeigniter.com/userguide3/libraries/calendar.html>,
FTP <http://codeigniter.com/userguide3/libraries/ftp.html>,
Javascript <http://codeigniter.com/userguide3/libraries/javascript.html>,
Shopping Cart <http://codeigniter.com/userguide3/libraries/cart.html>,
Trackback <http://codeigniter.com/userguide3/libraries/trackback.html>,
XML-RPC /-Server <http://codeigniter.com/userguide3/libraries/xmlrpc.html>,
and Zip Encoding <http://codeigniter.com/userguide3/libraries/zip.html>_... toctree:: :titlesonly:
upgrade_configuration
upgrade_database
upgrade_emails
upgrade_encryption
upgrade_file_upload
upgrade_html_tables
upgrade_images
upgrade_localization
upgrade_migrations
upgrade_responses
upgrade_pagination
upgrade_routing
upgrade_security
upgrade_sessions
upgrade_validations
upgrade_view_parser