zf增加.html的后缀,即可以实现静态化的插件

之前是.php后缀的页面,如何只需把后缀改成.html,像:http://www.gudalu.com/index.php,访问:http://www.gudalu.com/index.html 就会自成生成静态文件呢?

前段时间古大陆的cpu有点高,处理txt章节不给,决定搞成这种自动静态,zend framework很方便,三步搞定

1.创建一个ZendEx_Plugin_Html的插件,用来处理写html

_filePath = $filePath;
	}
	public function dispatchLoopShutdown()
    {
		$htmlContent = $this->_response->__toString();
		$pathinfo = pathinfo($this->_filePath);
		!is_dir($pathinfo['dirname']) && mkdir($pathinfo['dirname'], 0777, true);
		file_put_contents($filePath, $htmlContent);
	}
}

1. 在入口把所有.html的请求全部转发到HtmlController中~

	$router = $front->getRouter();
	$htmlRoute = new Zend_Controller_Router_Route_Regex(
		'[\d\w\/]+\.html',
		array(
			'module' => 'default',
			'controller' => 'html',
			'action'     => 'handle'
		)
	);

 

2.HtmlController注册ZendEx_Plugin_Html插件,这里可以对uri进行处理

_request->getRequestUri();
		$requestUri = parse_url($requestUri);
		$requestPath = $requestUri['path'];
		// --- 通过条件定位对应的参数来静态化数据
		$module = 'default';
		$controller = $action = '';
		$params = array();
		if (preg_match('/^\/index.html$/', $requestPath, $rs)) {
			$controller = 'index';
			$action = 'index';
		}
		// --- 文章页
		if (preg_match('/^\/file\/([\d]+)\/index\.html$/', $requestPath, $rs)) {
			$id = $rs[1];
			$controller = 'file';
			$action = 'view';
			$params = array('id' => $id);
		}
		if (empty($controller) || empty($action)) {
			//$this->_showError('无法解析地址');
			$this->_closeView();
			$this->_response->setHeader('Status', '404 Not Found', true);
			return;
		}
		
		$this->_forward($action, $controller, $module, $params);
		$this->_closeView();
		$filePath = str_replace('_app', '_www', APP_PATH) . $requestPath;
		// --- 给更低的运行级别,在render之后运行
		$helperHtml = new ZendEx_Plugin_Html();
		$helperHtml->setFilePath($filePath);
		$helperHtml->setRequest($this->_request)->setResponse($this->_response);
		$this->getFrontController()->registerPlugin($helperHtml);
	}
}

 

此条目发表在PHP分类目录。将固定链接加入收藏夹。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据