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

2020-03-07 06:48发布

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

2条回答
做自己的国王
2楼-- · 2020-03-07 07:28

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.

查看更多
对你真心纯属浪费
3楼-- · 2020-03-07 07:28

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.

查看更多
登录 后发表回答