0

I am implementing a quotation form. I then declared a module in which I defined a phtml template containing the form, the computed price display and the javascript/ajax script.

I also have defined a block :

<?php
class Mine_Quotemodule_Block_Quoteformblock extends Mage_Core_Block_Template
{
     public function methodblock()
     {
         return 'informations de mon block !!';
     }
}
?>

and a controller

<?php
class Mine_Quotemodule_IndexController extends Mage_Core_Controller_Front_Action
{
   public function indexAction()
   {
        $this->loadLayout();
        $this->renderLayout();
   }

   public function quoteAction()
   {
     echo 'test mamethode';
    }
}
?>

In order to be able to display and update my quotation price, I need to use some vars.

I thought declaring and initializing them in the indexAction() (for default price display) and update them in quoteAction() which will be called by my form script.

Is this the good way?

if declared in indexAction, how to refer to them in quoteAction() and in the phtml?

Thank you for your help,

Alex

1 Answer 1

1

The global variables are set using Mage::register('variable', $variable). This variable can be called from anywhere then using $_variable = Mage::registry('variable).

If you need a session variable, you can use :

Mage::getSingleton('core/session')->setMyVariable('value of variable');

This variable can be called later with :

$variable = Mage::getSingleton('core/session')->getMyVariable();

I would say that ideally the session and global variables are set in the controllers. You can then call them anywhere.

For the variables used is the view (.phtml), the best is to set them in the block. You can then call them using $this->getVariable().

private $_variable;
public function getVariable(){
    return $_variable
} 
public function setVariable(){
    ...logic...
}
4
  • I managed to declare a session variable in the block and access it in phtml. Just for info, if I want to use $this->getVariable('Myvar') in phtml how may I declare it in Block?
    – Alexglvr
    Oct 6, 2014 at 11:14
  • There is a xml way that I do not know so well but you can also create a variable in your block with the appropriate getter and setter. I update my answer with it. Oct 6, 2014 at 12:07
  • If I use a session var, declared in Block, may I change its value in the controller using the same command ? Mage::getSingleton('core/session')->setMyVariable('value of variable');
    – Alexglvr
    Oct 7, 2014 at 6:31
  • I edited the post. Ideally, globals and session are set in the controllers. Oct 7, 2014 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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