定制 yllumi/ci4-pages 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yllumi/ci4-pages

Composer 安装命令:

composer require yllumi/ci4-pages

包简介

Page based router for CodeIgniter 4

README 文档

README

tests build

ci4-pages is a package for CodeIgniter 4 that provides a page-based routing mechanism. This package simplifies routing management with a file-based and folder-structured approach, making it suitable for projects requiring dynamic routing. Think of it as similar to Next.js or Laravel Folio but maintaining the coding style unique to CodeIgniter 4.

Installation

Install this package via Composer with the following command:

composer require yllumi/ci4-pages

Configuration

Register the pageview_helper in app/Controllers/BaseController.php

protected $helpers = ['Yllumi\Ci4Pages\Helpers\pageview'];

Usage

Create a folder named app/Pages/. This folder will be used to store all your page controller files.

app/
├── Pages/
│   ├── home/
│   │   ├── PageController.php
│   │   ├── index.php
│   ├── profile/
│   │   ├── PageController.php
│   │   ├── index.php
│   │   ├── achievement/
│   │   │   ├── PageController.php
│   │   │   ├── index.php

Each folder inside app/Pages/ will represent a page route. For example, the folder app/Pages/home will be accessible at domain.com/home. Similarly, the folder app/Pages/profile/achievement/ will be accessible at domain.com/profile/achievement/.

There is one mandatory file that must exist in the pages folder, namely PageController.php, which will handle page requests. Besides that, you can create other .php files for views and so on.

Let's look at an example code below:

app/Pages/home/PageController.php

<?php

namespace App\Pages\home;

use App\Controllers\BaseController;

class PageController extends BaseController
{
    public function getIndex($name = null): string
    {
        $data['name'] = $name ?? 'World';

        return pageView('home/index', $data);
    }

    public function getDetail($id = null)
    {
        $data['name'] = 'Toni Haryanto';
        $data['id'] = $id;

        return pageView('home/detail', $data);
    }

}

app/Pages/home/index.php

<h1>Hello <?= $name ?>!</h1>
<p>Selamat berkarya dengan CodeIgniter!</p>

In the example above, we created two files. The first is PageController.php, a controller class with one method getIndex(). This method handles requests to mydomain.com/home or mydomain.com/home/index.

The getIndex() method can have parameters that capture the URI segment after the page segment. For instance, in the example above, you can call domain.com/home/Toni or domain.com/home/index/Toni, where the string 'Toni' will be received by the $name parameter of the getIndex() method.

In the example above, the getIndex() method returns the output of the pageView() function. This function is similar to view() in CodeIgniter but is adjusted to accept the path of the view file located under the app/Pages/ folder. return pageView('home/index', $data); means it returns the view file app/Pages/home/index.php.

In addition to the getIndex() method, you can also create other methods i.e. getDetail() or postInsert(). Only methods whose names start with an HTTP verb can handle HTTP requests. The method naming mechanism in this controller is the same as the Auto Route (improved) provided by CodeIgniter 4. Method getDetail() for example, can be accessed from mydomain.com/home/detail/[id].

API Endpoint

You can also return RESTful responses by adding the ResponseTrait to the controller class.

app/Pages/home/PageController.php

<?php

namespace App\Pages\profile;

use App\Controllers\BaseController;

class PageController extends BaseController
{
    use \CodeIgniter\API\ResponseTrait;

    public function getIndex()
    {
        $data['name'] = 'Toni Haryanto';
        $data['city'] = 'Bandung';

        return $this->respond($data);
    }

    public function getDetail($id = null)
    {
        $data['name'] = 'Toni Haryanto';
        $data['city'] = 'Bandung';
        $data['id'] = $id;

        return $this->respond($data);
    }
}

For more information about the API Response Trait, refer to the CodeIgniter documentation here: API Responses Trait.

Combination with Manual Routes

You can still use the Manual Route mechanism alongside the Auto Route (Improved) provided by CodeIgniter 4 in conjunction with this page-based routing. The execution order of the routers is [manual route] - [page-based route] - [auto route].

Page Template Generator

You can run this spark command to create a new page folder and files:

php spark page:create pagename
php spark page:create pagename/subpage

This command will create a new page folder with its sample controller and view file.

Contribution

We welcome community contributions! If you have ideas or find bugs, feel free to submit a pull request or open an issue in this repository.

License

Similar to the CodeIgniter 4 repository, this package is licensed under the MIT license. See the LICENSE file for more details.

yllumi/ci4-pages 适用场景与选型建议

yllumi/ci4-pages 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.4k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2024 年 12 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 yllumi/ci4-pages 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yllumi/ci4-pages 我们能提供哪些服务?
定制开发 / 二次开发

基于 yllumi/ci4-pages 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.4k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 9
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-27