简体   繁体   English

Opencart:来自另一个控制器的调用方法

[英]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 我需要在checkout / confirm.tpl文件中调用我在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 MVC
    • in an MVC architecture, a template serves solely for rendering/displaying data; 在MVC架构中,模板用于呈现/显示数据; 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). 它不应该(*)调用控制器/模型函数, 也不应该像我在许多第三方模块中看到的那样执行SQL查询 (甚至在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) 那么,为了你的目的,必须在类Loader有一个方法controller() - 这不是(幸运的)
  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 . 在MVC中,由路由调用的可调用函数称为操作 Using this sentence I can now say that you can invoke an action (controller function) by accessing certain URL . 使用这句话我现在可以说你可以 通过访问某个URL 来调用一个动作 (控制器功能)。

So let's say your controller is CatalogProductController and the method you want to invoke is custom() - in this case accessing this URL 因此,假设您的控制器是CatalogProductController ,您要调用的方法是custom() - 在这种情况下访问此URL

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

you will make sure that the custom() method of CatalogProductController is invoked/accessed. 您将确保调用/访问CatalogProductControllercustom()方法。

You can access such URL in many ways - as a cURL request, as a link's href or via AJAX request, to name some. 您可以通过多种方式访问​​此类URL - 作为cURL请求,作为链接的href或通过AJAX请求来命名。 In a PHP scope even file_get_contents() or similar approach will work. 在PHP范围内,即使file_get_contents()或类似的方法也可以。

(*) By shouldn't I mean that it is ( unfortunately ) possible in OpenCart but such abuse is against MVC architecture. (*)我不应该意味着( 不幸的是 )它可能在OpenCart中,但这种滥用是针对MVC架构的。

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'); 在laravel中它如此简单只需编写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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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