問題描述
我知道使用 ZF1,您將使用自定義視圖助手檢索模塊/控制器名稱,該助手將獲取單例 frontController 對象并在那里獲取名稱.
I know with ZF1 you would retrieve the module/controller name using custom View Helpers that would get the singleton frontController object and get the name there.
使用 ZF2,因為它們消除了框架的很多單例性質并引入了 DI,我在其中為該模塊中的所有控制器指定了別名......我可以想象我會通過訪問 DI 或也許將當前名稱注入到布局中.
Using ZF2 as they've abolished alot of the singleton nature of the framework and introduced DI where I've specified aliases for all of my controllers within this module... I can imagine I would get it through accessing the DI or perhaps injecting the current name into the layout.
任何人都知道你會怎么做.我想有一百種不同的方法,但是在嗅探了幾個小時的代碼之后,我真的無法弄清楚它現在是如何完成的.
Anyone got any idea how you would do it. I guess there a hundred different ways but after sniffing about the code for a few hours I can't really figure out how its meant to be done now.
我想要控制器名稱的原因是將其作為特定控制器樣式的類添加到主體中.
The reason I wanted the controller name is to add it to the body as a class for specific controller styling.
謝謝,多姆
推薦答案
ZF2 出來了,骨架也出來了.這是在骨架頂部添加的,所以它應該是你最好的例子:
ZF2 is out and so is the skeleton. This is adding on top of the skeleton so it should be your best example:
內部模塊.php
public function onBootstrap($e)
{
$e->getApplication()->getServiceManager()->get('translator');
$e->getApplication()->getServiceManager()->get('viewhelpermanager')->setFactory('controllerName', function($sm) use ($e) {
$viewHelper = new ViewHelperControllerName($e->getRouteMatch());
return $viewHelper;
});
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
實際的 ViewHelper:
The actual ViewHelper:
// Application/View/Helper/ControllerName.php
namespace ApplicationViewHelper;
use ZendViewHelperAbstractHelper;
class ControllerName extends AbstractHelper
{
protected $routeMatch;
public function __construct($routeMatch)
{
$this->routeMatch = $routeMatch;
}
public function __invoke()
{
if ($this->routeMatch) {
$controller = $this->routeMatch->getParam('controller', 'index');
return $controller;
}
}
}
在您的任何視圖/布局中
Inside any of your views/layouts
echo $this->controllerName()
這篇關于ZF2 - 將控制器名稱放入布局/視圖中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!