请教一个代码实现:在hooks里如何实现生成首页静态文件。
思路:在“module_content_after”内容发布后挂钩点中执行生成首页静态代码。
问题:以下代码从“dayrui/Core/Controllers/Home.php”代码中复制过来的,虽然成功生成了index.html文件,但是里边的内容不对,检查了下发现主要是
\Phpcmf\Service::V()->display('index.html');方法并没有渲染出index模板内容,而是渲染了“/dayrui/Core/Views/share_list.html"的内容。
请问各路大神,应该如何修改才能实现内容发布后生成首页静态,谢谢。
附思路实现代码:
\Phpcmf\Hooks::on('module_content_after', function($data, $old) {
// 内容发布或者修改之后
ob_start();
\Phpcmf\Service::V()->assign([
'indexc' => 1,
'fix_html_now_url' => defined('IS_MOBILE') && IS_MOBILE ? SITE_MURL : SITE_URL,
]);
\Phpcmf\Service::V()->assign(\Phpcmf\Service::L('Seo')->index());
\Phpcmf\Service::V()->display('index.html');
$html = ob_get_clean();
// 开启过首页静态时
if (SITE_INDEX_HTML) {
if (defined('IS_MOBILE') && IS_MOBILE) {
// 移动端,当移动端独立域名情况下才生成静态
if (SITE_MURL != SITE_URL) {
file_put_contents(\Phpcmf\Service::L('html')->get_webpath(SITE_ID, 'site', 'mobile/index.html'), $html);
}
} else {
// pc
file_put_contents(\Phpcmf\Service::L('html')->get_webpath(SITE_ID, 'site', 'index.html'), $html);
}
}
});
\Phpcmf\Hooks::on('module_content_after', function($data, $old) { // 内容发布或者修改之后 dr_catcher_data(SITE_URL'index.php', 10); });\Phpcmf\Hooks::on('module_content_after', function($data, $old) { // 内容发布或者修改之后 $html = dr_catcher_data(SITE_URL.'index.php',30); // 开启过首页静态时 if (SITE_INDEX_HTML) { if (defined('IS_MOBILE') && IS_MOBILE) { // 移动端,当移动端独立域名情况下才生成静态 if (SITE_MURL != SITE_URL) { file_put_contents(\Phpcmf\Service::L('html')->get_webpath(SITE_ID, 'site', 'mobile/index.html'), $html); } } else { // pc file_put_contents(\Phpcmf\Service::L('html')->get_webpath(SITE_ID, 'site', 'index.html'), $html); } } });并没有成功生成首页,生成一个0kb的index.html,请大侠明示。\Phpcmf\Hooks::on('module_content_after', function($data, $old) { // 内容发布或者修改之后 dr_catcher_data(SITE_URL'index.php', 10); // pc dr_catcher_data(SITE_MURL'index.php', 20); // yd });pc和移动都有了,两句话就搞定,不用那么多,多了反而无用\Phpcmf\Hooks::on('module_content_after', function($data, $old) { // 内容发布或者修改之后 file_get_contents(SITE_URL'index.php'); });这样写/** * 调用远程数据 * * @param string $url * @param intval $timeout 超时时间,0不超时 * @return string */ function dr_catcher_data($url, $timeout = 0) { // 获取本地文件 if (strpos($url, 'file://') === 0) { return file_get_contents($url); } // curl模式 if (function_exists('curl_init')) { $ch = curl_init($url); if (substr($url, 0, 8) == "https://") { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在 } curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 最大执行时间 $timeout && curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); $data = curl_exec($ch); $code = curl_getinfo($ch,CURLINFO_HTTP_CODE); if (CI_DEBUG && curl_errno($ch)) { log_message('error', '获取远程数据失败['.$url.']:'.curl_error($ch)); } curl_close($ch); if ($code == 200) { return $data; } else { return ''; } } //设置超时参数 if ($timeout && function_exists('stream_context_create')) { // 解析协议 $opt = [ 'http' => [ 'method' => 'GET', 'timeout' => $timeout, ], 'https' => [ 'method' => 'GET', 'timeout' => $timeout, ] ]; $ptl = substr($url, 0, 8) == "https://" ? 'https' : 'http'; $data = @file_get_contents($url, 0, stream_context_create([ $ptl => $opt[$ptl] ])); } else { $data = @file_get_contents($url); } return $data; }