kenjis / ci3-to-4-upgrade-helper
帮助将 CodeIgniter3 升级到 CodeIgniter4
v0.8.0
2023-09-01 04:40 UTC
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
此项目帮助您将 CodeIgniter3 应用程序升级到 CodeIgniter4。
- 目标是降低升级成本。
- 它为 CodeIgniter3 应用程序中的常见用例提供兼容的接口。
- 它还提供了兼容的接口来测试使用 ci-phpunit-test 的代码。
- 它并不旨在实现100%兼容。
- 此项目处于早期开发阶段。
- 此项目处于早期开发阶段。
- 我们欢迎拉取请求!
要求
- CodeIgniter 4.3.1 或更高版本
- ci4-app-template 可以使用
- PHP 7.4 或更高版本
示例代码
- 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
- (日语) https://github.com/kenjis/ci4-tettei-apps
如果您使用 ci3-to-4-upgrade-helper,则可以在 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>
如何从 CI3 升级到 CI4
如果您有 ci-phpunit-test 的测试代码,请参阅 如何从 CI3 升级测试代码到 CI4。
开发
安装
composer install
可用命令
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