English 中文(简体)
ZF2 - 进入布局/调查
原标题:ZF2 - Get controller name into layout/views

我与ZF1了解,你将利用海关观察器检索模块/控制器名称,这些用户将获得单一吨前主计长的反对,并在那里获得名字。

利用ZF2,当它们撤销了框架单一州性质的一个位置,并引进了DI,在此模块内,我为我的所有控制人员指定了别处...... 我可以想象一下,我会通过接触移民发展倡议,或者也许把目前的名字注入到布局。

任何人都想到你会怎样做。 我猜测有100种不同的方式,但经过几个小时对法典进行解密之后,我可以确切地说明它现在打算如何做。

我想要控制者名字的理由是,把它作为特定控制者ty平的类别添加到身体上。

Thanks, Dom

最佳回答

ZF2已经结束,也就是如此。 这使你成为最好的榜样:

模块内.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);
}

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;
        }
    }
}

在您的任何意见/说明中

echo $this->controllerName()
问题回答

This would be a solution I got to work with zf2 beta5

module/MyModule/Module.php

namespace MyModule;

use ZendMvcModuleRouteListener;
use MyModuleViewHelper 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());
        });
    }
    ...
}

module/MyModule/src/MyModule/View/Helper.php

namespace MyModuleView;

use ZendViewHelperAbstractHelper;

class Helper extends AbstractHelper
{

    protected $route;

    public function __construct($route)
    {
        $this->route = $route;
    }

    public function echoController()
    {
        $controller = $this->route->getParam( controller ,  index );
        echo $controller;
    }
}

In any viewfile

$this->myviewalias()->echoController();

请不要在<条形码>上扩大<条码>(<>>>>>>,否则,请在<条码>上使用<条码>。 实际帮助者没有变化,但你们有以下法规:

public function getViewHelperConfig()
{
   return array(
          factories  => array(
             ControllerName  => function ($sm) {
               $match = $sm->getServiceLocator()->get( application )->getMvcEvent()->getRouteMatch();
               $viewHelper = new ApplicationViewHelperControllerName($match);
               return $viewHelper;
            },
         ),
   );
}

Short Code here :

$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));

我想从导航菜单中获取现有的模块/控制器/名称,但除了执行海关观察器和查阅该软件外,别无选择,即在这里张贴。

<?php
namespace ApplicationViewHelper;

use ZendViewHelperAbstractHelper;

/**
 * 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 );

Hope this helps someone.

在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();
}

I created CurrentRoute View Helper for this purpose.

附上:

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

参看充分的文件和代码

最容易的方式:

$request = Zend_Controller_Front::getInstance()->getRequest();

$this->controllerName = $request->getControllerName();
$this->actionName = $request->getActionName();
$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));

www.un.org/Depts/DGACM/index_spanish.htm 泽德-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,
        );
}

它为我工作。 我希望,这也将有助于你。 感谢提出这一问题。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签