I am playing with zend framework's MVC. One thing I found out is that Zend Framework tries to include a view script whenever a controller is called. I can disable it in bootstrap with the following code.
$frontController->setParam('noViewRenderer',true);
However, I have to initialize Zend_View class in a controller method then render a script file myself.
How can I stop including a view script in a controller method so I can disable it if only I want to?
you can disable the view renderer controller helper, with this code in your controller:
public function myAction()
{
$this->_helper->viewRenderer->setNoRender(true);
// from now on, ZF won't search for a matching view script file.
}
The best example would be to use both commands above:
public function myAction() {
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
}
First one disables layout,in general is enabled
application.ini
default
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
$this->_helper->layout->disableLayout();
and second disables view script (.phtml) so exception is not thrown if view script file is not found
$this->_helper->viewRenderer->setNoRender(true);
There are also view helpers that include bouth listened abowe and are not necessary, for example you want to return JSON from array data and not render view element.
public function myAction() {
$this->_helper->json(array());
}
will not render layout nor view script.
Easy, just disable it from within your action.
$this->_helper->layout->disableLayout();
If you aren't talking about layouts, then just add an exit() to your action. Just understand what sort of impact that will have on your application.
Related
I'm Just confused while creating controller in code igniter.
I have two main modules in my project.
menu in header section.
contact us form.
for menu i have created one controller naming devot in devot class i have called all the views for menu, now for contact us form i should go with same controller or i should create new controller.
I'm bit of confused on selection of controllers, how many controller should be there in one application. With only one controller can we complete one application is that possible. ???????????
example:
class SampleController xtends CI_Controlle{
public functin index(){
$this->load->view('header.php');
$this->load->view('yourpage.php');
}
public function page1(){
$this->load->view('header.php');
$this->load->view('yourpage1.php');
}
}
codeigniter Read the Manual
refer
Whenever I create a new action in the zend framework controller using the zf CLI tool it creates the corresponding view file. However, I don't need the view file for every action in the controller.
If I delete that view file manually will it affect my project.xml file or have any other effect on my project ?
If your action does not require a view then you can disable it:-
public function myactionAction()
{
$this->_helper->layout()->disableLayout();//to disable layout
$this->_helper->viewRenderer->setNoRender(true);//to disable view
}
If you want to disable the view/layout for the whole controller then you can put the lines above in the init() method of your controller like this:-
public function init()
{
$this->_helper->layout()->disableLayout();//to disable layout
$this->_helper->viewRenderer->setNoRender(true);//to disable view
}
Once you have done that you can safely delete the view files without affecting anything else.
More details are available in the Action Controller manual.
No you don't need to.
Been a while since i worked with Zend Framework. But if memory serves me well, you have two options here.
1 - $this->_helper->viewRenderer->setNoRender(true);
Which will stop the view being rendered
2- You can simply do what you need to do and call exit() in the end of the of your action.
Hope it helps.
Cheers
So I created a view helper to specialize/simplify some of the functionality of the Flash Messenger helper.
I have been looking around a lot and saw that you should be able to access the FlashMessenger helper through code like this:
$this->view->_helper->flashMessenger->getMessages();
This won't work for me. I can access view but not anything under _helper. My helper class does extend Zend_View_Helper_Abstract. Do you have to explicitly pass in the view from the view script?
The $_helper property of Zend_Controller_Action stores an instance of the Zend_Controller_Action_HelperBroker, so instead of passing it from the view to your helper, you can get an instance of the HelperBroker and then get the FlashMessenger object from there.
// inside your view helper
$messenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
Now you have access to the Flash Messenger from within your view helper. You can do the same from anywhere in the application to get a reference to any Action Helpers.
Also note, if the helper has not yet been created, calling getStaticHelper will initialize it for you.
See Action Helpers - The Helper Broker for more information.
I have my view helpers in the layout like:
$this->viewSearchForm();
that is ok, in all the pages is show it, but what if a have two or tree page where i don't want to show that view helper? is this possible?
something like in an action:
$this->view->disable('viewSearchForm');
You could pass a flag to your view from your controller (init method or specific action).
In your layout you can have something like
if (!isset($this->disableSearchForm)) {
echo $this->view->viewSearchForm();
}
and from your controller send the following
$this->view->disableSearchForm = true;
I think you can't "disable" a view helper. If the helpers you have are in a specific folder, a workaround could be to remove that folder from the helper path using setHelperPath(), but the default view helpers path is never overwritten. See the Zend_View_Helper documentation for details.
Hope that helps,
Using MVC2
Have a master-page that needs to hide certain menus if currently logged in user does not have correct flags set.
Seems like a common problem. Found examples that require that all controllers inherit from a base controller (I have that) and where in constructor of the base controller, the passing of certain parameters to ViewData can occur. This is great and would be easy for me to do, but User.Identity and Request objects are NULL during the construction of base controller.
How do I get to User.Identity of the currently logged in user so that I can query database & modify the ViewData collection accordingly before Master Page view is rendered?
Thanks
You could use child actions along with the Html.Action and Html.RenderAction helpers. So you could have a controller action which returns a view model indicating the currently logged in user info:
public MenuController: Controller
{
public ActionResult Index()
{
// populate a view model based on the currently logged in user
// User.Identity.Name
MenuViewModel model = ...
return View(model);
}
}
and have a corresponding strongly typed partial view which will render or not the menus. And finally inside the master page include the menus:
<%= Html.Action("Index", "Menu") %>
This way you could have a completely separate view model, repository and controller for the menu. You could still use constructor injection for this controller and everything stays strongly typed. Of course there will be a completely different view model for the main controller based on the current page. You don't need to have base controllers or some base view model that all your action should return.