CodeIgniter学习笔记三:扩展CI的控制器、模型,codeigniterci
一、扩展CI中的控制器
有时需要对CI中的控制器作统一操作,如进行登录和权限验证,这时就可以通过扩展CI控制器来实现。
扩展CI控制器只需要在application/core文件夹中建一个继承自CI_Controller类的MY_Controller类即可,然后在这个类中实现自己需要的逻辑。
关于上面这句话,有两点需要解释一下:
1、为什么要在application/core文件夹中:是因为基类CI_Controller是在system/core文件夹中,这里需要跟system中对应。
2、为什么扩展的控制器前缀是MY_,可否换成其他的:这个前缀是在application/config/config.php中定义的:
<span>$config</span>['subclass_prefix'] = 'MY_';
只需要这两处对应上就可以了。
二、模型
示例application/models/user_model.php:
<?<span>php </span><span>/*</span><span>* * User_model </span><span>*/</span> <span>class</span> User_model <span>extends</span><span> CI_Model{ </span><span>//</span><span>return all users</span> <span>public</span> <span>function</span><span> getAll() { </span><span>$res</span> = <span>$this</span> -> db -> get('test'<span>); </span><span>return</span> <span>$res</span> -><span> result(); } }</span>
注意点:
1、文件名全小写
2、类名首字母大写
3、模型中可以使用超级对象中的属性
4、建议用_model作后缀,防止跟其他类名冲突
使用示例:
<span>public</span> <span>function</span><span> index() { </span><span>//</span><span>load model</span> <span>$this</span> -> load -> model('User_model'<span>); </span><span>$usermodel</span> = <span>$this</span> -> User_model -><span> getAll(); </span><span>//</span><span>别名</span> <span>$this</span> -> load -> model('User_model', 'user'<span>); </span><span>$usermodel</span> = <span>$this</span> -> user -><span> getAll(); </span><span>var_dump</span>(<span>$usermodel</span><span>); }</span>
模型主要用于规范项目结构。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
