add conditional logic to controller in zend framework - zend-framework

i want to add a conditional statement to my layout that tests:
the controller param in the url
the existence of a zend_auth()
what's the best way to achieve that? i have tried testing the $this->_getParam('controller') in the layout but got an error. i could just set that variable in all the controllers but that seems kind of dumb. how to best set a variable that i could use later from the layout with some conditional logic? or should i instead add my conditional logic that is inside a view helper and then loaded into the layout?

Edit The controller shouldn't be a URL parameter, unless you are doing some very strange routing. If you were getting a GET (or POST) variable, you would use ->getParam() on the request object, Zend_Controller_Front::getInstance()->getRequest(), as used below. But the controller is a separate property of that request object.
This is the auth part:
$loggedIn = Zend_Auth::getInstance()->hasIdentity();
This is the controller part:
$controller = Zend_Controller_Front::getInstance()->getRequest()->controller;

Related

Symfony best way to store search parameters set by a form

I'm looking for the best way to store parameters set by a form.
The form is generated by an external controller called in my base.html.twig
{{ render(controller('AcmeBundle:FooFilter:index', {'originalRequest': app.request })) }}
I tried to store the form posted datas in the session.
My goal is to use these parameters in others controllers to filter my sql queries. But when I'm try to use the params stored in session there are not updated until the request completly done.
What is the best way to dynamically store parameters from a form?
You need to do the treatment on the form in the controller method called with the route.
You should separate AcmeBundle:FooFilter:index in 2 methods, like AcmeBundle:FooFilter:pre_index and AcmeBundle:FooFilter:index.
AcmeBundle:FooFilter:pre_index should be static, so you can call AcmeBundle:FooFilter:pre_index(app.request); in the other controllers, in order to update the $_SESSION variable.

Nested View in Theme Views using Fuelphp

I have working with Fuelphp and done some tiny apps, Now i am moving to bigger one, now i am stuck with this.
I have enabled theme in fuelphp and it is working perfectly, In my App there is a Top Nav bar, In Nav bar there is 3 drop down notification system like facebook & also search option. So i have created top_nav partial. I want to make the search and notification system in partial inside partial, to making it more modular, so i created another partial of top_nav_search and top_nav_notif . Both partials need some variables to transfer from controller, How i do that. My variables are passing to top_nav only. Not top_nav_search or top_nav_notif.
How can i add partial inside the partial.
Answer Found:
using View Class will not work with theme. It will search for APP/View folder.
we have to use
echo Theme::instance()->view('partials/partial_file')->set_safe('var', $this->get('var'));
found working
You can use set_global on your view, and then the exposed variable will be available everywhere.
$view->set_global('some_data', $some_data);
// instead of
$view->some_data = $some_data;
Also, when using set_partial, you can specify a View instance instead of the path for the view. That way you can pass variables directly into the View instance specified to set_partial. Though I'm not sure it solves your problem. Still, here's an example:
$view = View::forge('nav/search');
$view->some_data = $some_data;
$theme->set_partial('top_nav_search', $view);
Edit: I wrote View::forge, but more appropriate would've been to write $theme->view in this example. Like this:
$view = $theme->view('nav/search');
$view->some_data = $some_data;
$theme->set_partial('top_nav_search', $view);

silverstripe shortcode return dynamic template

I followed this instruction to be able to include a variable in the Content Field (HTMLEditor) of a Page - so that the variable can be replaced with other content:
http://www.balbuss.com/mini-introduction-to-shortcodes/
I want to display a list of dataobjects within the $Content.
Sadly the DummyHandler in the instruction is static. So I canĀ“t access the Controller in it, to let him do something (generate the list.)
Is there a solution to access the controller in a static function or maybe is there another better way to put a variable in the $Content.
Thx,
Florian
Controller::curr() is probably what you are after?
To use in conjunction with Controller::hasCurr(), as no controller would mean an error when using Controller::curr()
See https://github.com/silverstripe/sapphire/blob/3.0/control/Controller.php#L384

How do I avoid repetition when passing variables from the Controller/Action to the Layout

I am currently working on a project developed using Zend Framework, based on the structure of my web page design I have reached a point where I have to pass a small number of variables to my layout from each Controller/Action. These variables are:
<?php Zend_Layout::getMvcInstance()->assign('pageId', 'page1'); ?>
<?php Zend_Layout::getMvcInstance()->assign('headerType', '<header id="index">'); ?>
The reason for passing this information is firstly, I pass the page id as the multi column layout may change depending on the content being displayed, thus the page id within the body tag links the appropriate CSS to how the page should be displayed. Secondly I display a promotional jQuery slider only on the index page, but I need the flexibility to have it displayed on potentially multiple pages in case the wind changes and the client changes their mind.
My actual question: Is there a more appropriate method of passing this information to the Layout that I am overlooking?
I am not really questioning whether the information has to be sent, rather is there some Zend Framework feature that I have, in my haste, overlooked which would reduce the amount of repetitive redundant code which may very well be repeated in multiple Actions within the same controller?
You could turn that logic into an action helper than you can call from your controllers in a more direct way. You could also make a view helper to accomplish the same thing but view helpers usually generate data for the view rather than set properties.
// library/PageId.php
class Lib_PageId extends Zend_Controller_Action_Helper_Abstract
{
public function direct($title, $pageId, $headerType)
{
$view = $this->getActionController()->view;
$view->headTitle()->append($title);
$view->pageId = $pageId;
$view->headerType = $headerType;
}
}
In your controller actions you can now do this:
$this->_helper->PageId('Homepage', 'page1', 'index');
// now pageId and headerType are available in the view and
// Homepage has been appended to the title
You will also need to register the helper path in your Bootstrap like this:
protected function _initActionHelpers()
{
Zend_Controller_Action_HelperBroker::addPrefix('Lib');
}
Doing it like that can reduce the amount of repetitive code and remove needing to assign the values from the view. You can do it in the controller very quickly. You can also have default values in the case that the helper hasn't been called.
You shoudn't really be passing anything from the view to the layout, for a start the view should be included IN the layout, not the other way around.
So, setting your page title should be done using similar code to what you have, but inside the controller action being called:
$this->view->headTitle()->append('Homepage');
And the other two issues - you need to rethink as I stated to begin with. Maybe you're misunderstanding the layout/view principle? If you include the different views per action, then you simply change the div id when needed, and include the header for your banner only in the index.phtml file.

Grails - Render a template by email

I have a controller's method which renders a template.
This works fine to render the template within my .gsp view.
I am also using the mail-plugin, and I would like to used the same controller's function to render the template by email, hence populating some email with it.
I know how to do that from a .gsp view via Ajax request but do not know any way to do that from within a controller or a service.
The idea would be to use my controller's action more like a function, take the rendered teplate and populate my email with it.
Also, my controller's action needs to have some 'params' properties to work properly.
Any suggestion most welcome.
Regards,
You can use the render tag( http://grails.org/doc/latest/ref/Tags/render.html ) can be used to return a string.
I would move whatever logic you have in your controller that is reusable into a service, and then use this to return a model, then you can simply call this via:
def model = myService.method( ... )
def emailContent = g.render( template: 'mytemplate', model: model)