/** * Runs the action. */ public function run() { foreach (Yii::app()->log->routes as $route) { if ($route instanceof CWebLogRoute || $route instanceof CFileLogRoute || $route instanceof YiiDebugToolbarRoute) { $route->enabled = false; } } $asset = $_GET['path']; #修改为action读取文件资料 $SAECommon = new SAECommon(); $path = $SAECommon->saedisk_decrypt($asset); if (!file_exists($path)) { throw new CHttpException(404, Yii::t('yii', 'The asset "{asset}" to be published does not exist.', array('{asset}' => $asset))); } // 浏览器根据etag来缓存,增加 date('H') 则为一小时更新 $etag = md5($path + date('d')); header("ETag: {$etag}"); $offset = 60 * 60 * 24; //css文件的距离现在的过期时间,这里设置为一天 $expire = "expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT"; header($expire); $type = CFileHelper::getMimeType($path); header("content-type: {$type}; charset: UTF-8"); //注意修改到你的编码 #header ("cache-control: max-age=$offset,must-revalidate"); header("cache-control: max-age={$offset}"); #header ("Pragma:"); #print_r($_SERVER['HTTP_IF_NONE_MATCH']);die; if (isset($_SERVER['HTTP_IF_NONE_MATCH']) and $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) { #header('HTTP/1.1 304 Not Modified'); header('Etag:' . $etag, true, 304); } else { if (extension_loaded('zlib')) { //检查服务器是否开启了zlib拓展 ob_start('ob_gzhandler'); } //加载文件 //include($path); echo file_get_contents($path); if (extension_loaded('zlib')) { ob_end_flush(); //输出buffer中的内容,即压缩后的css文件 } } exit; }
/** * Publishes a file or a directory. * This method will copy the specified asset to a web accessible directory * and return the URL for accessing the published asset. * <ul> * <li>If the asset is a file, its file modification time will be checked * to avoid unnecessary file copying;</li> * <li>If the asset is a directory, all files and subdirectories under it will * be published recursively. Note, in case $forceCopy is false the method only checks the * existence of the target directory to avoid repetitive copying.</li> * </ul> * * Note: On rare scenario, a race condition can develop that will lead to a * one-time-manifestation of a non-critical problem in the creation of the directory * that holds the published assets. This problem can be avoided altogether by 'requesting' * in advance all the resources that are supposed to trigger a 'publish()' call, and doing * that in the application deployment phase, before system goes live. See more in the following * discussion: http://code.google.com/p/yii/issues/detail?id=2579 * * @param string $path the asset (file or directory) to be published * @param boolean $hashByName whether the published directory should be named as the hashed basename. * If false, the name will be the hash taken from dirname of the path being published and path mtime. * Defaults to false. Set true if the path being published is shared among * different extensions. * @param integer $level level of recursive copying when the asset is a directory. * Level -1 means publishing all subdirectories and files; * Level 0 means publishing only the files DIRECTLY under the directory; * level N means copying those directories that are within N levels. * @param boolean $forceCopy whether we should copy the asset file or directory even if it is already published before. * This parameter is set true mainly during development stage when the original * assets are being constantly changed. The consequence is that the performance * is degraded, which is not a concern during development, however. * This parameter has been available since version 1.1.2. * @return string an absolute URL to the published asset * @throws CException if the asset to be published does not exist. */ public function publish($path, $hashByName = false, $level = -1, $forceCopy = false) { if (isset($this->_published[$path])) { return $this->_published[$path]; } else { if (($src = realpath($path)) !== false) { #修改为action读取文件资料 $SAECommon = new SAECommon(); $path = $SAECommon->saedisk_encrypt($src); $path_test = Yii::app()->createUrl('site/assets', array('path' => $path)); //createUrl 后自动转义,再转换回/ $path_test = str_replace('%2F', '/', $path_test); return $this->_published[$path] = $path_test; } } throw new CException(Yii::t('yii', 'The asset "{asset}" to be published does not exist.', array('{asset}' => $path))); }