0

I'm trying to pass a message from view to layout by using a model. This does not work when using _redirect(); I know there is a magento core functionality, but I'd like to understand why my custom solution does not work.

$status = Mage::getSingleton('custom/statusobject');
$status ->setMessage('Error:' + $message);

//$status ->getMessage(); Works fine

$this->_redirect('*/*/');

public function indexAction()
{
   $status = Mage::getSingleton('custom/statusobject');
   $status ->getMessage(); // does not exists
}

**** EDIT **** This does also not work for session

in controller

Mage::getSingleton('core/session')->setError('ERROR');
session_write_close();
$this->_redirectUrl('*/*/');
return;

In my block

$statusobject = Mage::getSingleton('core/session');
$message =$statusobject->getMessages()->getItems();

print_r($message); // empty array

I dont know why my messages are lost after redirect

1

You need to save() for carry data.

$status = Mage::getSingleton('custom/statusobject');
$status ->setMessage('Error:' + $message);
$status->save();
//$status ->getMessage(); Works fine
$this->_redirect('*/*/');

this will works when message is field of custom/statusobject .

Or better idea to sue core/session variable to carry value on new page.

Mage::getSingleton('core/session')->setMyMessage('Error:' + $message);

fetch Data on new page using

Mage::getSingleton('core/session')->getMyMessage();

  • ah okay I saw it does not work in this setup also for core/session. Okay so I really have to perfom DB action then? When using core/session I also need to do the save action? – xhallix Nov 12 '15 at 12:31
1

i think solution you need is register. that are use in magento to carry data between controller to block or view. because session need to reload the page.

you can set register

Mage::register('name-of-registry-key', $your-data);

get registry

$var = Mage::registry('name-of-registry-key');
  • is this more recommended than passing between controller -> model -> block? I thought this would be a more mvc like approach? – xhallix Nov 12 '15 at 12:48
  • yes it is mvc approach also it destroy after load page. – Qaisar Satti Nov 12 '15 at 12:49
  • Mage::register('name-of-registry-key', 'foobar'); -> did this in my controller this in my block $var = Mage::registry('name-of-registry-key'); but did not work – xhallix Nov 12 '15 at 12:54
  • did you echo it echo $var = Mage::registry('name-of-registry-key'); because i use as many time i can't count. so could not find any single problem with this. also use it before the $this->loadLayout(); in controller – Qaisar Satti Nov 12 '15 at 13:00
  • I used print_r to be sure :) It works until my redirect $this->_redirect('custom/formsubmit/'); then it is empty – xhallix Nov 12 '15 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.