Zend Framework 2 : Disable Layout in specific Module
Based on definition of module in Zend Framework 2 : “all related code and assets that solve a specific problem”, sometime, for example, we need a module that provide Ajax only, and no need a layout in all actions. So, we can do a trick to reduce code redundancy in every controller/actions.
namespace YourModule; use Zend\Mvc\MvcEvent; class Module { public function onBootstrap(MvcEvent $e) { $sharedEvents = $e->getApplication()->getEventManager()->getSharedManager(); $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) { $result = $e->getResult(); if ($result instanceof \Zend\View\Model\ViewModel) { $result->setTerminal($e->getRequest()->isXmlHttpRequest()); //if you want no matter request is, the layout is disabled, you can //set true : $result->setTerminal(true); } }); } }
How about all module ? attach into Zend\Mvc\Controller\AbstractActionController
namespace YourModule; use Zend\Mvc\MvcEvent; class Module { public function onBootstrap(MvcEvent $e) { $sharedEvents = $e->getApplication()->getEventManager()->getSharedManager(); $sharedEvents->attach('Zend\Mvc\Controller\AbstractActionController','dispatch', function($e) { $result = $e->getResult(); if ($result instanceof \Zend\View\Model\ViewModel) { $result->setTerminal($e->getRequest()->isXmlHttpRequest()); //if you want no matter request is, the layout is disabled, you can //set true : $result->setTerminal(true); } }); } }
Remember, you should return ViewModel() in every action in your module.
Hi, xdebug doesn’t stop at callback breakpoint, why?
Thank you for your tutorials. I will be your subscriber.
I’m not familiar with xdebug, sorry. You’re welcome
Ok, it was my fault. Everything is all right.
I have one question. How can I inject to all Controllers a new field without define setters and getters in every Controllers? I want to simply set entityManager (Doctrine2) in my Controllers untill dispatch after Controller instance is create. Can i use automagic method? I don’t wanna use $this->getServiceLocator()->get(’em’) all the time. I don’t wanna extends my Controllers with parent class where is entityManager defined. Maybe an event before Controller is a good place for it? Event driven architecture is great but i don’t know how can i use it.
Bests.
maybe like this :
and in Controller :
Hey samsonasik!
Thank you for all the help that you have done on your blog! I am not sure how I will survive learning ZF2 without you as well as the contributors to the framework.
I am currently following your posts on github as well. much easier for me to find the scripts that I needed to start off with the work I am currently doing.
But often I get to see some codes that you have not put on Github, so I copy and pasted yours and just referenced the page where I took it from. I hope that’s alright with you?
ok, no problem. You’re welcome
It works what i expected perfectly! Thank you so much! It also works if public $em is not defined in controller.
How do you think, is there any method to set this service in config arrays?
You’re welcome. I suggest to type public $em, it’s make code more testable and solid. maybe via ‘di’ Zend\Di ( personally, i’m not fan of dic which ‘inject’ class with config array in array in array definition), but i think it will be more complicated.
do yolu know where i can find documentation of how to use \Zend\File\Transfer\Transfer? tanks
No. but you can contribute to zf2 docs here : https://github.com/zendframework/zf2-documentation/
Maybe a simple and stupid question.. how can i unescape the label in form? tanks
strip the setLabel(). please read the docs : http://zf2.readthedocs.org/en/latest/modules/zend.form.elements.html
yes i know where the documentation is and i read it but i don’t find how to not escape the label in helper like this echo $this->formRow($form->get(‘title’)) . PHP_EOL;
if i do like this $name = $form->get(‘special’);
echo $formLabel->openTag() . $this->formInput($name) . $name->getOption(‘label’);
echo $this->formElementErrors($name);
echo $formLabel->closeTag();
everityng is fine the label aren’t escaped but i ended with a lot of markup and then whats the catch to use zend
just write your custom formLabel.
sound a little stupid to not be a flag or something, if you need a little markup in label like a span.. ok tanks. i ended modify zend till find a solution
custom, don’t modify the core, for ex :
Register it in ServiceManager :
this code don’t solve the problem this method is for label tag not what is inside, i see for the most of inputs that helper render something like
label txt so call that method only in textarea inputs
Must extend other class i don’t know what class must extend and what method must rewrite
it’s work with something like echo html_entity_decode($this->formRow($form->get(‘element’))) i write this here maybe someone else need because i see some of people asked on the google and not anybody answered. It’s a little bit stranhe to decode what is encode already alittle bit overhead but when you need it is ok. tanks for the answers.
You should override __invoke function of FormLabel :
Remember, you should return ViewModel() in every action in your module.
why is this needed. my action by default accesses my corresponding template file.
because by ViewModel, you can setTerminal.
cool thanks
You’re welcome 😉
Hi Samsonic,
I wonder if you can help me here… I have a base service file which I am using to try and load the Doctrine 2 service.. I followed your on onBootstrap suggestion on loading Doctrine2 into a controller:
public function onBootstrap(MvcEvent $e)
{
$sharedEvents = $e->getApplication()->getEventManager()->getSharedManager();
$sm = $e->getApplication()->getServiceManager();
$sharedEvents->attach(‘Zend\Mvc\Controller\AbstractActionController’,’dispatch’,
function($e) use ($sm) {
$controller = $e->getTarget();
$controller->em = $sm->get(’em’);
}, 100
);
}
This worked great.. But is there a way I can do this in a service file instead rather than using controller? I have a few files that extend from this service file. Any help would be appreciated. Right now, all I am getting is Fatal error: Call to a member function createQueryBuilder(). Which suggests that my entity variable in my service file is null. This is the code for my service file.
entityManager = $manager;
return $this;
}
/**
* @return \Doctrine\ORM\EntityManager
*/
public function getEntityManager()
{
if (null === $this->entityManager) {
$this->entityManager = $this->getServiceLocator()->get(‘doctrine.entitymanager.orm_default’);
}
return $this->entityManager ;
}
/**
* @param ServiceLocatorInterFace $serviceLocator
*/
public function setServiceLocator(ServiceLocatorInterFace $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
}
/**
* @returns ServiceLocatorInterFace
*/
public function getServiceLocator()
{
/*echo “
“;
exit;*/
return $this->serviceLocator;
}
}
The service file that extends the base service looks like this:
entityManager->createQueryBuilder();
$query = $qb->select(‘a.id_attendee, a.title, a.name’)
->from(‘CPortal\Entity\AttendeesEntity’,’a’)
->setFirstResult(0)
->setMaxResults(10);
$attendees = $query->getQuery()->getResult();
return $attendees;
}
}
Finally my controller looks like so:
getAttendees($this->em);
return new ViewModel(
array(
‘attendees’ => $attendees
)
);
}
}
Thanks again… I hope I am not asking for too much so any help would be welcome as to where I am going wrong… Superb tutorial btw.
can you place your code under sourcecode tag ? see this http://en.support.wordpress.com/code/posting-source-code/
BaseService.php
AttendeesService.php file which extends the BaseService.php file
AttendeesController.php file where a new instance of the AttendeesService is created.
I hope this helps, as all I get is a Fatal error: Call to a member function createQueryBuilder() in the AttendeesService.php file…
Thanks
with IoC concept, you should register service manager it first, so you call it by servicelocator. I think you should read about service manager first at this : https://samsonasik.wordpress.com/2013/01/02/zend-framework-2-cheat-sheet-service-manager/
One thing, your getAttendees() function is not pass anything. see again getAttendees() function in AttendeesService.
Thanks Samson… Just out of curiosity, was there a reason why I couldn’t call the Doctrine 2 from the service file but in a controller, this could easily be done?
check my post https://samsonasik.wordpress.com/2013/01/02/zend-framework-2-cheat-sheet-service-manager/ , see initializers / abstract_factories section.
[…] https://samsonasik.wordpress.com/2012/12/02/zend-framework-2-disable-layout-in-specific-module/ 2) […]
[…] also provides no answers. After a bit of cursing my frustration leads me to Google which yields Zend Framework 2 : Disable Layout in specific Module. OK that was a waste of time, but at least it works… except.. the obsessive compulsive part […]
Can i disable the layout in a particular page?
this should be help http://www.masterzendframework.com/views/change-layout-controllers-actions-zend-framework-2