本篇,咱们来实现类库自动加载,以及路由解析。php
原文地址:http://www.voidking.com/2017/...linux
常规加载通常使用include或者require,它们最根本的区别在于错误处理的方式不同。 git
include包括并运行指定文件。include一个文件存在错误的话,那么程序不会中断,而是继续执行,并显示一个警告错误。github
include_once的做用和include几乎相同,惟一的差异在于导入以前会检查要导入的文件是否已经被导入过了,若是有的话就不会再次重复导入。windows
require会将目标文件的内容读入,而且把自己替换成这些读入的内容。require一个文件存在错误的话,那么程序就会中断执行了,并显示致命错误。框架
require_once的做用和require几乎相同,惟一的差异在于导入以前会检查要导入的文件是否已经被导入过了,若是有的话就不会再次重复导入。函数
在使用一个文件(类库)的函数以前,咱们须要先使用include或者require,把该文件引入进当前文件,而后才能使用文件中的函数。ui
例如咱们要新建一个route对象。
一、core目录中,新建route.php:this
<?php /** * 路由控制 */ namespace core; class route{ public function __construct(){ echo 'route is ready!'; } }
二、根目录下index.php中,添加:spa
$route = new \core\route();
会报错Fatal error: Class 'coreroute' not found in...
须要改为:
include '\core\route.php'; $route = new \core\route();
或者:
require '\core\route.php'; $route = new \core\route();
bool spl_autoload_register ([ callable $autoload_function [, bool $throw = true [, bool $prepend = false ]]] )
将函数注册到SPL __autoload函数队列中。若是该队列中的函数还没有激活,则激活它们。成功时返回 TRUE,失败时返回 FALSE。
spl_autoload_register的通常用法:
spl_autoload_register(function ($class_name) { require_once $class_name . '.php'; }); $route = new \core\route();
在新建route对象时,class_name也就是\core\route
会传入到spl_autoload_register函数中,该函数的参数是一个回调函数。回调函数拿到class_name,而后进行文件的引入。
也就是说,和常规加载相比,使用自动加载,咱们没必要对每个类库单独进行引入。
上例中,spl_autoload_register的回调函数是一个匿名函数,并且比较简单。下面,咱们来写一个更高级的回调函数。新建aotuload.php,内容以下:
<?php /** * 自动加载类库 */ namespace core; class autoload{ public static function load($class_name){ if(file_exists($class_name.'.php')){ require_once $class_name.'.php'; return true; }else{ echo 'error: unable to load '.$class_name.'.php'; return false; } } }
使用的时候,改为:
include CORE.'/autoload.php'; spl_autoload_register('\core\autoload::load'); $route = new \core\route();
在使用include的时候,会用到php文件系统。在文件系统中访问一个文件有三种方式:
一、相对文件名形式如route.php
。它会被解析为 include_path/route.php
,其中 include_path 表示.;C:/laragon/bin/php/php-5.6.16/PEAR
。
假设当前目录是C:/laragon/www/vkphp
,则该文件名依次被解析为:
C:/laragon/www/vkphp/route.php
C:/laragon/bin/php/php-5.6.16/PEAR/route.php
二、相对路径名形式如core/route.php
,它会被解析为 include_path/core/route.php
。
假设当前目录是C:/laragon/www/vkphp
,则该文件名依次被解析为:
C:/laragon/www/vkphp/core/route.php
C:/laragon/bin/php/php-5.6.16/PEAR/core/route.php
三、绝对路径名形式如/core/route.php
,在linux系统中,它会被解析为/core/route.php
;在windows系统中,它会被解析为 include_path/core/route.php
,和相对路径同样。
绝对路径名形如C:/laragon/www/vkphp/core/route.php
或者C:\laragon\www\vkphp\core\route.php
或者 C:\\laragon\\www\\vkphp\\core\\route.php
,在windows系统中,会被解析为C:/laragon/www/vkphp/core/route.php
。也就是说,windows中斜线和反斜线和双反斜线效果相同。
获取include_path和设置include_path的栗子:
echo get_include_path(); ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.'lib_path/libs'); echo get_include_path();
一、访问地址 http://vkphp.dev/index.php ,此时,咱们看到“helloworld”和“route is ready!”。
二、访问地址 http://vkphp.dev/index.php/in... ,能够看到一样的信息。
三、访问地址 http://vkphp.dev/index/index ,则会报404错误。那么,咱们怎样隐藏掉index.php呢?答案是添加.htaccess。
在项目根目录下,添加.htaccess,内容以下:
Options +FollowSymLinks IndexIgnore */* RewriteEngine on # if a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # otherwise forward it to index.php RewriteRule . index.php
四、访问地址 http://vkphp.dev/index/index ,能够看到和一、2中相同的信息。
<?php /** * 路由控制 */ namespace core; class route{ public $ctrl; public $action; public function __construct(){ //echo 'route is ready!'; /** * 一、隐藏index.php * 二、获取URL中的控制器和方法 */ if(isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] != '/'){ $path = $_SERVER['REQUEST_URI']; $patharr = explode('/',trim($path, '/')); p($patharr); if(isset($patharr[0])){ if($patharr[0] != 'index.php'){ // 省略了index.php $this->ctrl = $patharr[0]; if(isset($patharr[1])){ $this->action = $patharr[1]; } else{ $this->action = 'index'; } }else{ // 没省略index.php if(isset($patharr[1])){ $this->ctrl = $patharr[1]; } if(isset($patharr[2])){ $this->action = $patharr[2]; } else{ $this->action = 'index'; } } }else{ $this->ctrl = 'index'; $this->action = 'index'; } }else{ $this->ctrl = 'index'; $this->action = 'index'; } } }
访问地址 http://vkphp.dev/index/index 或者 http://vkphp.dev/index.php/in... ,便可看到打印出的patharr信息。
<?php /** * 路由控制 */ namespace core; class route{ public $ctrl; public $action; public $params=array(); public function __construct(){ //echo 'route is ready!'; /** * 一、隐藏index.php * 二、获取URL中的控制器和方法 * 三、获取URL中的参数 */ if(isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] != '/'){ $path = $_SERVER['REQUEST_URI']; $patharr = explode('/',trim($path, '/')); //p($patharr); if(isset($patharr[0])){ if($patharr[0] != 'index.php'){ // 省略了index.php $this->ctrl = $patharr[0]; if(isset($patharr[1])){ $this->action = $patharr[1]; } else{ $this->action = 'index'; } $count = count($patharr); $i=2; while($i < $count){ $this->params[$patharr[$i]] = $patharr[$i+1]; $i = $i + 2; } }else{ // 没省略index.php if(isset($patharr[1])){ $this->ctrl = $patharr[1]; } if(isset($patharr[2])){ $this->action = $patharr[2]; } else{ $this->action = 'index'; } $count = count($patharr); $i=3; while($i < $count){ $this->params[$patharr[$i]] = $patharr[$i+1]; $i = $i + 2; } } }else{ $this->ctrl = 'index'; $this->action = 'index'; } }else { $this->ctrl = 'index'; $this->action = 'index'; } p($this->params); } }
访问地址 http://vkphp.dev/index/index/... 或者 http://vkphp.dev/index.php/in... ,便可看到打印出的params信息。
访问地址 http://localhost/vkphp/index.... ,没法正常获取控制器、方法和参数,修改以下:
<?php /** * 路由控制 */ namespace core; class route{ public $ctrl='index'; public $action='index'; public $params=array(); public function __construct(){ //echo 'route is ready!'; /** * 一、隐藏index.php * 二、获取URL中的控制器和方法 * 三、获取URL中的参数 */ if(isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] != '/' ){ $path = $_SERVER['REQUEST_URI']; $patharr = explode('/',trim($path, '/')); }else{ $patharr = array(); } if(isset($_SERVER['HTTP_HOST']) && ($_SERVER['HTTP_HOST'] == 'localhost' || $_SERVER['HTTP_HOST'] == '127.0.0.1') ){ // 去掉项目名称 $patharr = array_slice($patharr,1,count($patharr)-1); } if(isset($patharr[0])){ if($patharr[0] == 'index.php'){ // 去掉index.php $patharr = array_slice($patharr,1,count($patharr)-1); } if(isset($patharr[0])){ $this->ctrl = $patharr[0]; } if(isset($patharr[1])){ $this->action = $patharr[1]; } $count = count($patharr); $i=2; while($i < $count){ if(isset($patharr[$i+1])){ $this->params[$patharr[$i]] = $patharr[$i+1]; } $i = $i + 2; } } p($this->ctrl); p($this->action); p($this->params); } }
https://github.com/voidking/v...
2-php-include-path/)