Assigning Values to Views when using $this->renderScript - zend-framework

in order to avoid copy/paste, i can use a unique view for different actions, i can do this using one of these ways
$this->renderScript('index/index.phtml');
$this->view->var = "hello world";
or
$this->_helper->viewRenderer('index');
$this->view->var = "hello world";
if i want to use a different controller i have to use the 1st one, the viewRenderer is ok, but when i use the renderScript it shows nothing like var is not defined. how can i assign values to the view script?
index.phtml would be somthing like this
echo $this->var ;

It should work the way you described it
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
$this->view->var = 'echo me in any action viewscript and i will show you text';
}
public function indexAction()
{
// action body
$this->view->test = 'Don\'t put me here becuase this is not the action that is run';
}
public function testAction()
{
// action body
$this->view->test = 'Hello world';
$this->renderScript('index/index.phtml');
// or $this->_helper->viewRenderer('index');
}
}
in my view (index.phtml)
i have
<?php echo $this->test;?>
When i go to /index/test/ it will show "hello world"...
Also when i do it in another controller it gives me the result
class MyController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// action body
$this->view->test = 'Hello world';
$this->renderScript('index/index.phtml');
}
}

Related

Magento 2: passing parameters from controller to phtml view

There's a way to passing parameters from controller to phtml view using PageFactory class?
Controller code:
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
use MyModule\Services\Service\CurlService;
class Index extends Action implements HttpGetActionInterface
{
protected $resultPageFactory;
protected $curlService;
public function __construct(
Context $context,
PageFactory $resultPageFactory,
CurlService $curlService
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
$this->curlService = $curlService;
}
public function execute()
{
if(isset($_GET['action']) && $_GET['action'] == true)
{
$response = json_decode($this->curlService->response());
switch($response->status)
{
case 'ok': $msgReturn = 'Successfull'; break;
default: $msgReturn = 'Error'; break;
}
}
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu(static::MENU_ID);
$resultPage->getConfig()->getTitle()->prepend(__('Page Title'));
return $resultPage;
}
I need passing $msgReturn using PageFactory to the corresponding view
To add data to your phtml block, simply enter this example in the controller where you set your data.
use Magento\Framework\View\Result\PageFactory;
$page = $this->pageFactory->create();
$block = $page->getLayout()->getBlock('block_alias');
$block->setData('name_var', $data);
And then in the phtml file
$data = $block->getData('name_var');

How to call a function from one module to another Magento 2

I try to call a function from a module A to a module B
here is the module A code
namespace A\Epayment\Model;
class Etransactions
{
public function customPayment{
return "test";
}
and module b code
namespace B\Payment\Controller\Index;
class Payment extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;
protected $_transaction;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\ETransactions\Epayment\Model\Etransactions $transaction
)
{
$this->_pageFactory = $pageFactory;
$this->_transaction = $transaction;
parent::__construct($context);
}
public function execute()
{
echo "Hello World".PHP_EOL;
$foo="a";
echo $foo;
echo $this->_transaction->customPayment();
//echo $this->customPayment();
echo $foo;
exit;
}
}
this code return the "hello world", the first $foo, not the second and doesn't display any error
can someone explain me where is my error ?
EDIT: i didn't change anything but it works fine now.
thanks for the answers anyway
The object you want create the path your are injecting is incorrect.
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\A\Epayment\Model\Etransactions $transaction // changes are here
)
{
$this->_pageFactory = $pageFactory;
$this->_transaction = $transaction;
parent::__construct($context);
}
Kindly use exception handling.
try{
$this->_transaction->customPayment();
}catch(Exception $e){
//log your exception here.
}
In Magento, Helper classes are available to use anywhere (Block, Controller, Model, Observer, View). So you should create a method in helper class and call it anywhere by the following way.
Declar the helper class and method: ModuleA\Epayment\Helper\Data.
<?php
namespace ModuleA\Epayment\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
public function yourHelperMethod()
{
# code...
}
}
Call the method:
$helper = $this->_objectManager->create(ModuleA\Epayment\Helper\Data::class);
$helper->yourHelperMethod();
Note: If the object manager is not injected in your class. Please follow the steps below:
1) declare private property:
private $_objectManager;
2) inject in the constructor to initialize:
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectmanager
) {
$this->_objectManager = $objectmanager;
}
3) use in some method:
public function someMethod() {
$helper = $this->_objectManager->create(ModuleA\Epayment\Helper\Data::class);
$helper->yourHelperMethod();
}

Rendering Custom View in SuiteCRM not working

I have a controller that calls my custom view, but the custom view is not getting rendered.
I cant understand the issue, everything looks correct to me.
Neither is it displaying any errors or warnings.
My module name is SCRV_SSRS_CRM_Reports_View
I have below code in:
custom/modules/SCRV_SSRS_CRM_Reports_View/controller.php
require_once('include/MVC/Controller/SugarController.php');
class SCRV_SSRS_CRM_Reports_ViewController extends SugarController
{
function action_test(){
$GLOBALS['log']->fatal('Am in Controller');
$this->view = "test";
}
}
And in
custom/modules/SCRV_SSRS_CRM_Reports_View/views/view.test.php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('include/MVC/View/views/view.list.php');
class SCRV_SSRS_CRM_Reports_ViewViewtest extends ViewList
{
public function display()
{
echo "HIIII";
$GLOBALS['log']->fatal('Am in View');
}
}
Cant see the view getting rendered when I access it by
http://localhost:8080/dev-crm/index.php?module=SCRV_SSRS_CRM_Reports_View&action=test
I cant see HIIII displayed on screen or the log entry.
Controller should be
class CustomMeetingsController extends SugarController {
$this->view = 'invite';
}
and View should be like
class CustomMeetingsViewinvite extends ViewList {
public function display() {
echo 'hiiiii';
}
}

How to call a method of another controller in zend?

I have a one controller SearchController in Hotel module and i have a one method Search in this controller like below.
public function searchAction()
{
// Code for search
// want to call getlatlng method
}
now i created a one new controller DistanceController in same module and
created a one getlatlng method in distance controller.my getlatlng method like this.
public function getlatlng()
{
$location = $_REQUEST['lat'].','.$_REQUEST['lng'];
return $location;
}
Now i want to call a getlatlng method in searchAction method. which returns a latitude and longitude of the current place.I pass latitude and logitude to getlatlng function using post or get.
So how can i call getlatlng method in searchAction method?
You can call like this, If you are using ZF Version 1.x:
class SearchController extends Zend_Controller_Action
{
public function searchAction() {
echo "search_action_from_SearchController";
require_once ('DistanceController.php');
$distanceCtrl = new DistanceController($this->_request, $this->_response);
$distanceCtrl->xyzAction();
die;
}
}
class DistanceController extends Zend_Controller_Action
{
public function getlatlng() {
echo "getlatlng_from_DistanceController";
die;
}
}
Output:
URL: http://www.example.com/search/search
search_action_from_SearchController
getlatlng_from_DistanceController

how to have two controller actions, one shared view for Zend_Controller_Action Class?

How do you specify a custom view script for a given Controller Action method?
For example:
Class UserGalleryController extends Zend_Controller_Action
{
public function fooAction()
{
$this->view->actionMsg = 'foo';
// (uses foo.phtml automagically)
}
public function barAction()
{
$this->view->actionMsg = 'bar';
//use foo's view script ?????
}
}
I basically want to have one view script (foo.phtml)
Thanks :-)
public function barAction()
{
$this->view->actionMsg = 'bar';
$this->render('foo');
}