PHP MVC开发之单一入口文件(路由文件)

从本节起开始正式讲述mvc的开发,理论和代码一起上。

下面图片是目录结构

了解zend framework 的朋友肯定非常熟悉这样的目录结构

其中application 文件夹 是应用层的核心代码

Library文件夹 是mvc框架底层代码(咱们课程重点就是讲述这个文件夹里的文件)

www是网站的根目录,明显看到 网站跟目录和 application以及library 没有包含在www目录里,这样也可以起到一定的安全作用,www目录中放置模板,图片等一些代码

 本节主要讲述和网站入口相关的三个文件 图中中已经用红色表示出

首先看看.htaccess文件代码

1
2
3
4
5
6
7
8
9
10
11
12
13
SetEnv APPLICATION_ENV development
 
RewriteEngine On
 
RewriteCond %{REQUEST_FILENAME} -s [OR]
 
RewriteCond %{REQUEST_FILENAME} -l [OR]
 
RewriteCond %{REQUEST_FILENAME} -d
 
RewriteRule ^.*$ - [NC,L]
 
RewriteRule ^.*$ index.php [NC,L]

这个文件相当于apache服务器的配置文件一部分,每次当客户访问网站的时候,apache服务器都会先查看.htaccess文件的内容,然后再执行相关的请求,对.htaccess文件不了解的朋友直接google一下,资料很多.

上面几行代码的作用是: 如果访问的目录或文件不存在,全部执行index.php文件,举个例子大家就明白了, 比如用户访问http://tianliangle.com/a/b.html 如果网站根目录中确实存在这个文件,那么久执行 a目录下的b.html文件,如果没有这个相关文件 则执行index.php文件。有人可能会问,为什么不把所有的请求都指向index.php文件,这样不更好吗?这样做的不妥之处在于,但用户请求js 文件css文件图片文件时,没必要指向index.php,还有很多功能,也没必要执行index.php 文件

假如用户请求的地址为 http://tianliangle.com/a/b/c/1

Mvc 框架需要解决的基础问题是,通过地址栏 确定唯一的moudle, conttoller, action

由上面可知道,服务器指向了index.php文件

Index.php 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
 
header( "content-Type: text/html; charset=utf-8" );
 
     define( "COMMON_AUTH" ,TRUE);
 
    
 
     !defined( "COMMON_AUTH" ) && dir( "NO_AUTH" );
 
     ini_set ( 'display_startup_errors' , 1);
 
     ini_set ( 'display_errors' , 1);
 
     //error_reporting(0);
 
     error_reporting (0);
 
     // Define base path obtainable throughout the whole application
 
     defined( 'BASE_PATH' )
 
         || define( 'BASE_PATH' , realpath (dirname( __FILE__ )). '/../' );
 
     // Define path to application directory
 
     defined( 'APPLICATION_PATH' )
 
         || define( 'APPLICATION_PATH' , BASE_PATH . '/application' );
 
     // Define application environment
 
     defined( 'APPLICATION_ENV' )
 
         || define( 'APPLICATION_ENV' , ( getenv ( 'APPLICATION_ENV' ) ? getenv ( 'APPLICATION_ENV' ) : 'production' ));
 
     defined( 'WEB_ROOT' )
 
         || define( 'WEB_ROOT' , BASE_PATH . 'www' );
 
        
 
     /**
 
      * 设置模板类型
 
      */ 
 
          $_web_tpl = 'view' ;
 
     // Define application environment
 
     // Set include path to Zend (and other) libraries
 
     set_include_path( PATH_SEPARATOR . BASE_PATH . '/library' .
 
         PATH_SEPARATOR . APPLICATION_PATH . '/models' .
 
         PATH_SEPARATOR . APPLICATION_PATH . '/modules'
 
          PATH_SEPARATOR . WEB_ROOT . '/config'
 
            PATH_SEPARATOR . APPLICATION_PATH . '/function'
 
         PATH_SEPARATOR . get_include_path() .
 
         PATH_SEPARATOR . '.'
 
     );
 
session_start();
 
define( "WEB_AUTH" ,TRUE);
 
include_once APPLICATION_PATH. '/route.php' ;
 
?>

代码的作用是配置一些和项目相关的变量,设置一下include 目录

最后 include_once APPLICATION_PATH.'/route.php'; 把请求指向 route.php文件中

让我们看看route.php文件时如果解析http://tianliangle.com/a/b/c/1  并确定moudle, conttoller, action

Route.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
 
/**
 
  * 实现路由 认证 权限认证,以及 初始化
 
  */
 
  
 
defined( "WEB_AUTH" ) || die ( "NO_AUTH" );
 
/**
 
  * 包含基本配置文件
 
  */
 
include_once 'config.ini.php' ;
 
/**
 
  * 包含路由规则配置文件
 
  */
 
include_once 'route.ini.php' ;
 
  
 
/**
 
  * 包含所有模块 控制器列表
 
  */
 
include_once 'mvc.ini.php' ;
 
  
 
/**
 
  * 包含所有权限配置
 
  */
 
include_once 'role.ini.php' ;
 
  
 
/**
 
  * 包含路由类   路由核心类
 
  */
 
include_once 'route/route.class.php' ;
 
/**
 
  * 包含公用函数库
 
  */
 
include_once 'global.func.php' ;
 
/**
 
  * 初始化路由  实现 基本派遣
 
  */
 
$route = new Route();
 
  
 
//$route->viewMvc();
 
//print_r($_GET);
 
  
 
/**
 
  * 初始化程序所需的配置
 
  */
 
include_once 'common.ini.php' ;
 
  
 
/**
 
  * 执行程序
 
  */
 
$route ->run();
 
?>

文件包含了一些项目配置文件,主要是些变量声明

这里重点说说include_once 'route.ini.php'; 这个文件作用是给指定的地址形式配置固定的moudle, conttoller, action, 以便在以后程序中查看,本文中会讲到

举个列子

include_once 'route.ini.php

1
2
3
4
5
6
7
$routeArr = array (
 
"/reg" => array (
 
                "mac" => array ( "module" => "default" , "conttoller" => "index" , "action" => "reg" ),
 
                   ));

这个配置的作用是 当用户请求http://tianliangle.com/reg 的时候 就执行"module"=>"default","conttoller"=>"index","action"=>"reg" 对应的模块 而不用请求http://tianliangle.com/default/default/reg 起到缩短地址的作用

然后

include_once 'route/route.class.php';

$route = new Route();

$route->run();

我们可以知道 整个解析http://tianliangle.com/a/b/c/1  并派发  的实现是在 new Route() 时完成的,也就是在 Route类的析构函数中完成

route/route.class.php // 只粘贴核心的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
 
/**
 
  * 本文件实现路由解析功能 和 路由检测功能 以及 权限认证功能
 
  *
 
  */
 
  
 
/**
 
  * 本文件只能被index。php包含
 
  */
 
defined( "WEB_AUTH" ) || die ( "NO_AUTH" );
 
  
 
/**
 
  * 路由类
 
  *
 
  */
 
     class Route{
 
       
 
        /**
 
         * 模块
 
         */
 
        private $_moudle ;
 
       
 
        /**
 
         * contro控制器
 
         */
 
        private $_conttoller ;  
 
  
 
        /**
 
         * action
 
         */
 
        private $_action ;
 
  
 
        /**
 
         * 地址uri
 
         */
 
        private   $_uri ;
 
       
 
        /**
 
         * 所有mvc资源
 
         */
 
        private $moudle_arr ;
 
       
 
        /**
 
         * 所有路由配置资源
 
         */  
 
        private $route_arr ;
 
           
 
        /**
 
         * 访客角色
 
         */
 
        private $_role = "guest" ;
 
        /**
 
          * the page to direct to if there is not current user
 
          *
 
          * @var unknown_type
 
          */
 
         private $_default = array ( 'module' => 'default' ,
 
                                  'conttoller' => 'index' ,
 
                                  'action' => 'index' );
 
        /**
 
          * the page to direct to if there is not current user
 
          *
 
          * @var unknown_type
 
          */
 
         private $_adminDefault = array ( 'module' => 'admin' ,
 
                                  'conttoller' => 'index' ,
 
                                  'action' => 'login' );     
 
        /**
 
         * 初始化,传入参量
 
         * @return bool
 
         */
 
        public function __construct( $uri = NULL)
 
        {
 
            global $moduleArr , $routeArr ;
 
            $this ->moudle_arr  = $moduleArr ;
 
            $this ->route_arr   = $routeArr ;
 
            $uri == NULL && $uri = $_SERVER [ 'REDIRECT_URL' ];
 
            $this ->_uri   = $uri ;
 
           
 
            /*路由选取*/
 
            $this ->init();
 
        }
 
       
 
        /**
 
         * 实现所有路由相关的认证
 
         *
 
         */
 
        private function init()
 
        {
 
            /**
 
             * 接续url 得到相关参数
 
             */
 
            $this ->parseUri();   //这个函数实现了地址栏的解析
 
  
 
            /**
 
             * 根据路由信息得到 三层控制器
 
             */
 
            $this ->parseRoute();
 
           
 
            /**
 
             * 检查三层控制器 是否在本项目中存在
 
             */
 
            $this ->checkMvc();
 
           
 
            /**
 
             * 解析后台地址uri
 
             */
 
            $this ->getRealUri();
 
            /**
 
             * 得到当前访问者的角色
 
             */
 
            $this ->getRole();
 
           
 
            /**
 
             * 检测权限
 
             */
 
            $this ->checkAuth();
 
            //$this->viewMvc();
 
        }
 
       
 
        /**
 
         * 执行相应的 MCA
 
         *
 
         */
 
        public function run()
 
        {
 
            //$this->checAuth();
 
            $this ->runMvc();
 
            /**
 
             * 下面供调试使用
 
             */
 
           
 
        }

详细讲述一下init() 中得函数功能

$this->parseUri();

1
2
3
4
5
6
7
8
9
10
11
private function parseUri( $uri = NULL)
 
        {
 
            $uri == NULL && $uri = $this ->_uri;
 
            $this ->uriArr = explode ( '/' , $uri );
 
            $this ->uriArr && $this ->uriArr = array_filter ( $this ->uriArr);
 
       }

这个函数把地址简单根据/符号解析 并放在一个数组里面

$this->parseRoute();

这个函数的作用是:给某些请求固定设定好的moudle, conttoller, action,实现上面提到的当用户请求http://tianliangle.com/reg 的时候 就执行"module"=>"default","conttoller"=>"index","action"=>"reg" 对应的模块 而不用请求http://tianliangle.com/default/default/reg 起到缩短地址的作用

$this->checkMvc();

这个函数的主要作用是,根据系统中配置文件及$this->parseRoute();

最终确定唯一的moudle, conttoller, action。

原文:http://www.cnblogs.com/tianliangle/archive/2012/01/03/2310816.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值