簡體   English   中英

Opencart:來自另一個控制器的調用方法

[英]Opencart: call method from another controller

我需要在checkout / confirm.tpl文件中調用我在controller / product.php中創建的自定義函數

什么是最好的方法呢?

我試過這個,但不起作用:

$productController = $this->load->model('product/product');
$productController->customFunction();
  1. MVC
    • 在MVC架構中,模板用於呈現/顯示數據; 它不應該(*)調用控制器/模型函數, 也不應該像我在許多第三方模塊中看到的那樣執行SQL查詢 (甚至在SO的答案中)。
  2. $productController = $this->load->model('product/product');
    • 漂亮的眼睛必須發現你正在嘗試將模型加載到由控制器命名的變量中,並且你也試圖以這種方式使用它。 那么,為了你的目的,必須在類Loader有一個方法controller() - 這不是(幸運的)
  3. 怎么做?
    • 確保有一種方法可以從模板中訪問調用控制器功能。 在MVC中,由路由調用的可調用函數稱為操作 使用這句話我現在可以說你可以 通過訪問某個URL 來調用一個動作 (控制器功能)。

因此,假設您的控制器是CatalogProductController ,您要調用的方法是custom() - 在這種情況下訪問此URL

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

您將確保調用/訪問CatalogProductControllercustom()方法。

您可以通過多種方式訪問​​此類URL - 作為cURL請求,作為鏈接的href或通過AJAX請求來命名。 在PHP范圍內,即使file_get_contents()或類似的方法也可以。

(*)我不應該意味着( 不幸的是 )它可能在OpenCart中,但這種濫用是針對MVC架構的。

是的,我終於找到了正確的答案! 對不起最后一個壞答案

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

可能是這樣的事可以幫助你(或任何有興趣的人)

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

他和這些不好的答案!

在laravel中它如此簡單只需編寫Controller :: call('ApplesController @ getSomething');

但是我不能做得比這更好

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

在這種情況下,它的呼叫產品/准備就緒

$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