• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

从本节起开始正式讲述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;
 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
AES加密:PHP与Java互通问题发布时间:2022-07-10
下一篇:
yum安装php和php扩展发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap