In all of my Zend Framework applications that have user logins I like to display if the user is logged in and provide a link to login or logout depending on their current state. In Zend Framework 1 it was easy, I would define a function that would get called at the start of every action (_initView()) and then it would be easy to set any variables that needed to be set on every page.

function _initView(){
     $this->view->currentUser = Application_Model_User::getCurrent();
}

Then in the layout.phtml file you can easily access the variable like so.

$this->user->getName();

In Zend Framework 2 it isn't as easy.

The first problem is that instead of setting view variables through the view object you need to return a ViewModel object from the Controller. The example in the Quick Start shows creating a new ViewModel and passing an array of the variables that you are going to pass. However, there isn't anything saying you can't create the ViewModel in advance and then return it at the end of your actions.

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel;

class Controller extends AbstractActionController{
    protected $_viewModel;

    protected function _initView(){
        $this->_viewModel = new ViewModel();
        $this->_viewModel->user = $user;
    }

public function indexAction(){
        $this->_initView();
        return $this->_viewModel;
    }
}

That way you don't have to manually set every layout wide variable every time you create an action.

The nice thing about ZF2 is that it's even easier to access the variables you pass in the views but in the layouts it's not as simple. It turns out you have to go through an additional step in order to access them.

// this gets the modelView
$children = $this->viewModel()->getCurrent()->getChildren();
$child = $children[0];

$child->user->getName();

I'm still in the starting phase of my ZF2 application but it seems like a good update after you get past the namespaces and all the changes...