【问题标题】:Opencart: call method from another controllerOpencart:从另一个控制器调用方法
【发布时间】:2014-08-25 12:18:25
【问题描述】:

我需要在 checkout/confirm.tpl 文件中调用我在 controller/product.php

中创建的自定义函数

最好的方法是什么?

我试过了,但是不行:

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

【问题讨论】:

    标签: php templates opencart


    【解决方案1】:

    是的,我终于找到了正确的答案!!!抱歉最后一个不好的答案

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

    【讨论】:

    • 快速提示:您还可以将参数作为$this->load->controller('product/ready', array('foo'=>'bar')) 传递并在控制器中以public function index($args) 的形式访问它们。
    • aexl,我可以这样做吗!这将非常有用,谢谢:)
    【解决方案2】:
    1. MVC
      • 在 MVC 架构中,模板用于渲染/显示数据;它不应该 (*) 调用控制器/模型函数也不应该执行 SQL 查询,正如我在许多 第三方 模块(甚至在 答案在这里)。
    2. $productController = $this->load->model('product/product');
      • 聪明的眼睛必须发现您正在尝试将 模型 加载到由 controller 命名的变量中,并且您也在尝试以这种方式使用它。好吧,为了您的目的,Loader 类中必须有一个方法 controller() - 这不是(幸运的是)
    3. 应该怎么做?
      • 确定有办法从模板中访问调用控制器功能。在 MVC 中,由路由调用的可调用函数称为 action。使用这句话,我现在可以说您可以调用一个动作(控制器功能)通过访问某个 URL

    假设您的控制器是 CatalogProductController,而您要调用的方法是 custom() - 在这种情况下访问此 URL

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

    您将确保调用/访问CatalogProductControllercustom() 方法。

    您可以通过多种方式访问​​此类 URL - 作为 cURL 请求、链接的 href 或通过 AJAX 请求等等。在 PHP 范围内,即使 file_get_contents() 或类似方法也可以工作。

    (*) 不应该我的意思是(不幸)在 OpenCart 中是可能的,但这种滥用是针对 MVC 架构的。

    【讨论】:

      【解决方案3】:

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

      调用盒子控制器的ShipmentDate()函数

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

      【讨论】:

        【解决方案4】:

        可能这样的事情可以帮助你(或任何感兴趣的人)

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

        【讨论】:

          【解决方案5】:
          return $this->load->controller('error/not_found');
          

          【讨论】:

          • 解释这段代码如何解决问题会很有帮助。
          【解决方案6】:

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

          在这种情况下,它很好地调用product/ready

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-09-23
            • 1970-01-01
            • 1970-01-01
            • 2020-07-18
            • 1970-01-01
            • 2019-10-17
            • 1970-01-01
            • 2016-09-14
            相关资源
            最近更新 更多