ZF2 - Get controller name into layout/views

前端 未结 9 1592
逝去的感伤
逝去的感伤 2020-11-30 07:03

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.

U

9条回答
  •  青春惊慌失措
    2020-11-30 07:17

    I created CurrentRoute View Helper for this purpose.

    Install it:

    composer require tasmaniski/zf2-current-route
    

    Register module in config/application.config.php:

    'modules' => array(
        '...',
        'CurrentRoute'
    ),
    

    Use it in any view/layout file:

    $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
    

    You can see full documentation and code https://github.com/tasmaniski/zf2-current-route

提交回复
热议问题