change a autoloaded variable in codeigniter framework - codeigniter

I need to change a variable which is saved over the autoloader function.
Inside of a existing class (named "app") I can check if the variable is set.
$this->options[$name];
Now I want to change the value of these autoloaded variable, inside my app.php class in this way
---
echo"old value: ".$this->options[$name].")";
$this->options[$name] = $value;
echo "new value:".$this->options[$name];
return true;
...
for this, i get the correct new value.
The problem is, that it seems that this new value is not updated for the rest of the script!? If i access this variable later, i get the old value!?
What i do wrong?

The only "variables" that persist across different pages/loads/refreshes .etc. are those that are stored in a session variable, cookie or database.
To reiterate; this:
echo"old value: ".$this->options[$name].")";
$this->options[$name] = $value;
echo "new value:".$this->options[$name];
return true;
only affects the current instance at run-time (you can look at this as a page view). It will not persist. Same goes for config value changes in CodeIgniter.
Your only method is using some sort of file/database based storage.

Related

Get return variable or variable from one function to another function in controller

I am wondering if it is allowed to access/get/use the variable from one function to another function within one controller?
Thank you.
Try Something like this.
class Index extends CI_Controller {
protected $var1;
public function index() {
$this->setVar1();
echo $this->var1; // print $var1
}
public function setVar1() {
$this->var1 = 1;
}
}
$superduper = 'superduper' ;
available only locally within the method (function)
or passed like
$this->reviews->insertToReviews($superduper) ;
versus
$this->superduper = 'superduper' ;
as soon as $this->variablename is declared in a controller its instantly available to:
any method in the controller
any method in any model that is called by the controller
any view file called by the controller
so without passing the variable - in a view you can use
echo 'my lunch is ' . $this->superduper ;
but often its better to explicitly pass values to the view especially if they are unique to the method - it makes it easier to see what is going on. so in that case in controller:
$data['superduper'] = $this->superduper ;
and in view
echo 'my lunch is ' . $superduper ;
Now when anyone looks at the method in the controller - we can see that superduper is being passed to $data. the point is that even though you can avoid passing variable names to methods or the view by declaring $this->somename - if you pass them locally it can make it easier to see what is going on.
The flip side is something like:
$this->error_message = "Error retrieving database records";
is awesome. you can have error messages in any method and they will be automatically available no matter what else happens. so then in your view file have something like
if($this->error_message != '') { echo $this->error_message ;}
this is especially helpful while you are building the site.

Magento registry variables in controller

I have stored a variable in register by Mage::register('captcha', $var); in helper. And in the controller i tried to retrieve the variable by using Mage::registry('captcha'); But i dont getting any values here. Please help me to solve this.
In your helper file create a function like below :
public function getCaptcha(){
$var = 'myValue123';
Mage::register('varun', $var);
return Mage::registry('varun');
}
In your controller function:
$registryValue = Mage::helper('yourModule')->getCaptcha();
echo registryValue ; //prints myValue123
Hope it helps !!!
It's look like syntax is right.
Please first try to set some static value like $var="test"
Mage::register('captcha', $var);
after that got this value in controller.
Mage::registry('captcha');
if you got this value test then i think you have problem with $var in your helper.
Let me know if you have any problem
'captcha' is already in use, so magento never set your data in registry. Change the name, for example 'captcha1'
Mage::register('captcha1', $var);

How to get a url parameter in Magento controller?

Is there a Magento function to get the value of "id" from this url:
http://example.com/path/action/id/123
I know I can split the url on "/" to get the value, but I'd prefer a single function.
This doesn't work:
$id = $this->getRequest()->getParam('id');
It only works if I use http://example.com/path/action?id=123
Magento's default routing algorithm uses three part URLs.
http://example.com/front-name/controller-name/action-method
So when you call
http://example.com/path/action/id/123
The word path is your front name, action is your controller name, and id is your action method. After these three methods, you can use getParam to grab a key/value pair
http://example.com/path/action/id/foo/123
//in a controller
var_dump($this->getRequest()->getParam('foo'));
You may also use the getParams method to grab an array of parameters
$this->getRequest()->getParams()
If your url is the following structure: http://yoursiteurl.com/index.php/admin/sales_order_invoice/save/order_id/1795/key/b62f67bcaa908cdf54f0d4260d4fa847/
then use:
echo $this->getRequest()->getParam('order_id'); // output is 1795
If you want to get All Url Value or Parameter value than use below code.
var_dump($this->getRequest()->getParams());
If your url is like this: http://magentoo.blogspot.com/magentooo/userId=21
then use this to get the value of url
echo $_GET['userId'];
If you want more info about this click here.
If it's a Magento module, you can use the Varien Object getter. If it's for your own module controller, you may want to use the register method.
Source: http://www.vjtemplates.com/blog/magento/register-and-registry

Programmatically remove block from layout

I want to remove the product_options_wrapper block from the product view page according to the logedin user via frontend router controller.
I know that I can programmatically append a new block but I didn't find a remove function. :-(
Tried sth. like that
$this->getLayout()->unsetBlock('product_options_wrapper');
$this->getLayout()->getBlock('product.info')->remove('product_options_wrapper');
But nothing works.
Inorder to remove a block using its parent block use the code below
$this->getLayout()->getBlock('product.info')->unsetChild('product_options_wrapper');
The OP code should work, if it used the correct block name, which is product.info.options.wrapper, as opposed to the block alias.
$this->loadLayout();
//e.g.
if (Mage::getSingleton('customer/session')->getCustomerGroupId() == [id]){
$this->getLayout()->unsetBlock('product.info.options.wrapper');
}
$this->renderLayout();
This should work:
$blockName = 'left'; // Add yours
$update = Mage::app()->getLayout()->getUpdate();
$removeInstruction = "<remove name=\"$blockName\"/>";
$update->addUpdate($removeInstruction);
Why? Have a look in the file Mage_Core_Model_Layout in the method generateXml() the XML is parsed and where a remove is set for a block, the attribute ignore is added to the block. In the method generateBlocks() all the blocks which have that attribute are not added.

How to unset session in Magento?

I am using session in my magento custom module.Below is my code
$session = Mage::getSingleton("core/session", array("name"=>"frontend"));
$session->setData("day_filter", 'weeks');
$session->setData("days", '5');
$session->setData("next_delivery_date", '2012-05-12');
The above code is working fine, but now i want to unset or destroy all the value? Can you please provide me the solution how to unset all set values?
There are several ways to unset session variables in Magento. Most of these (not all) are defined in Varien_Object and so are avialable to all objects in Magento that extend it.
unsetData:
$session->unsetData('day_filter');
$session->unsetData('days');
$session->unsetData('next_delivery_date');
uns (which will be marginally slower and ultimately executes unsetData anyway):
$session->unsDayFilter();
$session->unsDays();
$session->unsNextDeliveryDate();
getData
Not a mistake! A relatively unkown method exists in Mage_Core_Model_Session_Abstract_Varien. The getData method in this class contains an optional boolean second parameter which if passed true will clear the variable while returning it.
So $session->getData('day_filter', true); would return the session variable day_filter and also clear it from the session at the same time.
Set to null:
$session->setData('day_filter', NULL);
$session->setData('days', NULL);
$session->setData('next_delivery_date', NULL);
unsetAll | clear
Finally you could use the nuclear option (BEWARE: This will unset ALL DATA in the session, not just the data you have added):
$session->unsetAll(); or $session->clear(); (both aliases of each other)

Resources