是否可以在Zend Framework模块中包含图像,CSS和脚本?

时间:2010-03-17 09:16:37

标签: zend-framework

在我的Zend Framework模块中,我的视图需要特定的javascript,CSS和图像。

是否可以将它们包含在模块中,而不必在每个使用该模块的项目中复制它们? (因为“public /”文件夹不是模块的一部分,而是特定于每个应用程序)

如果没有,我该怎么办?

谢谢!

2 个答案:

答案 0 :(得分:0)

“安装方向”包括将“public_html / mymodule /”链接/放入模块文件夹中的子目录,这是最简单的。

我写过一个控制器/动作/路线,过去提供脚本/ css /图像。它并不是非常复杂,但正确处理缓存有点痛苦。我不是说这是一种伟大的方式,我的意思是,你真的不应该花费所有时间来设置zend框架并调度一个动作来给某人一个静态的javascript文件...但是,你可以这样做 AND 告诉他们链接目录。

如果你决定使用它,这里是我写的摘要,它可以提供一些静态内容:

<?php

class MW_Script_Controller_Abstract extends Zend_Controller_Action
{
  protected $_basepath;

  public function publicAction()
  {
    // not initialized
    if (!$this->_basepath) throw new Exception("base path not set");


    if (!($filename = $this->_request->getParam("filename")))
      $filename = $this->_request->getParam(1);

    if (!$filename) throw new Exception("Unknown Filename");

    $fn = realpath($this->_basepath."/".$filename);

    if (!$fn || !is_file($fn)) throw new MW_Controller_404Exception("File not found");

    // determine extension to determine mime-type
    preg_match("#\.([^.]+)$#", $filename, $matches);
    if (!empty($matches[1]) switch (strtolower($matches[1]))
    {
      case "js": 
        $this->getResponse()->setHeader('Content-Type', 'text/javascript'); 
        ini_set('html_errors', 0);
        break;
      case "css": 
        $this->getResponse()->setHeader('Content-Type', 'text/css'); 
        ini_set('html_errors', 0);
        break;
      case "html":
        $this->getResponse()->setHeader('Content-Type', 'text/html');
        break;
      default:
        $this->getResponse()->setHeader('Content-Type', 'text/plain'); 
        ini_set('html_errors', 0);
        break;
    }

    // Disable Layout
    $this->_helper->viewRenderer->setNoRender(true);     
    $this->_helper->layout->disableLayout();

    $mtime = filemtime($fn);

    if ($modsince = $this->getRequest()->getServer('HTTP_IF_MODIFIED_SINCE'))
    {
      $modsince = new Zend_Date($modsince);
      $modsince = $modsince->getTimestamp();

      if ($mtime <= $modsince) {
        $this->getResponse()->setHttpResponseCode(304); 
        return;
      }
    }

    $this->getResponse()->setHeader('Last-Modified', gmdate("D, d M Y H:i:s",$mtime). " GMT");
    readfile($fn);
    // All Done
  }
}

路线:

<?php
class MW_Script_Route extends Zend_Controller_Router_Route_Abstract
{
  /**
   * The Root Path for this "MW_Script_Controller" area
   *
   * @var string a root "path" for this route
   **/
  protected $_rootpath;

  /**
   * Holds user submitted default values for route's variables. Name and value pairs.
   * @var array
   */
  protected $_defaults = array();

  /**
   * Instantiates route based on passed Zend_Config structure
   *
   * @param Zend_Config $config Configuration object
   */
  public static function getInstance(Zend_Config $config)
  {
      $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
      return new self($config->route, $defs);
  }
  /**
   * Stores the basepath for later matching, also stores defaults.  If no "module" is passed in to 
   * the $defaults array,  the "basepath" will be used as a module name, "index" will be used as a 
   * controller, and "public" will be used as a action.
   *
   * @param string $route Map used to match with later submitted URL path
   * @param array $defaults Defaults for map variables with keys as variable names
   * @param array $reqs Regular expression requirements for variables (keys as variable names)
   */
  public function __construct($route, $defaults = array())
  {

      $route = trim($route, '/');
      $this->_defaults = (array) $defaults;
      if (!isset($this->_defaults["module"])) $this->_defaults["module"] = $route;
      if (!isset($this->_defaults["controller"])) $this->_defaults["controller"] = "index";
      if (!isset($this->_defaults["action"])) $this->_defaults["action"] = "public";

      $this->_rootpath = $route;

  }

  public function match($request)
  {
    $path = trim($request->getPathInfo(), '/');
    if (substr($path, 0, strlen($this->_rootpath)) == $this->_rootpath)
    {
      $values = $this->_defaults;
      $values["filename"] = substr($path,strlen($this->_rootpath)+1);
      return $values;
    }
  }

  public function assemble($data = array(), $reset = false, $encode = false)
  {
    if (isset($data["filename"])) $filename = $data["filename"];
    if (isset($data["1"])) $filename = $data["1"];
    return $this->_rootpath . "/" .$filename;
  }

} // END class MW_Script_Controller_Route extends Zend_Controller_Router_Route_Abstract

答案 1 :(得分:0)

最简单的方法是将资产放在public目录中并使用baseUrl()帮助程序。

但是,最好的方法是将所有脚本移动到/public目录。然后你可以换掉'皮肤'。 当然,您必须添加其他规则.htaccess保护.phtml

为此,您必须自定义标准应用程序结构以满足您的需求 有关进一步说明,请参阅my answer in this post