2016-03-05 162 views
1

我想從另一個控制器加載一個功能。這是我的結構:從另一個控制器使用控制器CodeIgniter和HMVC

- modules 
--orderpages 
---controllers 
----WebshopCore.php 
----WebshopController.php 

在WebshopController.php我的功能insertItemInCart被調用。但是當我想從另一個控制器執行一個函數時,它會崩潰。

class WebshopController extends MX_Controller { 
    public function __construct() { 
     parent::__construct(); 
     $this->load->module('orderPages/WebshopCore'); 
    } 

    function insertItemInCart(){ 
     $partId = $this->input->post('partId'); 
     $quantity = $this->input->post('quantity'); 

     $output = $this->WebshopCore->getPickLocations($partId,$quantity); 

    } 
} 

我WebshopCore:

class WebshopCore extends MX_Controller { 
    public function __construct() { 
     parent::__construct(); 
    } 

    public function getPickLocations($partId,$amount){ 
     $result = "test"; 

     return $result; 
    } 
} 

錯在何處?我不明白這一點

解決辦法:

$output = modules::load('orderPages/WebshopCore/')->getPickLocations($partId,$quantity); 
+0

IMO,調試:'的var_dump($這個 - > WebshopCore, __FILE __.__ LINE __);緊接在'$ output = ....'之前。 Yopu應該看到WebshopCore對象。 –

+0

@RyanVincent NULL字符串(107)「/Applications/AMPPS/www/cart.local/application/modules/orderPages/controllers/WebshopController.php19」這就是我回到 – da1lbi3

+0

@RyanVincent我已經做到了 – da1lbi3

回答

0

你應該寫在這種情況下,一個圖書館。

在應用程序/庫創建Cart.php(或任何你想要的)

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Cart 
{ 
    protected $ci; 

    public function __construct() 
    { 
     $this->ci =& get_instance(); 
    } 

    public function getPickLocations($partId, $qty) 
    { 
     //Your stuff 
    } 

} 

然後在你的控制器:

$this->load->library("cart"); 
$data = $this->cart->getPickLocations($this->input->post('partId'), $this->input->post('quantity')); 
+0

是的,這是一個選項,但我已閱讀這篇文章http://stackoverflow.com/questions/14947261/codeigniterhmvc-跨模塊調用控制器方法是hmvc模型可以在更短的時間內完成此任務。 – da1lbi3

相關問題