Set Block Class Property from Custom Module Controller - magento-1.9

I am trying to access block class method from controller and set some value to block class property. The method is being accessed but when I try to print the property in .phtml file it is NULL.
Here is my code:
app/code/local/Amactech/Events/controllers/IndexController.php
<?php
class Amactech_Events_IndexController extends Mage_Core_Controller_Front_Action{
public function indexAction(){
$block=$this->getLayout()->createBlock('events/events');
$block->setFeedback("wrwqerqwer fasdfasdf vxzcvzxcv");
$this->loadLayout();
return $this->renderLayout();
}
}
?>
app/code/local/Amactech/Events/Block/Event.php
<?php
class Amactech_Events_Block_Events extends Mage_Core_Block_Template{
public $_feedback;
public function setFeedback($feedback){
echo $feedback;
$this->_feedback = $feedback;
}
public function getFeedback(){
echo "get feedback"; /*This line is being printing which means I am getting in this method*/
return $this->_feedback;
}
}
?>
app/design/frontend/base/default/template/events/events.phtml
<?php
echo $this->getFeedback();
?>
So I am accessing block setFeedback() method and setting $_feedback value. I print something string inside setFeedback() and getFeedback() methods to check whether I am getting into these methods. The printing string is shown which means I am getting into the methods but when I assign some value to property and try to print the property is shows NULL.

Can't you try magento session instead for setting up a value to block.
I think in the above case the value comes only while you call the controller, right ?
Mage::getSingleton('core/session')->setFeedback($inputMessage); // In controller
Mage::getSingleton('core/session')->getFeedback($inputMessage); // In PHTML

Related

How to use renderAjax() directly?

I create helper without extends controller, like this
class SomeClass{
public function static SomeMethod(){
//this function for send email
$controller = new \yii\web\Controller();
$controller->renderAjax("template", "array");
}
}
how ever the above code generate Missing argument 1 for yii\base\Controller::__construct(), called in common\component\BeoHelper.php on line 1715 and defined
How I can fix it?
Try this:
\Yii::$app->controller->renderAjax(...);

Twig loader in constructor

I try to make my own Response class using twig
<?php
namespace app\library;
class Response
{
public function __construct($temp, $data)
{
$loader = new Twig_Loader_Filesystem('app/views');
$twig = new Twig_Environment($loader);
print $twig->render($temp, $data);
}
}
But when I try to use it
use app\library\Response;
error_reporting(E_ALL);
require_once "vendor/autoload.php";
$arr = array('name'=>'Bob', 'surname'=>'Dow', 'gender'=>'male','age'=>'25');
new Response('temp.php', $arr);
It gives me
Fatal error: Class 'app\library\Twig_Loader_Filesystem' not found in /var/www/PHP/app/library/Response.php on line 12
Where the mistake?
Please check carefully the error. It says that class 'app\library\Twig_Loader_Filesystem' doesn't exists. Your Response class is under app\library namespace so every class which you try to instantiate there will be looked inside this namespace as well. Basically it's the same as write
$loader = new app\library\Twig_Loader_Filesystem('app/views');
$twig = new app\library\Twig_Environment($loader);
Generally in this case you have to either type the full name of a class including its namespace or make the shorthand with the help of use statement like you did for instantiating Response class.
In your particular case classes Twig_Loader_Filesystem and Twig_Environment exist in a global namespace so you could either add \ in front of classes to state that these classes are located in a global namespace:
$loader = \Twig_Loader_Filesystem('app/views');
$twig = \Twig_Environment($loader);
or create a shorthand like this:
use Twig_Loader_Filesystem;
use Twig_Environment;

How to make every action in a zend (1.12) controller return JSON?

I have a controller that is purely for API use (no view or layout).
I want every action to return JSON format and have the Content-Type in the response be application/json.
I could achieve the header part by using the controller postDispatch() but couldn't find a way to do json_encode() from single place (I know I can do it from every action however I wanted it to be centralized).
I have even tried to use a plugin and there to manipulate the request body but for some reason that is not clear to me it is always empty.
Currently my solution is as follows:
public function init()
{
// no Layout
$this->_helper->layout()->disableLayout();
// no views
$this->_helper->viewRenderer->setNoRender(true);
}
public function indexAction()
{
$data = array("likes","to","sleep");
echo Zend_Json::encode($data);
}
public function postDispatch()
{
$this->getResponse()->setHeader('Content-Type', 'application/json');
}
Now, if i only managed to do the echo Zend_Json::encode in one single place...
You can use the ContextSwitch action helper
The JSON context sets the 'Content-Type' response header to 'application/json', and the view script suffix to 'json.phtml'. By default, however, no view script is required. It will simply serialize all view variables, and emit the JSON response immediately.
You will need to register it within the controller.
class FooController extends \Zend_Controller_Action
{
public function init()
{
$contextSwitch = $this->_helper->getHelper('contextSwitch');
$contextSwitch->addActionContext('list', 'json')
->addActionContext('bar', 'json')
->initContext('json'); // json arg here means that we only allow json
}
}
You would then need to pass the format parameter in the url /module/foo/bar/format/json or as a query parameter ?format=json
You can create your own controller plugin.
In preDispatch turn off layout and view renderer and in postDispatch set content-type header in response.
Next option is to assing data to view.
public function indexAction() {
$data = array("likes","to","sleep");
$this->view->assign('data', $data);
}
and in view script call json view helper <?php echo $this->json($this->data); and do not use layout.

The _redirect() function in zend framework

I made a file in library/My/Utils/Utils.php. The content of the file is :
class My_Utils_Utils{
public function test(){
$this->_redirect('login');
}
}
This class is called from a layout; the problem is with the _redirect(); I get this error : The page isn't redirecting properly. My question is how call the _redirect() function from a class made by you in ZEND framework 1 .
Thanks in advance.
Use redirect() instead of _redirect(). Usage is:
$this->redirect(<action>, <controller>, <module>, <param>);
In your case $this->redirect('login'); should do the trick.
You can use the redirector action-helper, which you can get statically from the HelperBroker using:
// get the helper
$redirectHelper = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
// call methods on the helper
$redirect->gotoUrl('/some/url');
It should be noted, however, that the layout is considered part of the view layer. Typically, any checks that result in a redirect should probably take place earlier in the request dispatch-cycle, typically in a controller or in a front-controller plugin.
The _redirect function is provided by the class Zend_Controller_Action. You can fix this in two ways :
Extend Zend_Controller_Action and use _redirect
class My_Utils_Utils extends Zend_Controller_Action {
public function test(){
$this->_redirect('login');
}
}
in layout:
$request = Zend_Controller_Front::getInstance()->getRequest();
$response = Zend_Controller_Front::getInstance()->getResponse()
$util = new My_Utils_Utils($request, $response); // The constructor for Zend_Controller_Action required request and response params.
$util->test();
Use gotoUrl() function Zend_Controller_Action_Helper_Redirector::gotoUrl()
$redirector = new Zend_Controller_Action_Helper_Redirector();
$redirector->gotoUrl('login');
//in layout :
$util = new My_Utils_Utils();
$util->test();

How to import children() to Helper in CakePHP 2.0?

I was about making generate link of whole children. Because View doesnt allow to use Model. That's why i have to making own Helper to generate. then I use $this->Model->children($current_id, false, 'category'); but the problem i got error like this
Fatal error: Call to a member function children() on a non-object in
/user/app/View/Helper/my_helper.php on line 193
My code:
<?php
class MyHelperHelper extends AppHelper {
var $helpers = array('Html','Paginator','Tree');
function makechildrenlink($current_id, $category_id, $item_id) {
$this->Category->children($current_id, false, 'category');
}
}
?>
In your controller/component scope you need to fetch the data:
$this->loadModel('Category');
$categories = $this->Category->children($current_id, false, 'category');
$this->set(compact('categories')); // pass it down to the view
In the view you can now do with $categories whatever you want
echo $this->MyHelper->makeChildrenLink($categories);
for example.
And, as stated in the comments, your helper needs to be View/Helper/MyHelper.php!