如何在Zend Framework中切换主题而不是类别?

如何在Zend Framework中切换主题而不是类别?

问题描述:

first, I don't need to "switch" layout.

I'm speaking of CSS theme, that means the layout remains the same.

What I need to do is switch a css class when on certains category.

Category are defined by the route (name & id in request params)

So I think of a ViewHelper, used in my layout.phtml to set a <body class="theme-category"></body>

But I'll to access the request object in my view helper, is it possible? Is it best practice?

Secondly I though to use the same process but pass the parameters in my controller, and fallback on a default one in my helper if none provided.

What are you feedbacks?

首先,我不需要“切换”布局。 p>

我说的是CSS主题,这意味着布局保持不变。 p>

我需要做的是在某些类别上切换css类。 p>

类别由路由定义(请求参数中的名称和ID) p>

所以我想到了一个ViewHelper,在我的 layout.phtml code>中使用 设置&lt; body class =“theme-category”&gt;&lt; / body&gt; code> p>

但是我将在我的视图助手中访问请求对象 , 可能吗? 这是最佳做法吗? p>

其次我虽然使用相同的过程但是在我的控制器中传递参数,如果没有提供,则在我的助手中回退默认值。 p> \ n

你有什么反馈? p> div>

In your layout put this

<?=$this->headLink(); ?>

And in the controllers you can add a specific css file like this

$this->view->headLink()->appendStylesheet("/css/file.css"); 

That will enable you to change the css theme on every controller. P.S. add it in the init function of the controller.

What about a helper method that will just returns the css class, you call this one from your controller, and just pass the css value to the view ?

class YourController {
  public function yourpageAction(){
    // do your stuff, then call your helper
    $this->view->bodyCssClass = $this->setcss();
  }

  protected function setcss(){
    // analyse here all the parameter you wish (route, name, id etc.)
    // and just return the correct css string
    return $css;
  }
}

You may even use the "init()" method to call automatically your "setcss" method (or whatever the name you give it). To go further you may extend the Zend Controller class, and have all you controller extend your specific controller : all pages will inherit this css defining process. And it helps to maintain and read your code.

And in your view, just a simple call :

<body class="<?php echo $this->bodyCssClass;">