如何在zf2中获取控制器和操作名称

时间:2012-08-29 14:07:33

标签: php zend-framework2

在zf1中,我们可以使用

获取控制器和动作名称
$controller = $this->getRequest()->getControllerName();
$action = $this->getRequest()->getActionName();

我们如何在zf2中实现这一目标?

更新: 我试图使用

来获取它们
echo $this->getEvent()->getRouteMatch()->getParam('action', 'NA');
echo $this->getEvent()->getRouteMatch()->getParam('controller', 'NA');

但我收到错误

Fatal error: Call to a member function getParam() on a non-object

我喜欢在__construct()方法中获取它们;

理想情况下,我想检查是否没有定义Action会执行noaction()方法。我会检查使用php方法method_exists。

4 个答案:

答案 0 :(得分:13)

更简单:

$controllerName =$this->params('controller');
$actionName = $this->params('action');

答案 1 :(得分:4)

您无法在控制器__construct()方法中访问这些变量,但您可以使用dispatch方法和onDispatch方法访问它们。

但是如果你想检查是否存在动作,在zf2中已经有一个内置函数用于notFoundAction,如下所示

 public function notFoundAction()
{
    parent::notFoundAction();
    $response = $this->getResponse();
    $response->setStatusCode(200);
    $response->setContent("Action not found");
    return $response;   
} 

但如果您仍想手动执行此操作,则可以使用调度方法执行此操作,如下所示

namespace Mynamespace\Controller;

use Zend\Mvc\Controller\AbstractActionController;

use Zend\Stdlib\RequestInterface as Request;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\Mvc\MvcEvent;

class IndexController extends AbstractActionController 
{

    public function __construct()
    {


    }        

      public function notFoundAction()
    {
        parent::notFoundAction();
        $response = $this->getResponse();
        $response->setStatusCode(200);
        $response->setContent("Action not found");
        return $response;   
    }

    public function dispatch(Request $request, Response $response = null)
    {
        /*
         * any customize code here
         */

        return parent::dispatch($request, $response);
    }
    public function onDispatch(MvcEvent $e)
    {
        $action = $this->params('action');
        //alertnatively 
        //$routeMatch = $e->getRouteMatch();
        //$action = $routeMatch->getParam('action', 'not-found');

        if(!method_exists(__Class__, $action."Action")){
           $this->noaction();
        }

        return parent::onDispatch($e);
    }
    public function noaction()
    {        
        echo 'action does not exits';   
    }
}   

答案 2 :(得分:0)

您将在控制器内的Zf2中获得这样的模块,控制器和动作名称......

'R-203 - TEXT203
Range(Range("A2"), Range("A2").End(xlDown)).Select
    Set rColored = Nothing
    For Each rCell In Selection
        If rCell.Interior.Color = Color203 Then
            If rColored Is Nothing Then
                Set rColored = rCell
            Else
                Set rColored = Union(rColored, rCell)
            End If
        End If
    Next

On Error GoTo skipthispart203:
        rColored.Select
        Selection.Offset(0, 11).Select

        For Each i In Selection
            'i.Value = i.Value & Text
            'i.Value = Text & i.Value
            i.Value = Text203
        Next i
skipthispart203:

    Set rCell = Nothing
    Set rColored = Nothing

答案 3 :(得分:-6)

$controllerName = strtolower(Zend_Controller_Front::getInstance()->getRequest()->getControllerName());
相关问题