Zend Framework 2 display a view within a view

2019-03-30 11:18发布

I have two modules Admin and Login.

I want to display the Login view 'login.phtml' within the admin view 'index.html'

I have the following in the Admin modules indexAction controller

public function indexAction()
{    
    $login = new LoginController();

    $view = new ViewModel(array(
        'theloginform' => $login->loginAction(),
    ));

    return $view;
}

In the LoginAction method in the Login controller I return the ViewModel for the 'login.phtml' file.

public function LoginAction() {
       $view = new ViewModel();
       return $view;
}

The indexAction throws an error as the variable 'theloginform' is an object.

Catchable fatal error: Object of class Zend\View\Model\ViewModel could not be converted to string in...

If i add the following:

$authentication->loginAction()->captureTo('test')

The 'index.phtml' shows a string "content".

I have read that i may need to render the ViewModel before i assign it to the view variable 'theloginform', but i can't seem to get it to work, i have tried the following with no luck.

public function LoginAction() {

    $view = new ViewModel();

    $renderer = new PhpRenderer();
    $resolver = new Resolver\AggregateResolver();
    $map = new Resolver\TemplateMapResolver(array(
            'login'      => __DIR__ . '/../view/login.phtml'

    ));
    $resolver->attach($map);
    $view->setTemplate("login");
    return $renderer->render($view);
}

If get the following error:

Zend\View\Renderer\PhpRenderer::render: Unable to render template "login"; resolver could not resolve to a file

I have even tried adding the DI into the autoload_classmap.php file but still get the same error, i have double checked the login.phtml file is at the correct path:

'/Login/view/login/login/login.phtml' I even copied it to '/Login/src/Login/view/login.phtml'

Very confused have read then re-read the Zend documentation, i just want to pass a view to another view...

4条回答
我只想做你的唯一
2楼-- · 2019-03-30 11:44

In you admin view you have to use the render view helper and echo the script rendered, so you can do echo $this->render($this->theloginform);

查看更多
迷人小祖宗
3楼-- · 2019-03-30 11:45

If you need share some view content you can use partials for that:

$this->partial('partial/login.pthml', array()); //add this to your index view

you can read about them here

You may also find some usefull information: How does Zend Framework 2 render partials inside a module?

查看更多
beautiful°
4楼-- · 2019-03-30 11:58

In ZF 1.x I would likely recommend you build an action helper that is referenced to a view placeholder or a controller plugin that calls back to loginAction for the form logic.

In Zf2 it looks like action helpers have been replaced by controller plugins and seem to be triggered through the event manager and may need to be aware of one or more of the "managers". However the placeholder view helper still exists and even seems somewhat familiar.

I would suggest you look into building/adapting a controller plugin for your login form display that can then be attached to a placeholder view helper. You might be able to get the required functionality with just a view helper, if you're lucky.

I wish I could help more, but I'm still wading through this mess myself.

Good luck.

查看更多
Viruses.
5楼-- · 2019-03-30 12:02

As per this zf2 documentaion page

Write this in login Action:

    public function loginAction()
    {
        return new ViewModel();
    }

And in indexAction :

    $view = new ViewModel(
                  array(
                    //here any thig you want to assign to index view
                  )
                );
    $loginView = new ViewModel(
                  array(
                        //here any thig you want to assign to login view
                    )
                );


    $loginView->setTemplate('moduleName/controllerName/login'); 

    $view->addChild($loginView, 'login');
    return $view

In index.phtml you can just echo login <? echo $this->login ?> where ever you want to display loginView.

查看更多
登录 后发表回答