我知道使用ZF1,您将使用自定义View Helpers检索模块/控制器名称,这将获得单例frontController对象并获取其名称。
使用ZF2,因为他们废除了很多框架的单独性质,并引入了DI,我为这个模块中的所有控制器指定了别名...我可以想象我会通过访问DI或也许将当前名称注入布局。
任何人都知道你会怎么做。我想有一百种不同的方法,但在嗅了几个小时的代码后,我无法弄清楚它现在的意义如何。
我想要控制器名称的原因是将它作为特定控制器样式的类添加到正文中。
谢谢,Dom
答案 0 :(得分:15)
ZF2已经出局,骨架也是如此。这是在骨架上添加,所以它应该是你最好的例子:
Inside Module.php
public function onBootstrap($e)
{
$e->getApplication()->getServiceManager()->get('translator');
$e->getApplication()->getServiceManager()->get('viewhelpermanager')->setFactory('controllerName', function($sm) use ($e) {
$viewHelper = new View\Helper\ControllerName($e->getRouteMatch());
return $viewHelper;
});
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
实际的ViewHelper:
// Application/View/Helper/ControllerName.php
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
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;
}
}
}
在任何视图/布局中
echo $this->controllerName()
答案 1 :(得分:7)
这将是我使用zf2 beta5
的解决方案模块/ MyModule的/ Module.php
namespace MyModule;
use Zend\Mvc\ModuleRouteListener;
use MyModule\View\Helper as MyViewHelper;
class Module
{
public function onBootstrap($e)
{
$app = $e->getApplication();
$serviceManager = $app->getServiceManager();
$serviceManager->get('viewhelpermanager')->setFactory('myviewalias', function($sm) use ($e) {
return new MyViewHelper($e->getRouteMatch());
});
}
...
}
模块/ MyModule的/ SRC / MyModule的/视图/ Helper.php
namespace MyModule\View;
use Zend\View\Helper\AbstractHelper;
class Helper extends AbstractHelper
{
protected $route;
public function __construct($route)
{
$this->route = $route;
}
public function echoController()
{
$controller = $this->route->getParam('controller', 'index');
echo $controller;
}
}
在任何视图文件中
$this->myviewalias()->echoController();
答案 2 :(得分:5)
而不是在onBootStrap()
中扩展Module.php
,您可以使用getViewHelperConfig()
(也在Module.php
中)。实际的帮助程序没有改变,但您可以使用以下代码创建它:
public function getViewHelperConfig()
{
return array(
'factories' => array(
'ControllerName' => function ($sm) {
$match = $sm->getServiceLocator()->get('application')->getMvcEvent()->getRouteMatch();
$viewHelper = new \Application\View\Helper\ControllerName($match);
return $viewHelper;
},
),
);
}
答案 3 :(得分:3)
短代码:
$this->getHelperPluginManager()->getServiceLocator()->get('application')->getMvcEvent()->getRouteMatch()->getParam('action', 'index');
$controller = $this->getHelperPluginManager()->getServiceLocator()->get('application')->getMvcEvent()->getRouteMatch()->getParam('controller', 'index');
$controller = array_pop(explode('\', $controller));
答案 4 :(得分:2)
我想在导航菜单partial中访问当前的模块/控制器/路由名称,除了实现自定义视图帮助程序并访问它之外没办法,我想出了以下内容,我在这里发布。
<?php
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
/**
* View Helper to return current module, controller & action name.
*/
class CurrentRequest extends AbstractHelper
{
/**
* Current Request parameters
*
* @access protected
* @var array
*/
protected $params;
/**
* Current module name.
*
* @access protected
* @var string
*/
protected $moduleName;
/**
* Current controller name.
*
* @access protected
* @var string
*/
protected $controllerName;
/**
* Current action name.
*
* @access protected
* @var string
*/
protected $actionName;
/**
* Current route name.
*
* @access protected
* @var string
*/
protected $routeName;
/**
* Parse request and substitute values in corresponding properties.
*/
public function __invoke()
{
$this->params = $this->initialize();
return $this;
}
/**
* Initialize and extract parameters from current request.
*
* @access protected
* @return $params array
*/
protected function initialize()
{
$sm = $this->getView()->getHelperPluginManager()->getServiceLocator();
$router = $sm->get('router');
$request = $sm->get('request');
$matchedRoute = $router->match($request);
$params = $matchedRoute->getParams();
/**
* Controller are defined in two patterns.
* 1. With Namespace
* 2. Without Namespace.
* Concatenate Namespace for controller without it.
*/
$this->controllerName = !strpos($params['controller'], '\\') ?
$params['__NAMESPACE__'].'\\'.$params['controller'] :
$params['controller'];
$this->actionName = $params['action'];
/**
* Extract Module name from current controller name.
* First camel cased character are assumed to be module name.
*/
$this->moduleName = substr($this->controllerName, 0, strpos($this->controllerName, '\\'));
$this->routeName = $matchedRoute->getMatchedRouteName();
return $params;
}
/**
* Return module, controller, action or route name.
*
* @access public
* @return $result string.
*/
public function get($type)
{
$type = strtolower($type);
$result = false;
switch ($type) {
case 'module':
$result = $this->moduleName;
break;
case 'controller':
$result = $this->controllerName;
break;
case 'action':
$result = $this->actionName;
break;
case 'route':
$result = $this->routeName;
break;
}
return $result;
}
}
为了访问布局/视图中的值,这里是我的工作方式。
1. $this->currentRequest()->get('module');
2. $this->currentRequest()->get('controller');
3. $this->currentRequest()->get('action');
4. $this->currentRequest()->get('route');
希望这有助于某人。
答案 5 :(得分:1)
在zf2 beta4中,它以这种方式制作:
public function init(ModuleManager $moduleManager)
{
$sharedEvents = $moduleManager->events()->getSharedManager();
$sharedEvents->attach('bootstrap', 'bootstrap', array($this, 'onBootstrap'));
}
public function onBootstrap($e)
{
$app = $e->getParam('application');
// some your code here
$app->events()->attach('route', array($this, 'onRouteFinish'), -100);
}
public function onRouteFinish($e)
{
$matches = $e->getRouteMatch();
$controller = $matches->getParam('controller');
var_dump($controller);die();
}
答案 6 :(得分:1)
我为此创建了 CurrentRoute View Helper。
安装它:
composer require tasmaniski/zf2-current-route
在 config / application.config.php 中注册模块:
'modules' => array(
'...',
'CurrentRoute'
),
在任何视图/布局文件中使用它:
$this->currentRoute()->getController(); // return current controller name
$this->currentRoute()->getAction(); // return current action name
$this->currentRoute()->getModule(); // return current module name
$this->currentRoute()->getRoute(); // return current route name
您可以查看完整的文档和代码https://github.com/tasmaniski/zf2-current-route
答案 7 :(得分:0)
$this->getHelperPluginManager()->getServiceLocator()->get('application')
->getMvcEvent()->getRouteMatch()->getParam('action', 'index');
$controller = $this->getHelperPluginManager()->getServiceLocator()
->get('application')->getMvcEvent()->getRouteMatch()
->getParam('controller', 'index');
$controller = explode('\\', $controller);
print_r(array_pop($controller));
答案 8 :(得分:0)
在Zend-3框架中的控制器中获取控制器/动作名称
private function getControllerActionName()
{
$currentController = $this->getEvent()->getRouteMatch()->getParam('controller', 'index');
$explode_controller = explode('\\', $currentController);
$currentController = strtolower(array_pop($explode_controller));
$currentController = str_replace('controller', '', $currentController);
$currentAction = strtolower($this->getEvent()->getRouteMatch()->getParam('action', 'index'));
return array(
'controller' => $currentController,
'action' => $currentAction,
);
}
对我有用。希望对您有帮助。感谢您提出这个问题。