简体   繁体   中英

Opencart: call method from another controller

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();
  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.

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');
    }
}

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'])
);

he and what these bad answers!!

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

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

调用框控制器的ShipmentDate()函数

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

return $this->load->controller('error/not_found');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM