Hi am trying to add custom helper throughout my application
Have done following steps
index.php
$view = new Zend_View();
$view->addHelperPath('My/View/Helper', 'My_View_Helper');
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
Helper class in My/View/Helper
class My_View_Helper_Common extends Zend_View_Helper_Abstract
{
public function example()
{
return "ok";
}
}
now calling in view index.phtml
$this->example()
am getting this error
Uncaught exception 'Zend_View_Exception' with message 'script 'error/error.phtml' not found in path (.\application\views\scripts\)' in C:\xampp\htdocs\wyfixture\library\Zend\View\Abstract.php:924
Stack trace:
#0 C:\xampp\htdocs\wyfixture\library\Zend\View\Abstract.php(827): Zend_View_Abstract->_script('error/error.pht...')
#1 C:\xampp\htdocs\wyfixture\library\Zend\Controller\Action\Helper\ViewRenderer.php(903): Zend_View_Abstract->render('error/error.pht...')
#2 C:\xampp\htdocs\wyfixture\library\Zend\Controller\Action\Helper\ViewRenderer.php(924): Zend_Controller_Action_Helper_ViewRenderer->renderScript('error/error.pht...', NULL)
#3 C:\xampp\htdocs\wyfixture\library\Zend\Controller\Action\Helper\ViewRenderer.php(963): Zend_Controller_Action_Helper_ViewRenderer->render()
#4 C:\xampp\htdocs\wyfixture\library\Zend\Controller\Action\HelperBroker.php(277): Zend_Controller_Action_Helper_ViewRenderer->postDispatch()
#5 C:\xampp\htdocs\wyfixture\library\Zend\Controller\Action.php(523):
please help me
In addition to Vikas answer.
To call more than one method in a view helper you can use code like this:
In My/View/Helper/Example.php
class My_View_Helper_Example extends Zend_View_Helper_Abstract
{
public function example()
{
return $this;
}
public function foo()
{
return 'foo';
}
public function bar()
{
return 'bar';
}
public function __toString()
{
return $this->foo();
}
}
In you views:
echo $this->example()->foo() // prints foo
echo $this->example()->bar() // prints bar
echo $this->example() // prints foo
Seems like you have two problems here:
Your 'application/views/scripts/error/error.phtml' is missing. You can restore it and you'll get more accurate exception message at once.
Your helper class should contain a method named after the helper.
So, in your case it's file My/View/Helper/Example.php with the following body
class My_View_Helper_Example extends Zend_View_Helper_Abstract {
public function example() {...}
}
Then you'll be able to call it from the view with
$this->example()
Related
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();
}
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';
}
}
I try to disable layout in zend action in this way:
$this->_helper->layout->disableLayout();
But it doesn't work:
Fatal error: Call to undefined method Application_Controller_Helper_Layout::disableLayout() in application/controllers/AssetController.php on line 18
I reckon that its, because I made my own helper.
Bootsrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initMyActionHelpers()
{
$this->bootstrap('frontController');
$layout = Zend_Controller_Action_HelperBroker::getStaticHelper('Layout');
Zend_Controller_Action_HelperBroker::addHelper($layout);
}
}
application.ini
resources.frontController.actionhelperpaths.Application_Controller_Helper = APPLICATION_PATH "/controllers/helpers"
and my helper
class Application_Controller_Helper_Layout extends Zend_Controller_Action_Helper_Abstract
{
public function preDispatch()
{
}
}
Thanks!
If you want to rewrite Zend default layout helper, please extend Zend_Layout_Controller_Action_Helper_Layout.
class Application_Controller_Helper_Layout extends Zend_Layout_Controller_Action_Helper_Layout
Then you will still be able to use default disableLayout() function if you are not overwriting it in your own helper.
Try testing it using:
class Application_Controller_Helper_Layout extends Zend_Layout_Controller_Action_Helper_Layout
{
public function preDispatch(){}
public function disableLayout(){
echo "I won't disable layout anymore, because I overwrote the action with displaying this string.";
}
}
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');
}
}
My helper file Acl.php is in library/Helper and I have included it in bootstrap file as below:-
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initPlugins()
{
$helper= new Helper_Acl();
// $helper->setRoles();
// $helper->setResources();
// $helper->setPrivilages();
// $helper->setAcl();
}
}
but its giving error, Saying -> Fatal error: Class 'Helper_Acl' not found in Bootstrap.php.
Below is my helper file
class Helper_Acl
{
public $acl;
public function __construct()
{
$this->acl = new Zend_Acl();
}
}
in the bootstrap.php , try this , provided your class is in a Helper folder in the library :
protected function _initHelpers() {
Zend_Controller_Action_HelperBroker::addPrefix("Helper_");
}
if it doesnt work tell me , there are other methods.
You need to add Helper_ to your autoloadernamespaces. Typically, this is done in application/configs/application.ini:
autoloadernamespaces[] = "Helper_"