1

I've got a script put togheter form other sources, not my own thinking. The reason for me needing to get this to work is to get a drupal top menu into top of an opencart installation to be able to use the superior cms functionalites while letting opencart handle the superior web shop functions.

The problem is that the menu that prints to a cache file from the script uses relative url.s. This of course means that when the menu is required into opencart, which is on a subdomain, the paths show the path of the subdomain. Not good.

EDIT: Ok, I got the code working, got a little confused for a while, what with all $content and $contents everywhere. Changing code below to how it looks now. I needed to change the time interval to 1 second, otherwise the drupal page behaved very oddly.

<?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();
?>

Also, it would be great to be able to get the cached file always be written to the same filename, otherwise it wouldn't do much good since the menu in the shop would't change when the drupal menu does since the change would result in a new filename.

I've looked at other questions here regarding drupal and absolute paths but either I don't understand enough or they are not applicable.

4

1 回答 1

1

当然,您可以将 astr_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 文档来完成,但对于您给出的情况,这应该足够了

至于缓存,你只需将它保存为cache.unique-key.timeopencarts/system/cache/文件夹,然后调用

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

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

于 2012-04-15T11:16:09.220 回答