Zend redirect within view helper - php

Hi i have a following script to redirect within view helper
<?php
class Application_View_Helper_ExistUserRev extends Zend_View_Helper_Abstract{
public function existUserRev($params,$user)
{
$businessReviewMapper = new Application_Model_Mapper_BusinessReviewsMapper();
$businessReviewModel = new Application_Model_BusinessReviews();
$result = $businessReviewMapper->userReviewStatus($user>getUserId(),$params['bzid']);
if($result){
$url = 'http://www.akrabat.com';
$this->_helper->redirector->gotoUrl($url);
}
}
}
?>
But it seems that my above redirect seems not working. How can i redirect within view helper of my zend app? Thanks

As you're in a View Helper class, you can't use $this->_helper->redirector->gotoUrl($url);, this is an Action Controller function.
You have to call the redirector in your View Helper.
Try this :
$_redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
$_redirector->gotoUrl($url);

Redirector is a controller ACTION helper, not a View helper, so you should use it from the controller, not from the view.
To redirect from the view (not a good idea BTW, the logic should stay in the controller, not in the view), try using the Zend Action View Helper

This is even simpler then presented so far:
Excerpt from Zend Framework 1.x reference: Writing Custom Helpers
In general, the class should not echo or print or otherwise generate
output. Instead, it should return values to be printed or echoed. The
returned values should be escaped appropriately.
Basically a view helper should return a value, not perform an action.
Action helpers on the other hand can do pretty much anything you need done.
Here is a very simple example to demonstrate the form of using the direct() method in the helper:
<?php
/**
* Simply returns a search form to a placeholder view helper
*
*/
class My_Controller_Action_Helper_Search extends Zend_Controller_Action_Helper_Abstract
{
/**
* #param string $action
* #param string $label
* #param string $placeHolder
* #return \Application_Form_Search
*/
public function direct($action, $label = null, $placeHolder = null)
{
$form = new Application_Form_Search();
$form->setAction($action);
$form->search->setLabel($label);
$form->query->setAttribs(array(
'placeholder' => $placeHolder,
'size' => 20,
));
return $form;
}
}
here is how it's used in a controller to populate a placeholder helper in either a view script or a layout.
public function preDispatch()
{
$this->_helper->layout()->search = $this->_helper->search(
'/index/display', 'Search My Collection!', 'Search Query'
);
}
and in the view script or layout:
<?php echo $this->layout()->search?>
In your case you might use an action helper to establish the values needed to construct the proper url, then you could pass those value to the url() helper or to a helper of your own construction.

Related

How to display dynamic navigation bar from blade template

I have a navigation bar stored in the database, and I have a Controller witch lists the navbar for my template file.
$navbar=/*Query*/
return view('inc.template')->with('nav',$navbar);
I have other pages where I want to use the template with the navigation bar of course, but when I extend the template I get error message 'Undefined variable $nav'. I understand why I get this error message, because I don't returned the variable for the other page. So I need solution for this.. every idea is welcome!.
I have single product page, where I want to include the template with the navigation bar and also I will list the Single Product here.
I know I can copy the query code and paste it to the single product controller, but I believe this is not a good solution (repeating myself).
Thanks in advance for your ideas!
You need to check the View Composer which will help you do what you want : https://laravel.com/docs/8.x/views#view-composers (use the correct Laravel version).
In order to do this, you will need to create a new class that will be your view composer and then register it to the container of Laravel.
In your case, that would be something like this :
<?php
namespace App\Http\View\Composers;
use Illuminate\View\View;
class NavbarComposer
{
/**
* Create a new navbar composer.
*
* #return void
*/
public function __construct()
{
// If you need to do something when instanciating this view composer
}
/**
* Bind data to the view.
*
* #param \Illuminate\View\View $view
* #return void
*/
public function compose(View $view)
{
// here you can add as many variables that your navbar might need
// first parameter is the name of the variable and second the value.
$view->with('navbarData', []);
}
}
To register your view composer you can then do something like this :
View::composer('profile', ProfileComposer::class);
Maybe take a tour to https://www.laracasts.com to learn the basics of Laravel because that's not how to use routes.
I don't really understand your code, but I think this may help you:
If you want to get the variable in your non-yield blades, you can share your variable from controller's constructor, so don't need to add that in all methods. Just add this constructor method in your controller-class like this:
// ADD THIS >>>
public function __construct()
{
View::share('data', 'example');
}
// <<<
// YOUR EXISTING METHOD >>>
public function index()
{
return view('navbar');
}
// <<<
Now you can access $data variable in your blade, which are used in your appropriate page.
Don't forget to use this class in the top of controller's class:
use Illuminate\Support\Facades\View;
The simple answer is: you just need to return everything that belongs to your home.blade.php in the index function of the HomeController. You should do something like this:
public function index(){
$navbar = "Your query to get navbar data";
return view('home',[
'navbar' => $navbar
]);
}
Then call the navbar data into your home.blade.php ( or into the actual navbar.blade.php ) by using foreach.
Note: If you want to call the navbar in multiple views just call the navbar data from all the view returning functions as like index function.
UPDATE
To achieve that you can just do something as:
public function index(){
$navbar = "Your query to get navbar data";
if(count($navbar) == 0){
$navbar = "";
}
$slider = "Your query to get slider data";
if(count($slider) == 0){
$slider = "";
}
return view('home',[
'navbar' => $navbar,
'slider' => $slider,
]);
}

Laravel 3, render only one section (for ajax)

I'd like to reuse my templates and would like to return only one rendered section as an ajax response (html table) which belongs to the "content" section (index.blade.php).
#section('content')
html...
#endsection
I've created another layout called ajax (ajax.blade.php) which contains only:
#yield('content')
My controller:
class Some_Controller extends Base_Controller {
public $restful = true;
public $layout = 'layouts.main';
public function get_index (){
if ( Request::ajax() )
$this->layout = 'layouts.ajax';
$view = View::make('some.index')->with('data', 'shtg');
$this->layout->content = $view;
}
}
It works when I request the route via normal GET request... but when I request it via ajax I get an error:
Attempt to assign property of non-object
on the line containing
$this->layout->content = $view;
I've also tried
return Section::yield('content');
Which returns empty document.
Is there a way to return rendered section? I've searched over the forums and couldn't find anything apart from:
http://forums.laravel.io/viewtopic.php?id=2942
Which uses the same principle and doesn't work for me (I've tried all the variations mentioned on the link above).
Thanks!
You appear to be mixing blade templates with controller templates. If you wish to use controller layouts (my preference) then remove the #section('content') and #endsection, and replace #yield('content') with $content.
However, that is not your entire problem. The following line is picked up by the layout method and converted into a real view...
public $layout = 'layouts.main';
You could easily extend the layout function in your controller, adding a layout_ajax attribute like this...
/**
* The layout used by the controller for AJAX requests.
*
* #var string
*/
public $layout_ajax = 'layouts.ajax';
/**
* Create the layout that is assigned to the controller.
*
* #return View
*/
public function layout()
{
if ( ! empty($this->layout_ajax) and Request::ajax() )
{
$this->layout = $this->layout_ajax;
}
return parent::layout();
}

sending data from action helper to partial view

i want to send some data from Action Helper to view Partial and i am unable to do it, to get the clear picture. here is all the related code i am using.
in my layout.phtml i am using this placeholder. to generate onDemand navigation menu.
<?php echo $this->placeholder('action-navigation'); ?>
so when i need it in my controller or action method i can simply place use this code.
$this->_helper->navigation()->renderActionNavigation();
The Action helper i am using is.
class Zend_Controller_Action_Helper_Navigation extends Zend_Controller_Action_Helper_Abstract
{
private $_view = null;
public function direct()
{
$this->_view = $view = Zend_Layout::getMvcInstance()->getView();
$this->_view->placeholder('action-navigation');
return $this;
}
public function renderActionNavigation()
{
$config = new Zend_Config_Xml(
APPLICATION_PATH.'/configs/navigation.xml', strtolower(
$this->getRequest()->getControllerName().
$this->getRequest()->getActionName()
)
);
$container = new Zend_Navigation($config);
// here i want to send $container to _action-navigation.phtml.
$this->_view->addScriptPath(APPLICATION_PATH.'/layouts/')->render('partials/_action-navigation.phtml');
}
}
this is my view partial _action-navigation.phtml
$this->placeholder('action-navigation')->captureStart();
//i want to get zend_navigation instance. from the above action helper here.
$this->placeholder('action-navigation')->captureEnd();
i have problem sending data from action helper to partial view _action-navigation.phtml how do i do it?
Thank you.
Use partial() instead of render():
$this->_view->partial('partials/_action-navigation.phtml', array('nav' => $container));
And in your partial:
$this->nav // to get your container

Kohana Sessions Issue

Let me try to explain what I want to do here. I am trying to re-write a pet project from Codeigniter 2.x to Kohana 3.2.x.
I have created a Site Template controller (below)
class Controller_Site_Template extends Controller_Template
{
public $template = 'templates/hero';
/**
* The before() method is called before your controller action.
* In our template controller we override this method so that we can
* set up default values. These variables are then available to our
* controllers if they need to be modified.
*/
public function before()
{
parent::before();
if ($this->auto_render)
{
// Initialize empty values
$this->template->title = '';
$this->template->content = '';
$this->template->session = '';
$this->template->styles = array();
$this->template->footer_scripts = array();
$session = Session::instance();
$this->template->session = $session;
}
}
/**
* The after() method is called after your controller action.
* In our template controller we override this method so that we can
* make any last minute modifications to the template before anything
* is rendered.
*/
public function after()
{
if ($this->auto_render)
{
$styles = array(
'assets/css/style.css' => 'screen',);
$footer_scripts = array(
'assets/js/libs/jquery-1.7.1.min.js',
'assets/js/application.js',
);
$this->template->styles = array_merge( $this->template->styles, $styles );
$this->template->footer_scripts = array_merge( $this->template->footer_scripts, $footer_scripts );
}
parent::after();
}
After the login form is submitted I set the session data and I am able to retrieve the session data in the Controllers that extend the Controller_Site_Template but I am unable to retrieve the session data in any of the View files.
The only way I am able to get the session data in the view files is to pass the session data in each controller that extends the Template_Site_Template:
$this->template->content->set_global('session',$this->template->session->as_array());
Is there an easy way to establish and set the session in the template_controller that can be used in all of the controllers, modelc, views rather that using the set_global on each individual controller?
I don't know if I am explaining this well but I am used to the ease of Codeigniter's $this->session->userdata(); function that can be called in any controller, model, and view once it was set.
Thank you in advance for any input on what I am doing incorrectly.
You can set or bind global data to your views with the following
View::bind_global('session', $session);
View::set_global('session', $session);
If you plan to change any data further along the application logic, then use bind.
If no more changes to the data are required, use set.
Edit: oh, the above is just for views and you want it across the entire application.
Just use the Session::instance()->set() and Session::instance()->get() as required across your application rather then assigning it in your application controller.

Yii's magic method for controlling all actions under a controller

Commando need's help from you.
I have a controller in Yii:
class PageController extends Controller {
public function actionSOMETHING_MAGIC($pagename) {
// Commando will to rendering,etc from here
}
}
I need some magic method under Yii CController for controlling all subrequest under /page || Page controller.
Is this somehow possible with Yii?
Thanks!
Sure there is. The easiest way is to override the missingAction method.
Here is the default implementation:
public function missingAction($actionID)
{
throw new CHttpException(404,Yii::t('yii','The system is unable to find the requested action "{action}".',
array('{action}'=>$actionID==''?$this->defaultAction:$actionID)));
}
You could simply replace it with e.g.
public function missingAction($actionID)
{
echo 'You are trying to execute action: '.$actionID;
}
In the above, $actionID is what you refer to as $pageName.
A slightly more involved but also more powerful approach would be to override the createAction method instead. Here's the default implementation:
/**
* Creates the action instance based on the action name.
* The action can be either an inline action or an object.
* The latter is created by looking up the action map specified in {#link actions}.
* #param string $actionID ID of the action. If empty, the {#link defaultAction default action} will be used.
* #return CAction the action instance, null if the action does not exist.
* #see actions
*/
public function createAction($actionID)
{
if($actionID==='')
$actionID=$this->defaultAction;
if(method_exists($this,'action'.$actionID) && strcasecmp($actionID,'s')) // we have actions method
return new CInlineAction($this,$actionID);
else
{
$action=$this->createActionFromMap($this->actions(),$actionID,$actionID);
if($action!==null && !method_exists($action,'run'))
throw new CException(Yii::t('yii', 'Action class {class} must implement the "run" method.', array('{class}'=>get_class($action))));
return $action;
}
}
Here for example, you could do something as heavy-handed as
public function createAction($actionID)
{
return new CInlineAction($this, 'commonHandler');
}
public function commonHandler()
{
// This, and only this, will now be called for *all* pages
}
Or you could do something way more elaborate, according to your requirements.
You mean CController or Controller (last one is your extended class) ?
If you extended CController class like this:
class Controller extends CController {
public function beforeAction($pagename) {
//doSomeMagicBeforeEveryPageRequest();
}
}
you could get what you need

Categories