- Read CodeIgniter4 User Guide
- Install CodeIgniter4
- Install ci3-to-4-upgrade-helper
- Config
- Hooks
- Database Migrations
- Database Seeding
- Controllers
- Models
- Libraries
- Views
- Helpers
- Common Functions
- Language files
- Writable Paths
CodeIgniter 4 is a rewrite of the framework. It is not an exaggeration to say that it is a new framework. Please read the User Guide carefully to get an overview.
See https://codeigniter4.github.io/CodeIgniter4/installation/index.html.
Note Use 4.3.1 or later. ci4-app-template can be used.
$ composer require kenjis/ci3-to-4-upgrade-helper:1.x-dev
- Migrate
application/config/config.php
toapp/Config/App.php
manually. You can set your own values like base_url with.env
file.
- Migrate
application/config/routes.php
toapp/Config/Routes.php
manually.
See https://codeigniter4.github.io/CodeIgniter4/incoming/routing.html#setting-your-own-routing-rules.
- CI4 does not have CI3's “Auto-load” feature, except helper autoloading.
- To autoload helpers, add your
$autoload['helper']
value inautoload.php
config to the property$helpers
inapp/Config/Autoload.php
. This is CI4's feature. - To autoload libraries, add your
autoload.php
config in the property$libraries
inapp/Controllers/BaseController.php
. This feature is provided by ci3-to-4-upgrade-helper.
Example:
--- a/app/Controllers/BaseController.php
+++ b/app/Controllers/BaseController.php
@@ -20,6 +20,17 @@ use Psr\Log\LoggerInterface;
class BaseController extends Controller
{
+ /**
+ * CI3's $autoload['libraries']
+ *
+ * @var array
+ */
+ protected $libraries = [
+ 'database',
+ 'session',
+ 'form_validation',
+ ];
+
/**
* An array of helpers to be loaded automatically upon
* class instantiation. These helpers will be available
- Convert custom config files to Config classes manually.
See https://codeigniter4.github.io/CodeIgniter4/general/configuration.html#creating-configuration-files.
- Replace the CI3 config name with the new config classname in your code.
Example:
$this->config->load('config_shop');
↓
$this->config->load('ConfigShop');
- Migrate remaining
application/config/*.php
toapp/Config/*.php
manually. You can set your own values like database password with.env
file.
- If you use
$this->config
in view files, you need to add the following code.
--- a/app/Config/View.php
+++ b/app/Config/View.php
@@ -3,9 +3,12 @@
namespace Config;
use CodeIgniter\Config\View as BaseView;
+use Kenjis\CI3Compatible\Traits\View\ThisConfigInView;
class View extends BaseView
{
+ use ThisConfigInView;
+
/**
* When false, the view method will clear the data between each
* call. This keeps your data safe and ensures there is no accidental
- Migrate
application/config/hooks.php
toapp/Config/Events.php
orapp/Config/Filters.php
manually. - Migrate
application/hooks/*
to Events or Controller Filters manually.
See https://codeigniter4.github.io/CodeIgniter4/extending/events.html or https://codeigniter4.github.io/CodeIgniter4/incoming/filters.html.
CI4 has built-in Database Migrations. ci3-to-4-upgrade-helper provides CI_Migration
class that extends CI4's Migration class.
- Copy migration files to
app/Database/Migrations/
. - Rename migration file names. See https://codeigniter4.github.io/CodeIgniter4/dbmgmt/migration.html#migration-file-names.
- Rename migration class names. Remove
Migration_
and change to CamelCase.
- Add
namespace App\Database\Migrations
. - Add
use Kenjis\CI3Compatible\Library\CI_Migration;
Example:
namespace App\Database\Migrations; // Add
use Kenjis\CI3Compatible\Library\CI_Migration; // Add
class CreateBbs extends CI_Migration
{
...
}
$this->db
in migration files is CI4's Database connection. If you want to use CI3 compatible $this->db
, replace it with $this->db_
which ci3-to-4-upgrade-helper provides.
The table migrations
in CI3 is incompatible. The definition of the table for CI4 MySQL is:
create table migrations
(
id bigint unsigned auto_increment primary key,
version varchar(255) not null,
class varchar(255) not null,
`group` varchar(255) not null,
namespace varchar(255) not null,
time int not null,
batch int(11) unsigned not null
)
charset = utf8;
If you want to use the database you have been using in CI3:
- You must drop (or rename) the table
migrations
. - Create a new table
migrations
for CI4. - Run the CI4 migration in the development environment or so, to create the migration data.
- Import the new migration data of CI4 into your production
migrations
table.
CI4 has built-in Database Seeding. ci3-to-4-upgrade-helper provides Seeder
class that is based on Seeder class in ci-phpunit-test and extends CI4's Seeder class.
- Copy seeder files to
app/Database/Seeds/
.
- Add
namespace App\Database\Seeds
. - Add
use Kenjis\CI3Compatible\Library\Seeder;
Example:
namespace App\Database\Seeds; // Add
use Kenjis\CI3Compatible\Library\Seeder; // Add
class ProductSeeder extends Seeder
{
...
}
$this->db
in seeder files is CI4's Database connection. If you want to use CI3 compatible $this->db
, replace it with $this->db_
which ci3-to-4-upgrade-helper provides.
$this->call()
in seeder files is the method of CI4's Seeder. If you want to use ci-phpunit-test compatible $this->call()
, replace it with $this->call_()
.
- Copy
application/controllers/*
toapp/Controllers/*
. - Rename the sub-folder names so that only the first letter is uppercase.
- Add
namespace App\Controllers;
. - Add
use Kenjis\CI3Compatible\Core\CI_Controller;
Example:
namespace App\Controllers; // Add
use Kenjis\CI3Compatible\Core\CI_Controller; // Add
class News extends CI_Controller
{
...
}
- CI4 does not have
_output()
method.
- Use Controller Filters. See https://codeigniter4.github.io/CodeIgniter4/incoming/filters.html.
- Copy
application/core/MY_Controller
toapp/Controllers/MY_Controller
. - Add Namespace and Use statement as other controllers.
- Copy
application/models/*
toapp/Models/*
. - Rename the sub-folder names so that only the first letter is uppercase.
- Add
namespace App\Models;
. - Add
use Kenjis\CI3Compatible\Core\CI_Model;
Example:
namespace App\Models; // Add
use Kenjis\CI3Compatible\Core\CI_Model; // Add
class News_model extends CI_Model
{
...
}
- Copy
application/libraries/*
toapp/Libraries/*
. - Rename the sub-folder names so that only the first letter is uppercase.
- Add
namespace App\Libraries;
.
Example:
namespace App\Libraries; // Add
class Seeder
{
...
}
- CI4 has View templates to display errors.
- The CI3 methods to customize error output are not supported.
- Create your own templates, and configure it in
app/Config/Validation.php
. - See https://codeigniter4.github.io/CodeIgniter4/libraries/validation.html#customizing-error-display.
error_string()
is also not supported. Useerror_array()
instead, if you really need it.
Example:
array_reduce($this->form_validation->error_array(), function ($carry, $item) {
$carry .= '<p>'.$item.'</p>';
return $carry;
});
- If you need more than one template for
list
orsingle
, please use CI4's native methods and pass your template name.
Examples:
<?= \Config\Services::validation()->listErrors('my_list') ?>
<?= \Config\Services::validation()->showError('username', 'my_single') ?>
- But you can create a validation rule with Closure.
- Or Create your own Rule classes, and configure it in
app/Config/Validation.php
. See https://codeigniter4.github.io/CodeIgniter4/libraries/validation.html#creating-custom-rules.
set_message()
- If you create a custom rule, use the second param
&$error
and set the error message. See https://codeigniter4.github.io/CodeIgniter4/libraries/validation.html#creating-custom-rules. - Otherwise, use
CI_Form_validation::setError(string $field, string $error)
that ci3-to-4-upgrade-helper provides.
- CI4's
Validation
never changes your data.
- If you set the rule
trim|required
, the value during validation is trimmed, but the value after validation is not trimmed. You must trim it by yourself.
- Setting validation rules providing a constructor param array is not supported. Please convert it to
Config\Validation
class. See https://codeigniter4.github.io/CodeIgniter4/libraries/validation.html#saving-sets-of-validation-rules-to-the-config-file. - CI4's format rules like
alpha_numeric
,valid_email
do not permit empty string. If you want to permit empty, add the rulepermit_empty
.
- CI4 has View templates for Pagination.
- The CI3 configurations to customize pagination links are not supported.
- Create your own templates, and configure it in
app/Config/Pager.php
. - Use
$pager->hasNextPage()
and$pager->getNextPage()
instead of$pager->hasNext()
$pager->getNext()
for the next page link. - A sample file is included in
src/CI3Compatible/Views/Pager/
. You could use it.
app/Config/Pager.php
public $templates = [
'default_full' => 'Kenjis\CI3Compatible\Views\Pager\default_full',
'default_simple' => 'CodeIgniter\Pager\Views\default_simple',
'default_head' => 'CodeIgniter\Pager\Views\default_head',
];
- CI4 uses the actual page number only. You can't use the starting index (offset) for the items which is the default in CI3. So if you use offset, you have to convert page to offset.
$offset = max(($page - 1), 0) * $per_page;
- CI4 gets the base URL automatically from the current URL. You can't set it by config.
- Copy
application/views/*
toapp/Views/*
.
- Copy
application/helper/*
toapp/Helpers/*
.
- CI4 changed
redirect()
API.
- See https://codeigniter4.github.io/CodeIgniter4/general/common_functions.html#redirect.
- Replace
redirect($uri)
withreturn redirect()->to($uri)
, when you can return Response object. - Replace it with
throw new \CodeIgniter\Router\Exceptions\RedirectException($uri)
, when you cannot return Response object. - Or you could use
redirect_()
that ci3-to-4-upgrade-helper provides after$this->load->helper('url')
.
- Up to version 4.3.1, CI4
base_url()
removed the trailing slash. But the bug was fixed in v4.3.2.
- CI4's
url_title()
does not support the second param's'dash'
and'underscore'
. Replace them with'-'
or'_'
.
- CI4 does not have
show_error()
show_error()
that ci3-to-4-upgrade-helper provides does not support the third argument$heading
.- If you want to show error page like CI3, you have to create error templates like
app/Views/errors/html/error_500.php
where500
is the status code. - In error templates, you can use
$message
which has the Exception message.
- Copy language folders (
application/language/*
) toapp/Language/
.
- CI4 has new
writable
directory and the constantWRITEPATH
. Adjust the paths when you write files.