这是什么奇怪现象,CSS引起的

#1 tanidea

这个现象是:模板文件里,一个DIV的CSS背景图片引入失败,导致PHP的 get_included_files() 函数记录不同。
下面上的我测试代码。

1. 首先在入口文件 index.php 最后加入 file_put_contents('file',var_export(get_included_files(),true)); 记录本次运行所引入的所有文件
index.php代码
define('APP_DIR', realpath('./'));
require(APP_DIR.'/protected/lib/speed.php');
file_put_contents('file',var_export(get_included_files(),true));//生成的记录文件是 file 没有后缀
2. 新建一个模块,我用 admin 模块测试。protected\controller\admin
3. 新建一个控制器和两个方法,我用user控制器测试
UserController.php代码
class UserController extends Controller{

        function actioncss(){
                $this->display('admin/css.html'); //css.html模板文件
        }
        function actionnocss(){
                $this->display('admin/nocss.html'); //nocss.html模板文件
        }
}
4. 最后新建两个模板文件,放在 protected\view\admin 下面
css.html代码




untitle




有CSS的


nocss.html代码




untitle



没有CSS的


代码很简单,一个php文件,两个html文件,两个模块文件不同之处在于:css.html有css样式指定 bg { background-image:url(./img/bg0.jpg);} 但这个背景图片是不存在的。nocss.html没有任何css样式指定

下面就看测试结果
访问 index.php?m=admin&c=user&a=css
file 文件的记录是 (结果就是:有css的,没有正确显示引入admin模块下的 UserController.php)
array (
  0 => 'D:\\WAMP\\www\\speed\\index.php',
  1 => 'D:\\WAMP\\www\\speed\\protected\\lib\\speed.php',
  2 => 'D:\\WAMP\\www\\speed\\protected\\config.php',
  3 => 'D:\\WAMP\\www\\speed\\protected\\controller\\MainController.php',
  4 => 'D:\\WAMP\\www\\speed\\protected\\controller\\BaseController.php',
  5 => 'D:\\WAMP\\www\\speed\\protected\\include\\functions.php',
  6 => 'D:\\WAMP\\www\\speed\\protected\\tmp\\60155440c01a7c960d42ccd04744cfd5.1466823477.layout.html.php',
  7 => 'D:\\WAMP\\www\\speed\\protected\\tmp\\cd30298c7f199ab9b41324e3c1d6341c.1467940570.main_index.html.php',
)
访问 index.php?m=admin&c=user&a=nocss
file 文件的记录是 (结果就是:没有css的,正确显示引入admin模块下的 UserController.php)
array (
  0 => 'D:\\WAMP\\www\\speed\\index.php',
  1 => 'D:\\WAMP\\www\\speed\\protected\\lib\\speed.php',
  2 => 'D:\\WAMP\\www\\speed\\protected\\config.php',
  3 => 'D:\\WAMP\\www\\speed\\protected\\controller\\admin\\UserController.php',
  4 => 'D:\\WAMP\\www\\speed\\protected\\include\\functions.php',
  5 => 'D:\\WAMP\\www\\speed\\protected\\tmp\\d58c70a96d43aed1dbbdd97b431ec285.1468220112.nocss.html.php',
)

可以看到,同是访问 admin模块 user控制器,CSS有错的,没有引入admin模块下的的文件,没有CSS样式的,才正确显示引入admin模块下的 UserController.php
虽然访问的URL显示结果都正确,但get_included_files() 应该不会记录错吧? 这如何解释?jake.

2016-07-11 15:56:10

#2 jake

define('APP_DIR', realpath('./'));
require(APP_DIR.'/protected/lib/speed.php');
file_put_contents('file'.microtime(true),var_export(get_included_files(),true));//生成的记录文件是 file 没有后缀

测试时候注意你所谓访问一次有生成几个日志文件

另外,麻烦注意一下发帖的标题,谢谢。

2016-07-11 16:08:13

#3 tanidea

不好意思,下次一定注意,没有权限编辑,不然改一下了。

这次测试准确了,我重新解压的框架文件
1. 在config.php里设置 'rewrite'=>null, 不启用伪静态。
2. index.php 文件最后添加你修改过的 file_put_contents('file'.microtime(true),var_export(get_included_files(),true));
3. 在 MainController.php 添加一方法
        function actioncss(){
                $this->display('css.html');
        }
4.在 protected\view 新建一模板文件 css.html




untitle




有CSS的


然后清空 protected\tmp 目录

访问 index.php?c=main&a=css

问题出现了
根目录下生成了两个文件:
file1468229210.9578
file1468229211.1294

\protected\tmp 目录下生成了三个编译过的.php文件,打开看知道
一个是css.html
一个是layout.html
一个是main_index.html

经过分析测试结果得知:程序运行了两次
先运行 index.php?c=main&a=css
再运行 index.php?c=main&a=index

如果把模板文件css.html 中是 css 样式去掉,程序就只运行一次。

这是什么原因,好神奇啊,jake




2016-07-11 17:32:34

#4 jake

tanidea 发表于 2016-7-11 17:32
不好意思,下次一定注意,没有权限编辑,不然改一下了。

这次测试准确了,我重新解压的框架文件
.htaccess应该还在吧,删了吧。
如果服务器本身的rewrite去掉了,那就不会有你说的奇怪的情况了。

注意这里说的是服务器本身的rewrite,比如说apache+htaccess的配置,不是sp框架里面的rewrite = null

这是因为CSS里面访问那张不存在的图片的情况下,服务器接收到这个请求后,发现没有存在文件,根据伪静态规则会访问到你的程序里面的index.php(默认的页面),所以会有多一次访问的情况。

另外,部分浏览器还会访问你的./favicon.ico,是自动的,在没有此文件情况下也会触发服务器的伪静态。

2016-07-11 17:51:52

#5 tanidea

jake 发表于 2016-7-11 17:51
.htaccess应该还在吧,删了吧。
如果服务器本身的rewrite去掉了,那就不会有你说的奇怪的情况了。
是的,.htaccess还在,服务器也启用了rewrite_module。
唉,同一个访问,也会多次触发 rewrite。这样的话,每个文件都要100%准确无误了。几张图片不正确,又多运行几次。
谢谢jake

2016-07-11 18:09:44

#6 jake

是这样没错。所以一般我们正式上线的程序,会有一些处理:

要么就不要rewrite,包括服务器本身rewrite都去掉,这样由服务器本身返回404给不存在的请求,程序不会跑第二遍。

如果要开rewrite,

1. 那么首先框架+服务端都开rewrite,框架不会再接受index.php?c=xxx&a=xxx的请求,只有rewrite后的请求。

2. 并且,一般类似

                '/'          => '/',
                '/'                => 'main/index',
               
这样的配置也会屏蔽掉,路由规则必须是非常准确到具体的路由,而不是匹配c或者a。

当然,这是指比较正式上线的项目。

3. 一定会开启框架的404,返回404header,这样浏览器才知道这不是个真正的页面

2016-07-11 18:25:50

#7 tanidea

授教了,我还是暂时把.htaccess文件删了吧

2016-07-11 18:53:47