040.CI4框架CodeIgniter,使用Response实现前后端分离并返回JSON

news/2024/10/4 15:29:59

01、BaseController.php代码如下:

<?phpnamespace App\Controllers;use CodeIgniter\Controller;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;/*** Class BaseController** BaseController provides a convenient place for loading components* and performing functions that are needed by all your controllers.* Extend this class in any new controllers:*     class Home extends BaseController** For security be sure to declare any new methods as protected or private.*/
abstract class BaseController extends Controller
{//引用Responseuse ResponseTrait;/*** Instance of the main Request object.** @var CLIRequest|IncomingRequest*/protected $request;/*** An array of helpers to be loaded automatically upon* class instantiation. These helpers will be available* to all other controllers that extend BaseController.** @var list<string>*/protected $helpers = [];/*** Be sure to declare properties for any property fetch you initialized.* The creation of dynamic property is deprecated in PHP 8.2.*/// protected $session;/*** @return void*/public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger){// Do Not Edit This Lineparent::initController($request, $response, $logger);// Preload any models, libraries, etc, here.// E.g.: $this->session = \Config\Services::session();
    }public function get_url($aurl){return base_url('public/index.php/') . $aurl;}public function get_respond($adata = '', $acode = '0', $amessage = 'success'){$response = ['data' => $adata,'code' => $acode,'message' => $amessage,];return $this->respondCreated($data = $response);}
}

02、Login.php代码如下:

<?php namespace App\Controllers\Api\V1\Users;use App\Controllers\BaseController;//http://localhost/phmci4/public/index.php/api/v1/users/login
class Login extends BaseController
{public function index(){$data = array('token' => 'token-admin',);return $this->get_respond($data);}//http://localhost/phmci4/public/index.php/api/v1/users/login/infopublic function info(){$data = array('username' => 'token-admin','roles' => ['admin'],);return $this->get_respond($data);}//http://localhost/phmci4/public/index.php/api/v1/users/login/codepublic function code(){$img_url = 'http://dummyimage.com/100x40/dcdfe6/000000.png&text=V3Admin';return $this->get_respond($img_url);}
}

03、结构如下:

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/55000.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

使用 Quickwit 的搜索流功能为 ClickHouse 添加全文搜索

本指南将帮助您使用 Quickwit 的搜索流功能为知名的 OLAP 数据库 ClickHouse 添加全文搜索。Quickwit 暴露了一个 REST 端点,可以极快地(每秒最多 5000 万条)流式传输匹配搜索查询的 ID 或其他属性,ClickHouse 可以轻松地使用它们进行连接查询。 我们将采用 GitHub 存档数据…

【阅己书城】逆向生成代码

1、配置renren-generator # application.ymlserver:port: 80 # mysql spring:datasource:type: com.alibaba.druid.pool.DruidDataSource#MySQL配置driverClassName: com.mysql.cj.jdbc.Driverurl: jdbc:mysql:///:3306/yueji_pms?useUnicode=true&characterEncoding=UTF-…

SimpleTranslationAIAgent:基于C#与LLM的翻译AI Agent

基于C#与LLM通过简单对话即可实现文件到文件的翻译任务 该软件是MIT协议完全开源免费的,但是调用LLM的API可能需要费用,但是没关系,赛博菩萨硅基流动与智谱AI等都有免费的模型可调了。 这个Translation AI Agent只是一个简单的AI Agent示例应用,可能很多人都不需要它。 开源…

SimpleTranslationAIAgent借助SiliconCloud API 构建自己的专属翻译助手

SimpleTranslationAIAgent介绍 SimpleTranslationAIAgent是一款基于C#与LLM通过简单对话即可实现文件到文件的翻译任务的简单应用,基于WPF与Semantic Kernel构建。 该软件是MIT协议完全开源免费的,但是调用LLM的API可能需要费用,但是没关系,赛博菩萨硅基流动有免费的模型可…

autodock vina后处理分析

拆分对接结果vina_split --input result.pdbqt --ligand complex/lig使用mv命令批量修改文件名,把01-09修改成1-9,便于批量处理for i in `seq 1 9`; do > mv "lig0${i}.pdbqt" "lig${i}.pdbqt" > done使用Openbabel把pdbqt转成pdbfor i in `seq …

039.CI4框架CodeIgniter,封装Model模型绑定数据库的封装

01、ModelBase.php代码如下:<?phpnamespace App\Models;use CodeIgniter\Database\ConnectionInterface; use CodeIgniter\Model; use CodeIgniter\Validation\ValidationInterface;class ModelBase extends Model {var $Db;function __construct(ConnectionInterface $db…

洛谷 P11011 点的覆盖

洛谷 P11011 点的覆盖 题意 给定一个四边平行于坐标轴的矩形 \(A\),给定 \(n\) 个在矩形 \(A\) 内部(可能在边缘上)的点。 求有多少个 \(A\) 的子矩形覆盖了所有 \(n\) 个点(允许在边缘上)。 所有坐标都是整数。 思路 求出:\(X_1=\max_{i=1}^n x_i\),\(X_2=\min_{i=1}^n…

使用 nuxi build-module 命令构建 Nuxt 模块

title: 使用 nuxi build-module 命令构建 Nuxt 模块 date: 2024/8/31 updated: 2024/8/31 author: cmdragon excerpt: nuxi build-module 命令是构建 Nuxt 模块的核心工具,它将你的模块打包成适合生产环境的格式。通过使用 --stub 选项,你可以在开发过程中加快模块构建速度…