ci框架(一),ci框架(
ci框架(一),ci框架(
ci目录结构
|-----<span>system</span><span> 框架程序目录 </span>|-----<span>core 框架的核心程序 </span>|-----CodeIgniter.<span>php 引导性文件 </span>|-----Common.<span>php 加载基类库的公共函数 </span>|-----Controller.<span>php 基控制器类文件:CI_Controller </span>|-----Model.<span>php 基模型类文件:CI_Model </span>|-----Config.<span>php 配置类文件:CI_Config </span>|-----Input.<span>php 输入类文件:CI_Input </span>|-----Output.<span>php 输出类文件:CI_Output </span>|-----URL.<span>php URL类文件:CI_URl </span>|-----Router.<span>php 路由类文件:CI_Router </span>|-----Loader.<span>php 加载类文件:CI_Loader </span>|-----<span>helpers 辅助函数 </span>|-----url_helper.<span>php url相关的辅助函数,如:创建url的辅助函数 </span>|-----captcha_helper.<span>php创建图形验证码的辅助函数 </span>|-----<span>libraries 通用类库 </span>|-----Pagination.<span>php 通用分页类库 </span>|-----Upload.<span>php 通用文件上传类库 </span>|-----Image_lib.<span>php 通用图像处理类库 </span>|-----Session.<span>php 通用session类库 </span>|-----<span>language 语言包 </span>|-----<span>database 数据库操作相关的程序 </span>|-----DB_active_rec.<span>php 快捷操作类文件(ActiveRecord) </span>|-----<span>fonts 字库 </span>|-----<span>application 项目目录 </span>|-----<span>core 项目的核心程序 </span>|-----<span>helpers 项目的辅助函数 </span>|-----<span>libraries 通用类库 </span>|-----<span>language 语言包 </span>|-----<span>config 项目相关的配置 </span>|-----config.<span>php 项目相关的配置文件 </span>|-----database.<span>php 数据库相关的配置文件 </span>|-----autoload.<span>php 设置自动加载类库的配置文件 </span>|-----constants.<span>php 常量配置文件 </span>|-----routes.<span>php 路由配置文件 </span>|-----<span>controllers 控制器目录 </span>|-----welcome.<span>php 控制器文件,继承CI_Controller </span>|-----<span>models 模型目录 </span>|-----welcome_model.<span>php 模型文件,继承CI_Model </span>|-----<span>views 视图目录 </span>|-----welcome.php 视图模板文件,默认后缀名为.<span>php </span>|-----<span>cache 存放数据或模板的缓存文件 </span>|-----<span>errors 错误提示模板 </span>|-----<span>hooks 钩子,在不修改系统核心文件的基础上扩展系统功能 </span>|-----<span>third_party 第三方库 </span>|-----<span>logs 日志 </span>|-----index.php 入口文件
访问形式
在\application\controllers\welcome.php中添加函数:
<span>public</span> <span>function</span><span> hello() { </span><span>echo</span> "test"<span>; }</span>
入口文件.php/控制器/动作
自己新建控制器,hello.php:
<?php <span>if</span> ( ! <span>defined</span>('BASEPATH')) <span>exit</span>('No direct script access allowed'<span>); </span><span>class</span> Hello <span>extends</span><span> CI_Controller { </span><span>public</span> <span>function</span> sayHello(<span>$name</span><span>) { </span><span>echo</span> <span>$name</span>,",Hello World"<span>; } } </span>?>
注意:
- 方法名以下划线开头的是不能访问成功的,只能间接访问。
- 只能访问public修饰的方法。
- 尽量不要与类名相同的方法,会当作构造函数。
加载视图
application\views\view_test.php或者application\views\view\test.php(这中做法主要是方便同一类型的视图可以放在同一个文件夹下方便管理)
<span><</span><span>html</span><span>></span> <span><</span><span>head</span><span>></span> <span></</span><span>head</span><span>></span> <span><</span><span>body</span><span>></span><span> test_ci_hello_world </span><span></</span><span>body</span><span>></span> <span></</span><span>html</span><span>></span>
控制器:
<span>public</span> <span>function</span><span> addView() { </span><span>$this</span>->load->view("view_test"<span>); }</span>
或者:
<span>public</span> <span>function</span><span> addView2() { </span><span>$this</span>->load->view("view/test"<span>); }</span>
效果:
分配变量
把数据从控制器中传到视图中,控制器:
<span>public</span> <span>function</span><span> addView() { </span><span>$this</span>->load->vars("title","value"<span>); </span><span>$list</span> = <span>array</span><span>( </span><span>array</span>('id'=>1,'name'=>'jack','email'=>'123@123.com'), <span>array</span>('id'=>2,'name'=>'jack2','email'=>'1233@123.com'), <span>array</span>('id'=>3,'name'=>'jack3','email'=>'12333@123.com'<span>) ); </span><span>$data</span>['new_title']="标题"<span>; </span><span>$data</span>['list']=<span>$list</span><span>; </span><span>$this</span>->load->vars(<span>$data</span><span>); </span><span>$this</span>->load->view("view_test"<span>); }</span>
视图:
<html> <head> </head> <body> <h1><?php <span>echo</span> <span>$title</span>;?></h1> <h1><?php <span>echo</span> <span>$new_title</span>;?></h1><span> test_ci_hello_world </span><table> <?php <span>foreach</span>(<span>$list</span> <span>as</span> <span>$item</span>):?> <tr> <td><?=<span>$item</span>['id']?></td> <td><?=<span>$item</span>['name']?></td> <td><?=<span>$item</span>['email']?></td> </tr> <?php <span>endforeach</span>;?> </table> </body> </html>
效果:
public function addView() { $this->load->vars("title","value"); $list = array( array('id'=>1,'name'=>'jack','email'=>'123@123.com'), array('id'=>2,'name'=>'jack2','email'=>'1233@123.com'), array('id'=>3,'name'=>'jack3','email'=>'12333@123.com') ); $data['new_title']="标题"; $data['list']=$list; $this->load->vars($data); $this->load->view("view_test"); $this->load->view("footer"); }
视图view_test:
<html> <head> </head> <body> <h1><?php <span>echo</span> <span>$title</span>;?></h1> <h1><?php <span>echo</span> <span>$new_title</span>;?></h1><span> test_ci_hello_world </span><table> <?php <span>foreach</span>(<span>$list</span> <span>as</span> <span>$item</span>):?> <tr> <td><?=<span>$item</span>['id']?></td> <td><?=<span>$item</span>['name']?></td> <td><?=<span>$item</span>['email']?></td> </tr> <?php <span>endforeach</span>;?> </table>
视图footer:
<span>CI_hello_world!!! </span><span></</span><span>body</span><span>></span> <span></</span><span>html</span><span>></span>
显示效果:
uri参数获取
控制器:
<span>public</span> <span>function</span> getUri(<span>$id</span>,<span>$name</span>,<span>$year</span><span>) { </span><span>echo</span> "id--->".<span>$id</span>."---name--->".<span>$name</span>."---year--->".<span>$year</span>."<br />"<span>; </span><span>echo</span> "segment(1)--->".<span>$this</span>->uri->segment(1)."<br />"<span>; </span><span>echo</span> "segment(2)--->".<span>$this</span>->uri->segment(2)."<br />"<span>; </span><span>echo</span> "segment(3)--->".<span>$this</span>->uri->segment(3)."<br />"<span>; </span><span>echo</span> "segment(4)--->".<span>$this</span>->uri->segment(4)."<br />"<span>; </span><span>echo</span> "segment(5)--->".<span>$this</span>->uri->segment(5)."<br />"<span>; }</span>
效果:
加载数据库
这个操作在MVC中是放到model中做的。
在\application\config\database.php中配置数据库参数,注意dbprefix和swap_pre这两个参数。在php中写的是前缀,会默认当作swap_pre,然后放到数据库中的时候会转成dbprefix,但是最好两个都弄成一样的。
还有$active_group,默认是default,如果要连接两个数据库,把default另外取名,然后在函数中写明参数就OK。
必须继承数据核心类CI_Model,同时重载父类中的构造方法。
<span>class</span> Model_name <span>extends</span><span> CI_Model { </span><span>function</span><span> __construct() { parent</span>::<span>__construct(); } }</span>
在每次使用数据库的时候,都需要加载一次数据库:
<span>$this</span>->load->database();
为了方便,可以将数据库的加载设置成自动加载,在\application\config\autoload.php中。
<span>$autoload</span>['libraries'] = <span>array</span>('database');
对于数据库访问对象,装载到超级对象的属性中 $this->db
<span>$res</span> = <span>$this</span>->db->query(<span>$sql</span>);<span>//</span><span>返回对象</span> <span>$res</span>->result();<span>//</span><span>返回数组,数组中是一个一个的对象</span> <span>$res</span>->result_array();<span>//</span><span>返回二维数组,里面是关联数组</span> <span>$res</span>->row();<span>//</span><span>返回第一条数据,直接是一个对象</span>
AR操作数据库
在database.php文件中,将$active_recoed的值改为TRUE,这样就可以使用AR了。
<span>//</span><span>查询</span> <span>public</span> <span>function</span><span> index() { </span><span>$res</span> = <span>$this</span>->db->get('表名');<span>//</span><span>这里自动调用前缀</span> <span>foreach</span>(<span>$res</span>->result() <span>as</span> <span>$item</span><span>) { </span><span>echo</span> <span>$item</span>->name."<br />"<span>; } }</span>
<span>//</span><span>插入</span> <span>public</span> <span>function</span><span> index() { </span><span>$data</span>=<span>array</span><span>( </span>'name'=>'lisi', 'password'=><span>md5</span>('lisi'<span>) ); </span><span>$bool</span> = <span>$this</span>->db->insert("表名",<span>$data</span><span>); </span><span>var_dump</span>(<span>$bool</span><span>); }</span>
<span>//</span><span>更新</span> <span>public</span> <span>function</span><span> index() { </span><span>$data</span>=<span>array</span><span>( </span>'name'=>'wangwu', 'password'=><span>md5</span>('wangwu'<span>) ); </span><span>$bool</span> = <span>$this</span>->db->update('表名',<span>$data</span>,<span>array</span>('id'=>3<span>)); </span><span>var_dump</span>(<span>$bool</span><span>); }</span>
<span>//</span><span>删除</span> <span>$bool</span> = <span>$this</span>->db->delete('表名',<span>array</span>('id'=>2<span>)); </span><span>var_dump</span>(<span>$bool</span>);
常规的方法是ul嵌套,即主菜单ul-li里嵌套子菜单ul,要用到两级循环
首先循环主菜单,要有固定的条件来判断出主菜单,比如主菜单的uid==0或者其它。。。
- 栏目名称
if($news_item['uid'] == 0){ //判断并得到主菜单
echo " - ".$news_item['title'] . '
- ';
- "."ss".$child_item['title']."";
}
endforeach;
echo "";
}
endforeach; ?>
当然这仅限于两级菜单,多级或无限极,可以使用函数递归
function menu($uid=0){ //设置缺省从主菜单开始
global $news;
foreach ($news as $news_item):
if($news_item['uid'] == $uid){
echo " - ".$news_item['title'] . '
- ';
menu($news_item['id']); //递归调用
echo "";
}
endforeach;
}
------ 调用方法 ------------------------------- >
难道MY_Controller不要类名一致?
foreach ($news as $child_item): //循环二次
if($news_item['id'] == $child_item['uid']){ //判断并得到对应子菜单
echo " - "."ss".$child_item['title']."";

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Evaluating the cost/performance of commercial support for a Java framework involves the following steps: Determine the required level of assurance and service level agreement (SLA) guarantees. The experience and expertise of the research support team. Consider additional services such as upgrades, troubleshooting, and performance optimization. Weigh business support costs against risk mitigation and increased efficiency.

The learning curve of a PHP framework depends on language proficiency, framework complexity, documentation quality, and community support. The learning curve of PHP frameworks is higher when compared to Python frameworks and lower when compared to Ruby frameworks. Compared to Java frameworks, PHP frameworks have a moderate learning curve but a shorter time to get started.

The lightweight PHP framework improves application performance through small size and low resource consumption. Its features include: small size, fast startup, low memory usage, improved response speed and throughput, and reduced resource consumption. Practical case: SlimFramework creates REST API, only 500KB, high responsiveness and high throughput

Writing clear and comprehensive documentation is crucial for the Golang framework. Best practices include following an established documentation style, such as Google's Go Coding Style Guide. Use a clear organizational structure, including headings, subheadings, and lists, and provide navigation. Provides comprehensive and accurate information, including getting started guides, API references, and concepts. Use code examples to illustrate concepts and usage. Keep documentation updated, track changes and document new features. Provide support and community resources such as GitHub issues and forums. Create practical examples, such as API documentation.

Choose the best Go framework based on application scenarios: consider application type, language features, performance requirements, and ecosystem. Common Go frameworks: Gin (Web application), Echo (Web service), Fiber (high throughput), gorm (ORM), fasthttp (speed). Practical case: building REST API (Fiber) and interacting with the database (gorm). Choose a framework: choose fasthttp for key performance, Gin/Echo for flexible web applications, and gorm for database interaction.

In Go framework development, common challenges and their solutions are: Error handling: Use the errors package for management, and use middleware to centrally handle errors. Authentication and authorization: Integrate third-party libraries and create custom middleware to check credentials. Concurrency processing: Use goroutines, mutexes, and channels to control resource access. Unit testing: Use gotest packages, mocks, and stubs for isolation, and code coverage tools to ensure sufficiency. Deployment and monitoring: Use Docker containers to package deployments, set up data backups, and track performance and errors with logging and monitoring tools.

There are five misunderstandings in Go framework learning: over-reliance on the framework and limited flexibility. If you don’t follow the framework conventions, the code will be difficult to maintain. Using outdated libraries can cause security and compatibility issues. Excessive use of packages obfuscates code structure. Ignoring error handling leads to unexpected behavior and crashes.

When choosing a Go framework, key performance indicators (KPIs) include: response time, throughput, concurrency, and resource usage. By benchmarking and comparing frameworks' KPIs, developers can make informed choices based on application needs, taking into account expected load, performance-critical sections, and resource constraints.
