Opencart: call method from another controller
Asked Answered
S

6

6

I need to call in checkout/confirm.tpl file a custom function that i've made in controller/product.php

what's the best way to make this?

i've tried this, but doesn't work:

$productController = $this->load->model('product/product');
$productController->customFunction();
Saguenay answered 25/8, 2014 at 12:18 Comment(0)
S
4
  1. MVC
    • in an MVC architecture, a template serves solely for rendering/displaying data; it shouldn't (*) call controller/model functions nor it shouldn't execute SQL queries as I have seen in many third-party modules (and even in answers here at SO).
  2. $productController = $this->load->model('product/product');
    • nifty eye has to discover that you are trying to load a model into a variable named by controller and you are also trying to use it in such way. Well, for your purpose there would have to be a method controller() in class Loader - which is not (luckily)
  3. How it should be done?
    • sure there is a way how to access or call controller functions from within templates. In MVC a callable function that is invoked by routing is called action. Using this sentence I can now say that you can invoke an action (controller function) by accessing certain URL.

So let's say your controller is CatalogProductController and the method you want to invoke is custom() - in this case accessing this URL

http://yourstore.com/index.php?route=catalog/product/custom

you will make sure that the custom() method of CatalogProductController is invoked/accessed.

You can access such URL in many ways - as a cURL request, as a link's href or via AJAX request, to name some. In a PHP scope even file_get_contents() or similar approach will work.

(*) By shouldn't I mean that it is (unfortunately) possible in OpenCart but such abuse is against MVC architecture.

Selfimportant answered 25/8, 2014 at 13:55 Comment(0)
H
7

yes i find the right answer finally!!! sorry for last bad answer

class ControllerCommonHome extends Controller {
    public function index() {
        return $this->load->controller('product/ready');
    }
}
Hearse answered 7/9, 2015 at 11:15 Comment(2)
Quick note: you can also pass arguments as $this->load->controller('product/ready', array('foo'=>'bar')) and access them as public function index($args) in your controller.Geoid
aexl, could I do this!! it will be so useful, thanks :)Hearse
S
4
  1. MVC
    • in an MVC architecture, a template serves solely for rendering/displaying data; it shouldn't (*) call controller/model functions nor it shouldn't execute SQL queries as I have seen in many third-party modules (and even in answers here at SO).
  2. $productController = $this->load->model('product/product');
    • nifty eye has to discover that you are trying to load a model into a variable named by controller and you are also trying to use it in such way. Well, for your purpose there would have to be a method controller() in class Loader - which is not (luckily)
  3. How it should be done?
    • sure there is a way how to access or call controller functions from within templates. In MVC a callable function that is invoked by routing is called action. Using this sentence I can now say that you can invoke an action (controller function) by accessing certain URL.

So let's say your controller is CatalogProductController and the method you want to invoke is custom() - in this case accessing this URL

http://yourstore.com/index.php?route=catalog/product/custom

you will make sure that the custom() method of CatalogProductController is invoked/accessed.

You can access such URL in many ways - as a cURL request, as a link's href or via AJAX request, to name some. In a PHP scope even file_get_contents() or similar approach will work.

(*) By shouldn't I mean that it is (unfortunately) possible in OpenCart but such abuse is against MVC architecture.

Selfimportant answered 25/8, 2014 at 13:55 Comment(0)
J
3

$this->load->controller('sale/box',$yourData);

To call ShipmentDate() function of box Controller

$this->load->controller('sale/box/ShipmentDate',$yourData);

Jon answered 3/8, 2017 at 6:56 Comment(0)
F
2

May be something like this could help you (or anyone who's interested)

// Load seo pro
require_once(DIR_CATALOG."/controller/common/seo_pro.php"); // load file
$seoPro = new ControllerCommonSeoPro($this->registry); // pass registry to constructor

$url = HTTP_CATALOG . $seoPro->rewrite(
 $this->url('information/information&information_id=' . $result['information_id'])
);
Felten answered 19/8, 2015 at 8:7 Comment(0)
F
1
return $this->load->controller('error/not_found');
Fresh answered 5/3, 2019 at 15:46 Comment(1)
An explanation of how this code fixes the problem would be helpful.Cooperate
H
-1

in laravel its so simple just write Controller::call('ApplesController@getSomething');

but there i cant made better than this

$config = new Config();
// Response
$response = new Response();
$response->addHeader('Content-Type: text/html; charset=utf-8');
$response->setCompression($config->get('config_compression'));
$this->registry->set('response', $response);


$action = new Action('product/ready');
$controller = new Front($this->registry);
$controller->addPreAction(new Action('common/maintenance'));
$controller->addPreAction(new Action('common/seo_url'));
$controller->dispatch($action, new Action('error/not_found'));
$response->output();

in this case its well call product/ready

Hearse answered 3/9, 2015 at 15:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.