Opencart: calling a method from another controller
- 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 classLoader
- which is not (thankfully)
- 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
- 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.
source to share
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'])
);
source to share
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
source to share