Page 1 of 1
How do I access an array created by one controller in a second controller?
Posted: Thu Oct 03, 2024 11:32 pm
by Lumirion
Using Opencart 3.0.3.3.
How do I access data, created by one controller, in a second controller? For instance, one controller generates the following object and makes it available to the view.
Code: Select all
$data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
'description' => utf8_substr(trim(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'))), 0, $this->config->get('theme_' . $this->config->get('config_theme') . '_product_description_length')) . '..',
'price' => $price,
'special' => $special,
'tax' => $tax,
'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
'rating' => $result['rating'],
'href' => $this->url->link('product/product', 'product_id=' . $result['product_id']),
);
Now, rather than duplicating all the work done by the first controller, I want the second controller to access the same {{ ‘products’ }} that the twig file has access to. Is there some this->Data->get(‘products’); function I can use in the second controller?
Re: How do I access an array created by one controller in a second controller?
Posted: Thu Oct 03, 2024 11:35 pm
by OSWorX
You could achieve such with an event.
Re: How do I access an array created by one controller in a second controller?
Posted: Fri Oct 04, 2024 6:14 am
by Lumirion
How would I get the event to dispatch between the 2 controllers? Can the event access the stored variables the twig file references?
Re: How do I access an array created by one controller in a second controller?
Posted: Fri Oct 04, 2024 1:58 pm
by OSWorX
Lumirion wrote: ↑Fri Oct 04, 2024 6:14 am
How would I get the event to dispatch between the 2 controllers? Can the event access the stored variables the twig file references?
It depends what you want to do ..
Either call the event
before the controller/view is called, or
after.
Depending on that, you have access to several variables and can "manipulate" (add/remove/change) whatever you want/need.
Re: How do I access an array created by one controller in a second controller?
Posted: Mon Oct 07, 2024 6:39 pm
by paulfeakins
OSWorX wrote: ↑Thu Oct 03, 2024 11:35 pm
You could achieve such with an
event.
Alright straightlight.
Re: How do I access an array created by one controller in a second controller?
Posted: Mon Oct 07, 2024 6:40 pm
by paulfeakins
Lumirion wrote: ↑Thu Oct 03, 2024 11:32 pm
How do I access data, created by one controller, in a second controller?
You shouldn't. Data should come from models and both controllers should call the same method in a model.
Re: How do I access an array created by one controller in a second controller?
Posted: Tue Oct 08, 2024 12:44 am
by nonnedelectari
Lumirion wrote: ↑Thu Oct 03, 2024 11:32 pm
Using Opencart 3.0.3.3.
How do I access data, created by one controller, in a second controller? For instance, one controller generates the following object and makes it available to the view.
Code: Select all
$data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
'description' => utf8_substr(trim(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'))), 0, $this->config->get('theme_' . $this->config->get('config_theme') . '_product_description_length')) . '..',
'price' => $price,
'special' => $special,
'tax' => $tax,
'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
'rating' => $result['rating'],
'href' => $this->url->link('product/product', 'product_id=' . $result['product_id']),
);
Now, rather than duplicating all the work done by the first controller, I want the second controller to access the same {{ ‘products’ }} that the twig file has access to. Is there some this->Data->get(‘products’); function I can use in the second controller?
Apart from events,
You can also just pass the products array as a parameter to the target controller.
I use that to pass settings to a product listng module:
Code: Select all
$module_name = 'products';
$module_setting = array(
'csrf_token' => $csrf_token,
'view' => $module_view,
'product_kind' => $product_kind,
'limit' => 8,
'type' => 'PATH',
'sort' => 'RAND',
'category' => $product_category_id,
'filter_sub_category' => true,
'exclude' => $product_id,
'only_not_sold' => true,
'only_stock' => true
);
$output = $this->load->controller('extension/module/' . $module_name, $module_setting);
Then in the target controller, extension/module/products.php in my case, you give the index function the parameter:
Code: Select all
public function index($setting) {
........
}
So you can pass your products array in the same manner.