How can I use ZF1 $this->action in ZF2?

2020-03-07 07:03发布

问题:

My problem is that in ZF2 missing Action View Helper.
How can I use for example in layout.phtml (or in other view)?

ZF1: $this->action("index", "index") // call IndexController indexAction

ZF2: ???

How can i solve this?


The problem solved!!! ;) http://www.michaelgallego.fr/blog/?p=223

回答1:

First you need to write a custom helper like this:

https://github.com/AlloVince/eva-engine/blob/master/vendor/Eva/View/Helper/Action.php

Maybe you need to change the namespace to fit your project, Then register this helper as a invokable helper service in your module config file:

'view_helpers' => array(
    'invokables' => array(
        'action' => 'Eva\View\Helper\Action',
    ),  
),

Then you could call any controller action in view like this:

$this->action('Engine\Controller\PagesController', 'indexAction');

EDIT:

OP found a nice solution here: http://www.michaelgallego.fr/blog/?p=223. Since he didn't post it as an answer, other users might miss it.



回答2:

Action view helper was not recommended in zf1 and was completely removed in zf2 as bad practice. You should consider changing application design to not rely on such behaviour.