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);
Related
I use display value or data. In case of a single value use loop, but I know this is a bad habit. How can I solve this? for example I use this:
#foreach($result as $result)
{{$result->data}}
#endforeach
First of all, your code doesn't tell anything, and also you have this code all wrong.
You can't name a pointer as the same name of the array, ($result as $result ???) you will have wrong results or errors.
Also inside the froeach you are referencing a variable that doesn't exists (?).
In my opinion "foreach" is necessary to loop through arrays.
I really don't know what are you trying to do, so that would be great if you expand your question and provide more information.
You can replace that with some code like this:
#if($result)
{{$data}}
#endif
You can try code below see
//example in file ExampleController.php
use App\Posts;
public function showAll(){
$data = Posts::all();
return view('showall',['result'=>$data]);
}
//file web.php
Route::get('/show-all','ExampleController#showAll');
//show-all.blade.php
#foreach($result as $value)
{{$value.title_name}}
#endforeah
Mail::send('emails.cont',['name'=>$n,'email'=>$email],function($message){
$message->to('abc#gmail.com','efe')->from('cde#gmail.com')->subject($s);
});
There for the subject, I am trying to pass a variable called $s which contains a value which defined there. But, it will underlying in red and saying undefined variable called $s. How to solve that?
can you please try the following code
Mail::send('emails.cont',['name'=>$n,'email'=>$email],function($message) use ( $s ) {
$message->to('abc#gmail.com','efe')->from('cde#gmail.com')->subject($s);
});
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.
I am trying to do this route trick:
$route['cp/roles/:num'] = "cp/roles/index/:num";
but it doesn't work :(
please help me!!
advanced thanks .
According to the documentation on URI Routing:
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
“A URL with "product" as the first segment, and a number in the second will be remapped to the "catalog" class and the "product_lookup_by_id" method passing in the match as a variable to the function.”
So, for your particular instance, you would do the following:
$route['cp/roles/(:num)'] = "cp/roles/index/$1";
You could try
$route['cp/roles/:num'] = "cp/roles";
and then instead of passing a variable in your function you use
$this->uri->segment(3);
or the number that correspond to the segment.
I am creatting a Code Igniter project in which I want to pass a variable through the URL like a get statement, like this:
url: /site/cake/1
controller function: cake($var)
but when the variable is left blank, I receive an error, how can I get code igniter, to ignore this?
In your controller, do this:
function cake($var = null) {
// your other code here
}
When $var isn't present in the URL, it will be set to null and you'll receive no error.
To explain why Colin's answer works:
The issue you had, was that there was no default value for that controller function. In php, creating a default value for a function parameter is done by assigning it a value in the function definition ($var = false). Now when the cake() function is called with no parameter, it will set $var to false by default.