|
Hi,
How can I access the controller's name and action in the layout in ZF2? Thanks! |
|
I'm stuck
![]() Anyone? |
|
Hi,
In your controller test ;) var_dump($this->getEvent()-> getRouteMatch()); Regards, 2012/5/11 cmple <[hidden email]> > I'm stuck > > Anyone? > > -- > View this message in context: > http://zend-framework-community.634137.n4.nabble.com/ZF2-Access-controller-name-action-from-layout-tp4624104p4627351.html > Sent from the Zend Framework mailing list archive at Nabble.com. > > -- > List: [hidden email] > Info: http://framework.zend.com/archives > Unsubscribe: [hidden email] > > > -- Pierre RAMBAUD - I N S T A N T | L U X E - 40 rue d'Aboukir - 75002 Paris - France Web : www.instantluxe.com |
Thanks Pierre, But this means that I'll have to assign(return) the controller/action variables to the view in every controller action, is there a way to directly get those from inside of the layout.phtml or possibly assign the variables in Module.php to the view? here is a ZF1 example of what I need: <?php // layout.phtml $controller = Zend_Controller_Front::getInstance()->getRequest()->getControllerName(); $action = Zend_Controller_Front::getInstance()->getRequest()->getActionName(); ?> |
|
I finally solved it..
![]() // Module.php public function init(Manager $moduleManager) { $sharedEvents = $moduleManager->events()->getSharedManager(); $sharedEvents->attach('bootstrap', 'bootstrap', array($this, 'initializeView'), 100); $sharedEvents->attach('Application', 'dispatch', function($e) { $controller = $e->getTarget(); $route = $controller->getEvent()->getRouteMatch(); $controller->getEvent()->getViewModel()->setVariables(array( 'controller' => $route->getParam('controller'), 'action' => $route->getParam('action'), )); },100); } // layout.phtml <?php echo "{$controller}/{$action}; "; ?> |
|
Careful, I would not just blindly overwrite them. Leave the ones that are currently set, or make that logic configurable :)
Marco Pivetta http://twitter.com/Ocramius     http://marco-pivetta.com    On 18 May 2012 15:37, cmple [via Zend Framework Community] <[hidden email]> wrote: I finally solved it.. |
Not sure what you mean, can you give me a code example? |
|
I simply mean that you should check as following (pseudocode):
$viewModel = $controller->getEvent()->getViewModel(); if (!$viewModel->getVariable('controller')) { $viewModel->setVariable('controller', $route->getParam('controller')); } Marco Pivetta http://twitter.com/Ocramius http://marco-pivetta.com On 18 May 2012 17:50, cmple <[hidden email]> wrote: > > Marco Pivetta wrote > > > > Careful, I would not just blindly overwrite them. Leave the ones that are > > currently set, or make that logic configurable :) > > > > Marco Pivetta > > > > http://twitter.com/Ocramius > > > > http://marco-pivetta.com > > > > > > > > On 18 May 2012 15:37, cmple [via Zend Framework Community] < > > ml-node+s634137n4644518h78@.nabble> wrote: > > > >> I finally solved it.. > >> > >> // Module.php > >> public function init(Manager $moduleManager) { > >> > >> $sharedEvents = $moduleManager->events()->getSharedManager(); > >> $sharedEvents->attach('bootstrap', 'bootstrap', array($this, > >> 'initializeView'), 100); > >> > >> $sharedEvents->attach('Application', 'dispatch', function($e) { > >> $controller = $e->getTarget(); > >> $route = $controller->getEvent()->getRouteMatch(); > >> > >> $controller->getEvent()->getViewModel()->setVariables(array( > >> 'controller' => $route->getParam('controller'), > >> 'action' => $route->getParam('action'), > >> )); > >> },100); > >> } > >> > >> // layout.phtml > >> <?php echo "{$controller}/{$action}; "; ?> > >> > >> ------------------------------ > >> If you reply to this email, your message will be added to the > discussion > >> below: > >> > >> > http://zend-framework-community.634137.n4.nabble.com/ZF2-Access-controller-name-action-from-layout-tp4624104p4644518.html > >> To unsubscribe from Zend Framework Community, click > >> here< > ; > >> . > >> NAML< > http://zend-framework-community.634137.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> > ; > >> > > > Not sure what you mean, can you give me a code example? > > -- > View this message in context: > http://zend-framework-community.634137.n4.nabble.com/ZF2-Access-controller-name-action-from-layout-tp4624104p4644681.html > Sent from the Zend Framework mailing list archive at Nabble.com. > > -- > List: [hidden email] > Info: http://framework.zend.com/archives > Unsubscribe: [hidden email] > > > |
Oh, got it! thanks!! |
|
can u please paste your working module.php code
i m having same problem adding zend translate variable to view model |
just replace the following function: public function init(Manager $moduleManager) { $sharedEvents = $moduleManager->events()->getSharedManager(); $sharedEvents->attach('bootstrap', 'bootstrap', array($this, 'initializeView'), 100); $sharedEvents->attach('Application', 'dispatch', function($e) { $controller = $e->getTarget(); $route = $controller->getEvent()->getRouteMatch(); $viewModel = $controller->getEvent()->getViewModel(); $variables = $viewModel->getVariables(); if ( false === isset($variables['controller'])) { $viewModel->setVariable('controller', $route->getParam('controller')); } if ( false === isset($variables['action']) ) { $viewModel->setVariable('action', $route->getParam('action')); } },100); } make sure your namespace is identical to the event <?php namespace Application; ... $sharedEvents->attach('Application', 'dispatch'... |
|
im getting this error :
Fatal error: Uncaught exception 'Zend\Stdlib\Exception\InvalidCallbackException' with message 'Invalid callback provided; not callable' in D:\xampp\htdocs\ttacounting\vendor\ZendFramework\library\Zend\Stdlib\CallbackHandler.php on line *82 *Zend\Stdlib\Exception\InvalidCallbackException: Invalid callback provided; not callable in D:\xampp\htdocs\ttacounting\vendor\ZendFramework\library\Zend\Stdlib\CallbackHandler.php on line *82 *my code : https://gist.github.com/2726585* * actually this line make the error : $sharedEvents->attach('bootstrap', 'bootstrap', array($this, 'initializeView'), 100); > -- ________________ Sincerely Sina Miandashti www.InTheMix.ir & www.InvisionPower.ir Admin |
Do you have the initializeView($e) function in your Module.php? |
|
no
On Fri, May 18, 2012 at 11:36 PM, cmple <[hidden email]> wrote: > > sina miandashti wrote > > > > im getting this error : > > > > Fatal error: Uncaught exception > > 'Zend\Stdlib\Exception\InvalidCallbackException' with message 'Invalid > > callback provided; not callable' in > > > D:\xampp\htdocs\ttacounting\vendor\ZendFramework\library\Zend\Stdlib\CallbackHandler.php > > on line *82 > > *Zend\Stdlib\Exception\InvalidCallbackException: Invalid callback > > provided; > > not callable in > > > D:\xampp\htdocs\ttacounting\vendor\ZendFramework\library\Zend\Stdlib\CallbackHandler.php > > on line *82 > > > > > > *my code : > > > > https://gist.github.com/2726585* > > > > > > * > > actually this line make the error : > > > > $sharedEvents->attach('bootstrap', 'bootstrap', array($this, > > 'initializeView'), 100); > > > > > >> > > > > > > -- > > ________________ > > Sincerely > > Sina Miandashti > > www.InTheMix.ir & www.InvisionPower.ir Admin > > > Do you have the initializeView($e) function in your Module.php? > > -- > View this message in context: > http://zend-framework-community.634137.n4.nabble.com/ZF2-Access-controller-name-action-from-layout-tp4624104p4644950.html > Sent from the Zend Framework mailing list archive at Nabble.com. > > -- > List: [hidden email] > Info: http://framework.zend.com/archives > Unsubscribe: [hidden email] > > > -- ________________ Sincerely Sina Miandashti www.InTheMix.ir & www.InvisionPower.ir Admin |
// Module.php .. public function initializeView($e) { $app = $e->getParam('application'); $basePath = $app->getRequest()->getBasePath(); $locator = $app->getLocator(); $renderer = $locator->get('Zend\View\Renderer\PhpRenderer'); $renderer->plugin('url')->setRouter($app->getRouter()); $renderer->doctype()->setDoctype('HTML5'); $renderer->plugin('basePath')->setBasePath($basePath); } |
|
In reply to this post by cmple
Hello Roman
I have asked myself the same question: How can I pass basic, standard variables to the View that should be available on every section of the module. I your case, you just want the names of controller and action in the View. I also want some more standard variables (locale, uri etc). I have come up with a solution that involves a custom \Zend\Mvc\Controller\ActionController that is then used by the Controllers. Please take a look at this solution: https://gist.github.com/2792133 I am looking at your solution right now. What are the advantages of these two approaches? Jonathan Maron On Thu, May 10, 2012 at 7:43 PM, cmple <[hidden email]> wrote: > Hi, > > How can I access the controller's name and action in the layout in ZF2? > > Thanks! > > -- > View this message in context: http://zend-framework-community.634137.n4.nabble.com/ZF2-Access-controller-name-action-from-layout-tp4624104.html > Sent from the Zend Framework mailing list archive at Nabble.com. > > -- > List: [hidden email] > Info: http://framework.zend.com/archives > Unsubscribe: [hidden email] > > -- List: [hidden email] Info: http://framework.zend.com/archives Unsubscribe: [hidden email] |
|
Hi Jonathan,
2012/5/26 Jonathan Maron <[hidden email]> > Hello Roman > > I have asked myself the same question: How can I pass basic, standard > variables to the View that should be available on every section of the > module. I your case, you just want the names of controller and action > in the View. I also want some more standard variables (locale, uri > etc). > > I have come up with a solution that involves a custom > \Zend\Mvc\Controller\ActionController that is then used by the > Controllers. > > Please take a look at this solution: > > https://gist.github.com/2792133 > > I am looking at your solution right now. > > What are the advantages of these two approaches? > > Jonathan Maron In your approach you always need to use $this->view for returning a view object. If you use another module from someone else, s/he probably isn't using your own action controller, so there you don't have those variables. For very simple parameter injection I'd go with the Module.php listener approach. It's independent from whatever you do with your controllers and thus much safer. Keep in mind this can go wrong for perhaps CLI requests or http requests which do not use the PhpRenderer but instead return a JSON response. If you have more complex variables required in your view script, you could write a view helper for that. Your uri parameters is an excellent example for that imho. -- Jurian Sluiman |
|
Hello Jurian
Thank you very much for your comments. At the moment, I am still playing with (read: learning) ZF2 for dummy projects. I hope that by the time the first RC comes our, I can start think about using it for client work. > In your approach you always need to use $this->view for > returning a view object. I agree, this is a significant disadvantage. > For very simple parameter injection I'd go with the > Module.php listener approach. I will code the same functionality using this approach. And then compare them side-by-side. > If you have more complex variables required in > your view script, you could write a view helper > for that. Your uri parameters is an excellent > example for that imho. I agree with you here too. There are many ways to skin a cat. At the moment, I trying to find them, so I can make an informed decision about which one is most suitable to my applications. Again, thanks for your feedback. Jonathan Maron On Sat, May 26, 2012 at 10:00 AM, Jurian Sluiman <[hidden email]> wrote: > Hi Jonathan, > > 2012/5/26 Jonathan Maron <[hidden email]> >> >> Hello Roman >> >> I have asked myself the same question: How can I pass basic, standard >> variables to the View that should be available on every section of the >> module. I your case, you just want the names of controller and action >> in the View. I also want some more standard variables (locale, uri >> etc). >> >> I have come up with a solution that involves a custom >> \Zend\Mvc\Controller\ActionController that is then used by the >> Controllers. >> >> Please take a look at this solution: >> >> https://gist.github.com/2792133 >> >> I am looking at your solution right now. >> >> What are the advantages of these two approaches? >> >> Jonathan Maron > > > In your approach you always need to use $this->view for returning a view > object. If you use another module from someone else, s/he probably isn't > using your own action controller, so there you don't have those variables. > > For very simple parameter injection I'd go with the Module.php listener > approach. It's independent from whatever you do with your controllers and > thus much safer. Keep in mind this can go wrong for perhaps CLI requests or > http requests which do not use the PhpRenderer but instead return a JSON > response. > > If you have more complex variables required in your view script, you could > write a view helper for that. Your uri parameters is an excellent example > for that imho. > -- > Jurian Sluiman -- List: [hidden email] Info: http://framework.zend.com/archives Unsubscribe: [hidden email] |
|
Hello Jurian
> If you have more complex variables required in > your view script, you could write a view helper > for that. Your uri parameters is an excellent > example for that imho. To solve the uri issue, I have come up with this ViewHelper: https://gist.github.com/2794307 Where it gets its Request via DI 'di' => array ( 'instance' => array ( 'My\View\Helper\Uri' => array ( 'parameters' => array ( 'request' => 'Zend\Http\PhpEnvironment\Request' ) ) ) ) I prefer this solution too. Thanks for your feedback. Jonathan Maron On Sat, May 26, 2012 at 10:37 AM, Jonathan Maron <[hidden email]> wrote: > Hello Jurian > > Thank you very much for your comments. At the moment, I am still > playing with (read: learning) ZF2 for dummy projects. I hope that by > the time the first RC comes our, I can start think about using it for > client work. > >> In your approach you always need to use $this->view for >> returning a view object. > > I agree, this is a significant disadvantage. > >> For very simple parameter injection I'd go with the >> Module.php listener approach. > > I will code the same functionality using this approach. And then > compare them side-by-side. > >> If you have more complex variables required in >> your view script, you could write a view helper >> for that. Your uri parameters is an excellent >> example for that imho. > > I agree with you here too. > > There are many ways to skin a cat. At the moment, I trying to find > them, so I can make an informed decision about which one is most > suitable to my applications. > > Again, thanks for your feedback. > > Jonathan Maron > > > > > > On Sat, May 26, 2012 at 10:00 AM, Jurian Sluiman > <[hidden email]> wrote: >> Hi Jonathan, >> >> 2012/5/26 Jonathan Maron <[hidden email]> >>> >>> Hello Roman >>> >>> I have asked myself the same question: How can I pass basic, standard >>> variables to the View that should be available on every section of the >>> module. I your case, you just want the names of controller and action >>> in the View. I also want some more standard variables (locale, uri >>> etc). >>> >>> I have come up with a solution that involves a custom >>> \Zend\Mvc\Controller\ActionController that is then used by the >>> Controllers. >>> >>> Please take a look at this solution: >>> >>> https://gist.github.com/2792133 >>> >>> I am looking at your solution right now. >>> >>> What are the advantages of these two approaches? >>> >>> Jonathan Maron >> >> >> In your approach you always need to use $this->view for returning a view >> object. If you use another module from someone else, s/he probably isn't >> using your own action controller, so there you don't have those variables. >> >> For very simple parameter injection I'd go with the Module.php listener >> approach. It's independent from whatever you do with your controllers and >> thus much safer. Keep in mind this can go wrong for perhaps CLI requests or >> http requests which do not use the PhpRenderer but instead return a JSON >> response. >> >> If you have more complex variables required in your view script, you could >> write a view helper for that. Your uri parameters is an excellent example >> for that imho. >> -- >> Jurian Sluiman -- List: [hidden email] Info: http://framework.zend.com/archives Unsubscribe: [hidden email] |
| Powered by Nabble | Edit this page |
