【问题标题】:Need help print cache of drupal menu and convert relative paths of links to absolute ones需要帮助打印 drupal 菜单的缓存并将链接的相对路径转换为绝对路径
【发布时间】:2012-04-26 23:23:46
【问题描述】:

我有一个脚本来自其他来源,而不是我自己的想法。我需要让它工作的原因是在 opencart 安装的顶部安装一个 drupal 顶部菜单,以便能够使用高级 cms 功能,同时让 opencart 处理高级网上商店功能。

问题是从脚本打印到缓存文件的菜单使用相对 url.s。这当然意味着当需要菜单进入子域上的opencart时,路径显示子域的路径。不好。

编辑:好的,我得到了代码,有一段时间有点困惑,到处都是 $content 和 $contents。将下面的代码更改为现在的样子。我需要将时间间隔更改为 1 秒,否则 drupal 页面的行为非常奇怪。

<?php  
  $cache_time = 1; // Time in seconds to keep a page cached  
  $cache_folder = 'cache'; // Folder to store cached files (no trailing slash)  
  $cache_filename = $cache_folder.md5($_SERVER['REQUEST_URI']);
  // Location to lookup or store cached file  
  //Check to see if this file has already been cached  
  // If it has get and store the file creation time
  $cache_created  = (file_exists($cache_file_name)) ? filemtime($cache_filename) : 0;

  if ((time() - $cache_created) < $cache_time) {
    readfile($cache_filename);
    // The cached copy is still valid, read it into the output buffer
    die();
  }  
?>

<?php ob_start(); // Turns on output buffering ?>
<?php $contents = ob_get_contents(); ?>
<!-- //Stores the contents of the buffer in a variable as a string -->

<?php if ($page['header_menu']): ?>
<div id="header-menu-wrapper">
<div id="header-menu">
<?php print render($page['header_menu']); ?>
</div>
</div>
<?php endif; ?>

<?php
  $contents = ob_get_contents();
  $contents = str_replace('href="','href="http://yourdomain.com', $contents);
  file_put_contents($cache_filename, $contents());
  ob_end_flush();
?>

另外,如果能够将缓存文件始终写入相同的文件名,那就太好了,否则它不会有多大好处,因为商店中的菜单不会随着 drupal 菜单的变化而改变,因为更改将导致新的文件名。

我在这里查看了有关 drupal 和绝对路径的其他问题,但要么我不够了解,要么它们不适用。

【问题讨论】:

    标签: drupal url menu opencart absolute


    【解决方案1】:

    当然,您可以只使用 str_replace() 来解决 url 问题,将域放入其中。所以在您的脚本结束时

    <?php  
    $content = ob_get_contents();
    $content = str_replace('href="','href="http://yourdomain.com/', $content);
    file_put_contents($cache_filename, $content);  
    ob_end_flush();  
    ?>
    

    如果您想获得真正的技术性,您可以使用正则表达式甚至 DOM 文档来完成,但对于您给出的情况应该就足够了

    至于缓存,只需要在opencarts/system/cache/文件夹中保存为cache.unique-key.time,然后调用

    $menu_var = $this->cache->get('unique-key');
    

    如果不为空/false则打印$menu_var,否则生成

    【讨论】:

    • SWEET!好的,我得到了代码,并且得到了绝对路径。非常感谢 :) 我不明白关于唯一键等的事情,以及 opencart 出现在图片中的位置。缓存文件保存在 drupal 安装的根目录中,我需要将它放入我在 opencart 中使用的主题中。因此,除了导入实际的缓存文件之外,我真的没有尝试对 opencart 做更多的事情。所以,我需要做的是确保它在 drupal 的根目录中始终是相同的名称,并且它会覆盖自己。再次感谢您的帮助:)
    • 啊,我明白了。没问题。如果解决了,请点击我的答案旁边的勾:)
    • 我尝试点击,但由于我的声誉不存在,我无法投票。还是您的意思是要点击其他内容?
    • 它旁边(上方)应该有一个勾号,但我不确定这是否仅适用于高于特定评级的用户。两种方式都没有问题:)
    猜你喜欢
    • 2017-08-01
    • 2011-11-07
    • 2011-05-02
    • 2016-12-14
    相关资源
    最近更新 更多