Opencart: calling a method from another controller

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

What's the best way to do this?

I tried this but doesn't work:

$productController = $this->load->model('product/product');
$productController->customFunction();

      

+3


source to share


5 answers


  • MVC
    • in the MVC architecture, the template is used exclusively for rendering / displaying data; it shouldn't (*) call controller / model functions and not execute SQL queries as I've seen in many third party modules (and even in the answers here on SO).
  • $productController = $this->load->model('product/product');

    • A distinctive look should reveal that you are trying to load the model into a variable named by the controller and you are also trying to use it that way. Well, for your purpose there should be a method controller()

      in the class Loader

      - which is not (thankfully)
  • How to do it?
    • sure there is a way to access or call controller functions from templates. In MVC, a callable function called by a routing is called an action . Using this sentence, I can now say that you can invoke an action (controller function) by accessing a specific URL .

So let's say your controller CatalogProductController

and the method you want to call custom()

- in this case, access that url

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

      



you make sure the method is called custom()

CatalogProductController

.

You can access this URL in many different ways - as a cURL request, as a href link, or via an AJAX request, to name a few. In the PHP realm, even file_get_contents()

or a similar approach will work.

(*) Shouldn't it be understood that in OpenCart it is possible ( unfortunately ), but such abuse is contrary to the MVC architecture.

+3


source


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



class ControllerCommonHome extends Controller {
    public function index() {
        return $this->load->controller('product/ready');
    }
}

      

+3


source


Maybe something like this could help you (or anyone 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'])
);

      

0


source


him and what are these bad answers !!

in laravel it is so easy to write Controller :: call ('ApplesController @getSomething');

but i can't do it better

$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 product is well call / ready

0


source


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

To call a ShipmentDate()

controller window function

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

0


source







All Articles