Passing Variable Between Hooks in Same Class File - php

I am trying to retrieve a value from one action hook to be displayed in admin page.
public function hookActionProductCancel($params)
{
$this->response = "response";
}
public function hookDisplayAdminOrder($params) {
$this->context->smarty->assign('response', $this->response);
return $this->display(__FILE__, 'views/templates/admin/response.tpl');
}
I am not receiving the value "response" in response.tpl. Probably a small issue but I am not getting it right at the moment.
Any guidance is truly appreciated. Thank you.

You should just store the response to cookie and clear it before displaying it.
public function hookActionProductCancel($params)
{
// code
$this->context->cookie->mymodule_response = "response";
$this->context->cookie->write();
}
public function hookDisplayAdminOrder($params)
{
// if no response stored in cookie do nothing
if (!$this->context->cookie->mymodule_response) {
return false;
}
// assign response from cookie to smarty then clear response from cookie
$this->context->smarty->assign('response', $this->context->cookie->mymodule_response);
unset($this->context->cookie->mymodule_response);
return $this->display(__FILE__, 'views/templates/admin/response.tpl');
}

You are forgetting to use this
public function hookActionProductCancel($params)
{
..... codes .....
$this->response = "response";
}
public function hookDisplayAdminOrder($params)
{
$this->context->smarty->assign('response', $this->response);
return $this->display(__FILE__, 'views/templates/admin/response.tpl');
}

Related

How to return parent function value from child

I'm trying to get a child function to return on behalf of the parent. Is this possible? Here is the pseudo code of what I'm trying to achieve:
function editUserProfile(){
uploadFileToS3($file);
//Save the rest of the edited profile
}
function uploadFileToS3($file){
if( fileSize($file) + userStorageUsage > userMaxStorage){
return redirect()->back()->withErrors(['storage' => 'Storage full']);
}
//upload the file
}
EDIT:
public function parent()
{
return $this->child();
}
public function child(){
if(foo==bar){
return 'Test';
}
}
Just use the return keyword, like:
function editUserProfile(){
return uploadFileToS3($file);
}
Or, if you want to do something before you return the value, then:
function editUserProfile(){
$results = uploadFileToS3($file);
//Save the rest of the edited profile
return $results;
}

Recover data from columns DC2Type:array

Have a column with this format "DC2Type:array" in symphony try to read data as array but receive a Null.
my function is:
public function getData()
{
return $this->data;
}
need some particular way or is another inconvenient different. Thank you.
check your data is set or not: example code
public $data ;
public function getData()
{
return $this->data;
}
public function setData($data)
{
return $this->data = $data;
}

How to access data of one function to another in laravel

Okay the issue is something like this
I have a function in AController
public function index()
{
$store = Store::(query)(to)(rows)->first();
return view('store.index', compact('store'));
}
Now in the same controller I have another function
public function abc()
{
return view('store.abc');
}
Now to this function I also want to send the compact('store') to the view abc I can just add the query again in the abc() function but that would be lazy and make performance issues. Is there a way that I can access $store object in other functions too?
If I understand you correctly you want to access the same query from two places. So extract getting stores to another method like
private function store()
{
$minutes = 10; // set here
return Cache::remember('users', $minutes, function () {
return Store::(query)(to)(rows)->first();
});
}
Additionally I have cached the query. So it get executed once at a defiened time.
Then access it from other two methods like,
public function index()
{
$store = $this->store();
return view('store.index', compact('store'));
}
public function abc()
{
$store = $this->store();
return view('store.abc', compact('store'));
}
class StoreController extends Controller
{
public function index()
{
return view('admin.store',['data' => $this->getSetting()]);
}
public function getStoreData()
{
//get your data here, for example
$data = Store::where('status',1)->first();
//get all data
//$data = Store::all();
return ($data);
}
}
Try the following. Not testing but it should work for you.
class AController
{
public function getStore()
{
$store = Store::(query)(to)(rows)->first();
return compact('store');
}
public function index()
{
return view('store.index', $this->getStore());
}
public function abc()
{
return view('store.abc', $this->getStore());
}
}

unset a session then return its data

I want to get a session and then return its data then unset it (or get a session and then unset it then return the data), I am doing 2 (header) redirects. after setting the session till getting the session.
but the problem is its not returning anything.
following is the class I am using:
class Session {
private $session_data1;
public function __construct() {
session_start(); // starts session on all files at first...
}
public function set_session($session_name, $session_data) {
return ($_SESSION[$session_name] = $session_data) ? true : false;
}
public function get_session($session_name) {
return isset($_SESSION[$session_name]) ? $_SESSION[$session_name] : false;
}
public function get_session_once($session_name) {
$this->session_data1 = $this->get_session($session_name);
$this->unset_session($session_name);
return $this->session_data1;
}
public function unset_session($session_name) {
if (isset($_SESSION[$session_name])) {
unset($_SESSION[$session_name]);
// return true;
}
}
public function destroy_all_session() {
session_destroy();
}
}
$session = new Session();
I am using 'set_session()' to set a session and wanted to use 'get_session_once()' which will unset the session and then return the value of that session.
if I dont unset it in the method 'get_session_once()' then it works, like the following:
public function get_session_once($session_name) {
$this->session_data1 = $this->get_session($session_name);
// $this->unset_session($session_name);
return $this->session_data1;
}
I am new in PHP, please help
I tested this, and for me it's working.
<?
$session = new Session();
$session->set_session('Username', 'StackOverflow');
echo $session->get_session_once('Username'); // echos 'StackOverflow'
echo $session->get_session_once('Username'); // no echo
?>
This is still the same:
public function get_session_once($session_name) {
$this->session_data1 = $this->get_session($session_name);
$this->unset_session($session_name);
return $this->session_data1;
}
Maybe the header redirect is the cause, it might be clear/delete the session.
Did you put session_start() at the start of every page?

Another way to assign a function without executing it

Is there another way to assign or return a function without executing it? I think this is not the best solution ...
class Foo {
public function getDisplayFunction() {
return function() {
$this->display();
};
}
private function display() {
echo 'something';
}
}
$display = (new Foo())->getDisplayFunction();
//and execute it
$display();
I get it ... after trying 100 of things and 1 hour searching. Here my answer for all who looking also for the solution.
public function getDisplayFunction() {
return function() {
return $this->display();
}
}
is the same as:
public function getDisplayFunction() {
return [$this, 'display'];
}
the advantage is you don't have to pass args with this solution. That was the reason for the question.

Categories