Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework 2 display a view within a view

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...

like image 786
Blu Towers Avatar asked Dec 26 '22 16:12

Blu Towers


1 Answers

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?

like image 126
Aurimas Ličkus Avatar answered Jan 10 '23 04:01

Aurimas Ličkus