Table of Contents
CodeIgniter自定义控制器MY_Controller用法分析,codeigniter控制器
您可能感兴趣的文章:
Home php教程 php手册 CodeIgniter自定义控制器MY_Controller用法分析,codeigniter控制器

CodeIgniter自定义控制器MY_Controller用法分析,codeigniter控制器

Jun 13, 2016 am 08:47 AM
codeigniter controller

CodeIgniter自定义控制器MY_Controller用法分析,codeigniter控制器

本文实例讲述了CodeIgniter自定义控制器MY_Controller用法。分享给大家供大家参考,具体如下:

Codeigniter所有的控制器都必须继承CI_Controller类,但CI_Controller类位于system目录下,不太方便修改。为方便做一些公用的处理,通常情况下我们会在core下创建MY_Controller,用来继承CI_Controller,从而项目中所有的控制器继承MY_Controller。

那么,MY_Controller 通常会做些什么呢?

所有的控制器都继承了MY_Controller, MY_Controller常常会加载一些公用帮助函数、公用类库,以及实现一些公用的方法。

公用的方法?公有的方法?

看到这些方法会意识到一个问题,如果方法是public的,那是否可以通过浏览器访问到。答案是可以的!这样不该让用户访问到的方法让用户访问到了。那设置protected吧。。。

备注:CI_Controller中写public方法不会被访问到,框架限制了CI_Controller中方法通过浏览器访问。

随着项目的不断进展,MY_Controller中的公用方法会越来越多。如果此时要增加后台管理的功能,所有的控制器依然继承MY_Controller,那其中的很多方法可能不适用了。如果后台需要的一些公用方法也写在这里,这里将会变得混乱。

如何按模块区分不同的控制器?

有两种处理的方式,第一种是通过不同的公用控制器文件来区分,由控制器去决定继承哪一个公用控制器,当然这里得引入公用文件。还有这种方式是可以通过对象的一个属性来维护,不同的模块赋予该属性不同的对象。如:

<&#63;php 
if ( ! defined('BASEPATH'))
  exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
 public function __construct($type = NULL)
 {
   parent::__construct();
   switch($type) {
    case 'api' :
     $this->load->library('api_helper', NULL, 'helper');
     break;
    case 'admin' :
     $this->load->library('admin_helper', NULL, 'helper');
      break;
    default :
     $this->load->library('app_helper', NULL, 'helper');
      break
   }
 }
}
/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */

Copy after login

控制器调用MY_Controller构造函数并传入type值,根据不同的type值会加载不同的类库,然后给类定义一个统一的别名,方便处理。具体的library可以处理该模块公用的方法或load公用的资源,相当于该模块的一个公用类。当然处理方式也可以是直接通过路由中的目录名或者控制器名称来控制等等。

这样避免了加载不同的文件,调用方法时只需要通过$this->helper对象调用。在仔细看看,可以发现不同模块的公用类是放在library中,放在library或helper中都可以使用get_intance获取控制器对象,但每次使用都需要获取实例,相对麻烦,如果是模型呢?感觉也不太好。其中的公用方法有一些会跟业务逻辑相关,放在library感觉不太合适。

业务逻辑好像并没有一个好的地方去实现,控制器的私有方法?模型?

先总结下上面的处理方法:

1、不同模块之间可以按需加载以及实现自定义的公用方法,各个模块之间互不影响。如果各模块之间的公用方法比较多,也可以再去继承一个公用的类。

2、公用方法放在library中,调用CI实例不方便。

3、如果不喜欢$this->herlper的调用方法,可以让控制器去继承不同的公用控制器,思路是一样的,只是可能需要手动引入文件。

更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》和《CI(CodeIgniter)框架进阶教程》

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

您可能感兴趣的文章:

  • Codeigniter控制器controller继承问题实例分析
  • 2个Codeigniter文件批量上传控制器写法例子
  • CodeIgniter钩子用法实例详解
  • CodeIgniter配置之database.php用法实例分析
  • CodeIgniter多语言实现方法详解
  • CI(CodeIgniter)模型用法实例分析
  • CodeIgniter视图使用注意事项
  • CodeIgniter读写分离实现方法详解
  • CI(CodeIgniter)简单统计访问人数实现方法
  • CodeIgniter控制器之业务逻辑实例分析
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to properly calibrate your Xbox One controller on Windows 11 How to properly calibrate your Xbox One controller on Windows 11 Sep 21, 2023 pm 09:09 PM

Since Windows has become the gaming platform of choice, it's even more important to identify its gaming-oriented features. One of them is the ability to calibrate an Xbox One controller on Windows 11. With built-in manual calibration, you can get rid of drift, random movement, or performance issues and effectively align the X, Y, and Z axes. If the available options don't work, you can always use a third-party Xbox One controller calibration tool. Let’s find out! How do I calibrate my Xbox controller on Windows 11? Before proceeding, make sure you connect your controller to your computer and update your Xbox One controller's drivers. While you're at it, also install any available firmware updates. 1. Use Wind

How to implement custom middleware in CodeIgniter How to implement custom middleware in CodeIgniter Jul 29, 2023 am 10:53 AM

How to implement custom middleware in CodeIgniter Introduction: In modern web development, middleware plays a vital role in applications. They can be used to perform some shared processing logic before or after the request reaches the controller. CodeIgniter, as a popular PHP framework, also supports the use of middleware. This article will introduce how to implement custom middleware in CodeIgniter and provide a simple code example. Middleware overview: Middleware is a kind of request

How to use CodeIgniter4 framework in php? How to use CodeIgniter4 framework in php? May 31, 2023 pm 02:51 PM

PHP is a very popular programming language, and CodeIgniter4 is a commonly used PHP framework. When developing web applications, using frameworks is very helpful. It can speed up the development process, improve code quality, and reduce maintenance costs. This article will introduce how to use the CodeIgniter4 framework. Installing the CodeIgniter4 framework The CodeIgniter4 framework can be downloaded from the official website (https://codeigniter.com/). Down

Learning Laravel from scratch: Detailed explanation of controller method invocation Learning Laravel from scratch: Detailed explanation of controller method invocation Mar 10, 2024 pm 05:03 PM

Learning Laravel from scratch: Detailed explanation of controller method invocation In the development of Laravel, controller is a very important concept. The controller serves as a bridge between the model and the view, responsible for processing requests from routes and returning corresponding data to the view for display. Methods in controllers can be called by routes. This article will introduce in detail how to write and call methods in controllers, and will provide specific code examples. First, we need to create a controller. You can use the Artisan command line tool to create

CodeIgniter middleware: Accelerate application responsiveness and page rendering CodeIgniter middleware: Accelerate application responsiveness and page rendering Jul 28, 2023 pm 06:51 PM

CodeIgniter Middleware: Accelerating Application Responsiveness and Page Rendering Overview: As web applications continue to grow in complexity and interactivity, developers need to use more efficient and scalable solutions to improve application performance and responsiveness. . CodeIgniter (CI) is a lightweight PHP-based framework that provides many useful features, one of which is middleware. Middleware is a series of tasks that are performed before or after the request reaches the controller. This article will introduce how to use

What is laravel controller What is laravel controller Jan 14, 2023 am 11:16 AM

In laravel, a controller (Controller) is a class used to implement certain functions; the controller can combine related request processing logic into a separate class. Some methods are stored in the controller to implement certain functions. The controller is called through routing, and callback functions are no longer used; the controller is stored in the "app/Http/Controllers" directory.

How to use the database query builder (Query Builder) in the CodeIgniter framework How to use the database query builder (Query Builder) in the CodeIgniter framework Jul 28, 2023 pm 11:13 PM

Introduction to the method of using the database query builder (QueryBuilder) in the CodeIgniter framework: CodeIgniter is a lightweight PHP framework that provides many powerful tools and libraries to facilitate developers in web application development. One of the most impressive features is the database query builder (QueryBuilder), which provides a concise and powerful way to build and execute database query statements. This article will introduce how to use Co

Use PHP framework CodeIgniter to develop a real-time chat application to provide convenient communication services Use PHP framework CodeIgniter to develop a real-time chat application to provide convenient communication services Jun 27, 2023 pm 02:49 PM

With the development of mobile Internet, instant messaging has become more and more important and popular. For many companies, live chat is more like a communication service, providing a convenient communication method that can quickly and effectively solve business problems. Based on this, this article will introduce how to use the PHP framework CodeIgniter to develop a real-time chat application. Understand the CodeIgniter framework CodeIgniter is a lightweight PHP framework that provides a series of simple tools and libraries to help developers quickly

See all articles