kenjis / ci3-to-4-upgrade-helper
Help upgrade from CodeIgniter3 to CodeIgniter4
Installs: 4 506
Dependents: 0
Suggesters: 0
Security: 0
Stars: 65
Watchers: 7
Forks: 22
Open Issues: 5
Requires
- php: ^7.4 || ^8.0
- kenjis/ci3-like-captcha: ^1.0.1
- kenjis/phpunit-helper: ^1.1.2
Requires (Dev)
- codeigniter4/codeigniter4: ^4.3.1
- doctrine/coding-standard: ^9.0
- friendsofphp/php-cs-fixer: ^3.4
- mikey179/vfsstream: ^1.6.11
- nikic/php-parser: ^4.15
- phpmd/phpmd: ^2.9
- phpmetrics/phpmetrics: ^2.7
- phpstan/phpstan: ^1.3
- phpunit/phpunit: ^9.5.11
- squizlabs/php_codesniffer: ^3.5
- vimeo/psalm: ^5.0
README
This project helps you upgrade your CodeIgniter3 apps to CodeIgniter4.
- The goal is to reduce upgrade costs.
- It provides compatible interfaces for common use cases in CodeIgniter3 apps.
- It also provides compatible interfaces to test code using ci-phpunit-test.
- It does not aim to be 100% compatible.
- This project is under early development.
- This project is under early development.
- We welcome Pull Requests!
Requirements
- CodeIgniter 4.3.1 or later
- ci4-app-template can be used
- PHP 7.4 or later
Sample Code
- https://github.com/kenjis/ci3-to-4-news
- https://github.com/kenjis/ci3-to-4-news/tree/main/tests/app
- https://github.com/kenjis/ci4-online-games-store
- https://github.com/kenjis/ci4-qrcode
- (Japanese) https://github.com/kenjis/ci4-tettei-apps
If you use ci3-to-4-upgrade-helper, You can run the following code on CodeIgniter4.
app/Controllers/News.php
<?php namespace App\Controllers; use App\Models\News_model; use Kenjis\CI3Compatible\Core\CI_Controller; use Kenjis\CI3Compatible\Library\CI_Form_validation; /** * @property News_model $news_model * @property CI_Form_validation $form_validation */ class News extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('news_model'); $this->load->helper('url_helper'); } public function index() { $data['news'] = $this->news_model->get_news(); $data['title'] = 'News archive'; $this->load->view('templates/header', $data); $this->load->view('news/index', $data); $this->load->view('templates/footer'); } public function view($slug = null) { $data['news_item'] = $this->news_model->get_news($slug); if (empty($data['news_item'])) { show_404(); } $data['title'] = $data['news_item']['title']; $this->load->view('templates/header', $data); $this->load->view('news/view', $data); $this->load->view('templates/footer'); } public function create() { $this->load->helper('form'); $this->load->library('form_validation'); $data['title'] = 'Create a news item'; $this->form_validation->set_rules('title', 'Title', 'required'); $this->form_validation->set_rules('text', 'Text', 'required'); if ($this->form_validation->run() === false) { $this->load->view('templates/header', $data); $this->load->view('news/create'); $this->load->view('templates/footer'); } else { $this->news_model->set_news(); $this->load->view('news/success'); } } }
app/Models/News_model.php
<?php namespace App\Models; use Kenjis\CI3Compatible\Core\CI_Model; class News_model extends CI_Model { public function __construct() { parent::__construct(); $this->load->database(); } public function get_news($slug = false) { if ($slug === false) { $query = $this->db->get('news'); return $query->result_array(); } $query = $this->db->get_where('news', ['slug' => $slug]); return $query->row_array(); } public function set_news() { $this->load->helper('url'); $slug = url_title($this->input->post('title'), '-', true); $data = [ 'title' => $this->input->post('title'), 'slug' => $slug, 'text' => $this->input->post('text') ]; return $this->db->insert('news', $data); } }
app/Views/news/create.php
<h2><?php echo $title; ?></h2> <?php echo validation_errors(); ?> <?php echo form_open('news/create'); ?> <label for="title">Title</label> <input type="input" name="title" /><br /> <label for="text">Text</label> <textarea name="text"></textarea><br /> <input type="submit" name="submit" value="Create news item" /> </form>
How to Upgrade from CI3 to CI4
See How to Upgrade from CI3 to CI4.
If you have test code with ci-phpunit-test, see How to Upgrade Test Code from CI3 to CI4.
For Development
Installation
composer install
Available Commands
composer test // Run unit test
composer tests // Test and quality checks
composer cs-fix // Fix the coding style
composer sa // Run static analysys tools
composer run-script --list // List all commands