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
Related
I'm running Laravel 7, and I wonder if it is possible to return a rendered Blade component from a controller, just like you would with a view. I can return the view of the component like the following.
return View::make('components.some-view');
However, I do not have access to any of the data or methods inside the SomeView component class. If I try to use a variable defined in the component, I receive an undefined variable error.
Try this, it works for me in Laravel 8
for example I have App\View\Components\Form\Button component
<?php
class Button extends Component{
public $variable1;
public $variable2;
public function __construct($variable1, $variable2){
$this->variable1 = $variable1;
$this->variable2 = $variable2;
}
public function render(){
// return view('view_component_name');
}
}
?>
& I wants to render that component in controller without view layout
then I can do like this
<?php
use App\View\Components\Form\Button;
class TestController extends Controller{
public function index(Request $request)
$obj = new Button($variable1, $variable2);
$obj->render()->with($obj->data());
}
}
?>
** data function include methods, properties and attributes of Component.
I hope this one helps to you
You can render a blade component from your controller using view function:
//first parameter = Path of your component
//second argument = All your variables that your component receive
$html = view('components.yourComponentName', ['x-variable' => $value]);
return $html;
Create object of component in controller like
$com = new SomeComponent($variable1, $variable2);
//call the render function
$comHtml = $com->render();
Hopefully this will help.
Use YourDirectory\some-view;
$controllerObject = new some-view;
$controllerObject -> variable;
I have page, for example, /about.
In page's view i may set page title: $this->title = "abc" - it works ok.
Also i have Header component in /components with his own view /components/views/Header.php
How could I change my page title from my component's view?
$this->title does not work because I'm in component's view, not page.
Not sure how you are calling your component, but to change the title you need to specify you want to change the current view.
Here is an example, in the view add something like (or use any method you already used but make sure you insert the view as a parameter):
MyComponent::changeTitle($this);
And in your component (whatever method you want to do this):
public static function changeTitle($view)
{
$view->title = 'changed';
}
If this is not related with your situation, please add an example of the view and the component so we can understand better what is the scenario.
Embed the page object into the component. Then change the properties of the page object through the aggregation composition.
The component class would read something like...
class MyComponent extends Component
{
private $pageObject;
public $title;
public function __construct(yii\web\View $view)
{
$this->pageObject = $view;
}
// this would change the title of the component
public function setTitle(string $newTitle)
{
$this->title = $newTitle;
}
public function changePageTitle(string $newTitle)
{
$this->pageObject->title = $newTitle;
}
}
Where, if you're in a view and you want to use your component in that view, you can instantiate it using
$comp = new MyComponent($this);
// where `$this` is the current page object
Now, from the component scope, $this->title = 'bleh'; would change the title of the component while $this->changePageTitle('bleh'); would change the page's title.
I've tried many solutions that had the same questions like mine. But didn't found a working solution.
I have a controller:
event.php
And two views:
event.phtml
eventList.phtml
I use eventList to get data via ajax call so I want to populate both views with a variable named "eventlist" for example.
Normally I use this code for sending a variable to the view:
$this->view->eventList = $events;
But this variable is only available in event.phtml.
How can I make this available for eventlist.phtml? (without adding a second controller)
Edit:
I get this error now
Call to undefined method Page_Event::render()
Function:
private $_event;
public function init(){
$dbTable = new Custom_Model_DbTable_Events();
//Get Events
$this->_event = $dbTable->getEntries($this->webuser->businessId);
$this->index();
}
public function indexAction(){
$this->eventList = $this->_event;
$this->render();
$this->render('eventlist');
}
If I use $this->view->render('event.phtml') and eventlist.phtml it won't pass the data
I'm using zend version 1
You can pass variables to other views using render()
public function fooAction()
{
// Renders my/foo.phtml
$this->render();
// Renders my/bar.phtml
$this->render('bar');
}
Copy and paste this in your controller and rename your controller from event.php to EventController.php
class EventController extends Zend_Controller_Action
{
private $_event;
public function init(){
$dbTable = new Custom_Model_DbTable_Events();
//Get Events
$this->_event = $dbTable->getEntries($this->webuser->businessId);
$this->index();
}
public function indexAction(){
// You're calling the index.phtml here.
$this->eventList = $this->_event;
$this->render('event');
$this->render('eventlist');
}
}
To specify that only written #Daan
In your action:
$this->view->eventList= $events;
$this->render('eventList'); // for call eventList.phtml
In you View use : $this->eventList
You could render it within the view itself (eventList.phtml), rather than within the controller, using the same line of code you used above:
$this->render('event[.phtml]');
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.
Hi i have issue here of calling another controller action to send an mail, here is my code:
user.php
public function followAction()
{
$follow_id = $this->_getParam('id');
$response = "<a href='javascript: void(0)' class='i-wrap ig-wrap-user_social i-follow_small-user_social-wrap'><i class='i i-follow_small-user_social ig-user_social'></i>Stop Following</a>";
notifyEmail() ------> need to call Notity Controller with notifyEmail() Action along with
params
$this->_helper->json($response); ----> return json response
}
NotifyController.php
<?php
class NotifyController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function index()
{
}
public function notifyEmailAction()
{
// rest of email code goes here
}
}
Thanks for help!
You have to move send mails functionality to another place,
and call it in both methods.
Check this thread
Calling member function of other controller in zend framework?
I suggest to create at the path /library a new folder 'My' and in it new file Utilities.php and in that file a new class where you can put all your help methods
class My_Utilities {
// put here your custom help methods
}
You need to auto-load that namespace.In configs/application.ini put
autoloaderNamespaces.my = "My_"
Then you can use namespace My_ and class My_Utilities.
In any case, you can call method form another controller:
include 'NotifyController.php';
$a = new NotifyController($this->_request, $this->_response);
$a->notifyEmailAction();
$this->action('action', 'controller', 'module', 'params')
That view helper walk through frontController and dispatch all plugins again.
I think is not the best solution keep in mind wasting resources
Please try this code
$this->action("action","controller","module")