ZF2 - Retrieve navigation in Controller ?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

ZF2 - Retrieve navigation in Controller ?

MichaelB
Hi all,

I need to retrieve my navigation in my controllers (i need breadcrumbs element to be in the JsonModel) for some ajax request.

My navigation is generated by a service class that extend DefaultNavigationFactory. In my Layout, is no problem. I can use $this->navigation()->breadcrumbs()...


So, in my controller, i can retrieve my service like that:
$container = $this->getServiceLocator()->get('NavigationAdmin');


But now, how to retrieve the breadcrumbs or menu with $container ?


Thanks in advance,
Michael
Reply | Threaded
Open this post in threaded view
|

Re: ZF2 - Retrieve navigation in Controller ?

weierophinney
Administrator
On Tue, Jul 30, 2013 at 4:06 AM, MichaelB <[hidden email]> wrote:

> I need to retrieve my navigation in my controllers (i need breadcrumbs
> element to be in the JsonModel) for some ajax request.
>
> My navigation is generated by a service class that extend
> DefaultNavigationFactory. In my Layout, is no problem. I can use
> $this->navigation()->breadcrumbs()...
>
>
> So, in my controller, i can retrieve my service like that:
> $container = $this->getServiceLocator()->get('NavigationAdmin');
>
>
> But now, how to retrieve the breadcrumbs or menu with $container ?

Those are actually two separate things -- the navigation container in
the service manager is a top-level object from the Zend\Navigation
component. The view helper "navigation" gives you access to this
container, but then provides access to additional view helpers -- that
what "breadcrumbs" and "menu" actually are, view helpers.

If you want access to those inside your controller, you will have to
inject them. You can do this by creating a factory for your
controller, and within that factory pulling the ViewHelperManager from
the service manager, and then the individual view helpers.

That said, I'd rethink _why_ you want access to the menu and
breadcrumbs in your controller. Usually, if you start mixing view
logic into controllers, it's a sign that you're mixing concerns that
shouldn't be.

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: ZF2 - Retrieve navigation in Controller ?

Razorblade
I think I had almost the same problem,
the need was to retrieve the breadcrumbs in an asyncronous application.

The current breadcrumbs are found searching the navigation container by the fragment value.

I came up with a solution that actually mixes view and controller logic and I knew it,
in this case, what would be a better solution? Search the container within the view script?

https://gist.github.com/kaiohken1982/6112983

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: ZF2 - Retrieve navigation in Controller ?

MichaelB
In reply to this post by weierophinney
Thanks for your answer!

Actually my goal are to output an ViewModel with the view content and the breadcrumbs, without layout (when XmlHttpRequest).

I use $view->setTerminal($this->getRequest()->isXmlHttpRequest()) to render only the view to work with pushState HTML5 when is available. And i need the breadcrumbs in this case to be include to the content. Is why i asked that question.

And when setTerminal==0 (normal request) the breadcrumbs come from the layout.

Ok i will see to inject ViewHelperManager in my controller, but can you tell me please what is the difference with that way? (i found it after i ask)

...
// Get View Helper Manager in action controller
$viewHelperManager = $this->getServiceLocator()->get('viewHelperManager');

// Get navigation
$navigation = $viewHelperManager->get('navigation');
...


Thanks in advance,
Michael