现在的web开发确实已经到了无框架卜欢乐的状态,无论是前端:JavaScript(vue.js\react.js\angular.js框架和jQuery库),CSS(bootstrap),还是后端:PHP(CI/thinkPHP/laravel/YII/),python(Django/Tornado/Flask)都是一片欣欣向荣的景象;这种框架横行的情况在一定程度上说明了这个方向的火热和技术的不断沉淀。“牛逼的人都在写框架,菜鸟都在学习用框架”或许成了这个方向上判定一个人技术高度的标准,所以没有写过框架的人都不能算是大牛了。但是回归生产,我们不一定要会写框架,但是一定要会用框架,本文将介绍如何在我们的网站中配置一个轻量级、性能出色、兼容各种主机环境、没有大规模集成类库、几乎 0 配置、无需学习模板语言、没有严格的编码规则、让你有更多的时间远离电脑做你喜欢做的事情的PHP框架CodeIgniter,俗称PHP CI框架。
关于网站的基本搭建以及配置请看我的上一篇博客 LNMP环境搭建
不多介绍,HERE WE GO
1. 下载框架文件并配置到你的网站根目录
框架官网找自己看上的版本随便下
例如我们下载了一个CodeIgniter-3.1.5.zip的框架包,然后:
unzip CodeIgniter-3.1.5.zip
#解压框架文件
cd CodeIgniter-3.1.5
mv * /usr/local/nginx/html/
#将框架文件移动到你的网站根目录下
这时候的目录结构如下:
[root@rhel6-vm html]# tree -L 2
.
├── 50x.html
├── application
│ ├── cache
│ ├── config
│ ├── controllers
│ ├── core
│ ├── helpers
│ ├── hooks
│ ├── index.html
│ ├── language
│ ├── libraries
│ ├── logs
│ ├── models
│ ├── third_party
│ └── views
├── composer.json
├── contributing.md
├── index.html
├── index.php
├── license.txt
├── readme.rst
├── system
│ ├── core
│ ├── database
│ ├── fonts
│ ├── helpers
│ ├── index.html
│ ├── language
│ └── libraries
└── user_guide├── changelog.html├── contributing├── database├── DCO.html├── documentation├── _downloads├── general├── genindex.html├── helpers├── _images├── index.html├── installation├── libraries├── license.html├── objects.inv├── overview├── search.html├── searchindex.js├── _static└── tutorial
访问测试一下:
根据提示我们可知展现的网页是由一下两个文件生成:
application/views/welcome_message.php
application/controllers/Welcome.php
只写过简单网页的同学在这里就要迷糊了,为什么需要两个网页来生成一个页面,那么你需要好好学习一下CI框架的工作机制了(model / control / view)。
默认情况下Nginx是不支持CodeIgniter的,上边的页面也只是一个测试页,如果要写一个真正的应用的话那么我们还有一些事情要做:
1. 修改CodeIgniter配置:
vim /usr/local/nginx/html/application/config/config.php
修改以下两项内容
$config['base_url'] = '/usr/local/nginx/html';
#对于已经上线的网站可以写成域名$config['uri_protocol'] = 'PATH_INFO';
//$config['uri_protocol'] = 'REQUEST_URI';
2. 修改Nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
server {listen 80;server_name localhost;#access_log logs/host.access.log main;location /{root html;index index.php index.html index.htm;rewrite ^/$ /index.php last;rewrite ^/(?!index\.php|robots\.txt|images|js|styles)(.*)$ /index.php/$1 last;#重写网页,修改CI中的默认的index.php}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}
location /index.php{fastcgi_pass 127.0.0.1:9000;fastcgi_param SCRIPT_FILENAME /usr/loca/nginx/html/index.php;fastcgi_param PATH_INFO $fastcgi_path_info;fastcgi_split_path_info ^(.+\.php)(.*)$;fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;include fastcgi.conf;
}
}
接下来我们就可以按照CI框架的编程习惯来写程序了
1. 写一个自己的controller
按照CI框架的规范,我们在网站跟目录的application/controllers下新建一个controller,需要注意的是controller的文件名称首字母必须是大写,后缀是.php
[root@rhel6-vm controllers]# pwd
/usr/local/nginx/html/application/controllers
[root@rhel6-vm controllers]# cat Halo.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Halo extends CI_Controller {public function index(){echo 'Hello CodeIgniter!';}
}
来我们测试一下:
Note: 如果项目的内容比较多,那么我们就需要把控制器进行分类,要怎么做呢,只需要在控制器目录中建立子目录就可以了,如下auth就是子目录,其中包含了一个Login控制器:
这样在访问的时候只需要把子目录写在控制器前面就可以了:
2. 写一个自己的view
视图就是一般的PHP文件,文件命名和内容格式没有什么要求
[root@rhel6-vm views]# pwd
/usr/local/nginx/html/application/views
[root@rhel6-vm views]# cat login.php
<?php
echo 'It is a view for controller Halo to call!';
?>
写了视图文件要怎么用呢?我们要知道光有视图文件没用的,它必须被controller调用才能生效,接下来我们去修改一下controller:
[root@rhel6-vm controllers]# cat Halo.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Halo extends CI_Controller {public function index(){$this->load->view('login.php');############注意这一行就是对视图的调用,这样我们就完成了视图与控制器的关联。}
}
测试一下结果:
Note: 如果项目的内容比较多,那么我们就需要把视图进行分类,要怎么做呢,只需要在视图目录中建立子目录就可以了,如下auth就是子目录,其中包含了一个login.php视图页面:
[root@rhel6-vm auth]# pwd
/usr/local/nginx/html/application/views/auth
[root@rhel6-vm auth]# cat login.php
<?php
echo 'This is a sub directory file called by controller';
?>
[root@rhel6-vm auth]#
这时候要怎么把控制器和视图函数进行绑定呢,很简单,真的很简单,引入文件的时候把子目录加到视图文件之前:
[root@rhel6-vm auth]# cat Login.php
#对应的控制器中内容如下:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');class Login extends CI_Controller {public function index(){$this->load->view('auth/login.php');}
}
[root@rhel6-vm auth]# pwd
/usr/local/nginx/html/application/controllers/auth
[root@rhel6-vm auth]#