Zend确定它是否是视图助手中的首页(或默认页面)

时间:2012-06-18 03:22:04

标签: zend-framework

我正在编写一个自动加载资产的视图助手。我希望它为所有不是子页面的页面自动加载sub.css。我怎么会这样呢?

这是我到目前为止所得到的:

class Zend_View_Helper_CssHelper extends Zend_View_Helper_Abstract {
  /**
   * protected $this->isFront()
   * 
   */
  public function isFront($request='') {
    // Create $request from Zend Controller
    if (!isset($request))
      $request = Zend_Controller_Front::getInstance()->getRequest();
    // If request not set
    if (!empty($request)) {
      // Hard code front page condition
      // @TODO provide more sophisticated way to determine front page
      if ($request->module == 'default' && $request->controller == 'page' && $request->action == 'index') return true;
      else return false;
    }
  }

  public function cssHelper($return=false) {
    // Set folder prefix
    $prefix = '/myprefixpath/assets/css/';
    $file_prefix = '/public/assets/css/';

    $request = Zend_Controller_Front::getInstance()->getRequest();
    // Css autoloading pattern {controller} / {action}
    // create $part_uri workaround because of the shitty routing path
    $part_uri = ASSETS_URL . '/css/' . $request->getControllerName() . '/' . $request->getActionName() . '.css';
    $file_uri = ROOT_DIR . $file_prefix . $request->getControllerName() . '/' . $request->getActionName() . '.css';

    //$file_uri = APPLICATION_PATH . $request->getControllerName() . '/' . $request->getActionName() . '.css';

    //Zend_Debug::Dump($file_uri);
    //Zend_Debug::Dump(file_exists($file_uri));

    if ($return == true) {
      return $part_uri;
    }
    /**
     * Autoload my sub.css file if it's the front page 
     */
    var_dump($this->isFront());
    if ($this->isFront() == false) {
      //var_dump(ASSETS_URL . '/sub.css');
      //$this->view->headLink()->appendStylesheet(ASSETS_URL . '/css/page/sub.css');
    }

    // Find out if the file exists
    if (file_exists($file_uri)) {
       // Workaround due to my local alias 
       // @TODO need to test on dev server
       // $this->view->headStyle()->appendStyle(file_get_contents($file_uri));
       $this->view->headLink()->appendStylesheet($part_uri);
    // If there isn't load the error.css file
    } else {
      if (file_exists(ASSETS_URL . 'error.css')) 
        $this->view->headScript()->appendFile('/' . 'error.css');
    }
    return $this->view->headLink();
  }
}

1 个答案:

答案 0 :(得分:2)

Zend_Controller_Front有方法getDefaultModule()getDefaultControllerName()getDefaultAction(),分别检查默认模块,控制器名称和操作。 您可以使用这些方法来确定索引页面是什么。

样本使用

$defaultModule = Zend_Controller_Front::getInstance()->getDefaultModule();

$defaultControllerName = Zend_Controller_Front::getInstance()->getDefaultControllerName();

$defaultAction = Zend_Controller_Front::getInstance()->getDefaultAction();