2017-10-06 54 views
0

我有兩個控制器和一個模型如下,我想調用旁邊模型中的第二個控制器方法。但我不知道如何調用它。Codeigniter呼叫控制器方法從模型

1)控制器1

class controller1 extends CI_Controller { 

    function __construct() { 
     parent::__construct(); 

     $this->load->model('job'); 
    } 

    public function getjob() { 
     $this->job->check_payment(); 
    } 

} 

2)Paypal.php(控制器)

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

class Paypal extends CI_Controller { 

    function __construct() { 
     parent::__construct(); 

     $this->load->model('job'); 

     // Load helpers 
     $this->load->helper('url'); 

     // Load PayPal library 
     $this->config->load('paypal'); 

     $config = array(
      'Sandbox' => $this->config->item('Sandbox'), 
      'APIUsername' => $this->config->item('APIUsername'), 
      'APIPassword' => $this->config->item('APIPassword'), 
      'APISignature' => $this->config->item('APISignature'), 
      'APISubject' => '', 
      'APIVersion' => $this->config->item('APIVersion') 
     ); 

     // Show Errors 
     if ($config['Sandbox']) { 
      error_reporting(E_ALL); 
      ini_set('display_errors', '1'); 
     } 

     $this->load->library('Paypal_pro', $config); 
    } 

    function sendPayemnt() { 
     echo "Hello..."; 
    } 

} 

2)Job.php(模型)

class Job extends CI_Model { 

    function check_payment() { 
     // I want to call method of Paypal controller here... 
    } 

} 

我希望有人幫我解決這個問題。 謝謝,

+1

爲什麼你想編碼出Codeigniter的結構? –

+2

編寫一個幫助函數而不是控制器,並從模型中調用它。這是Codeigniter的正確結構。 –

+0

我知道這是無效的編碼結構,但這只是達到我的要求的方法。 – Hardik

回答

1

雖然它在技術上是可能的,但如果你認爲你需要,它表明你的應用程序的設計存在缺陷。

控制器層是您應用程序的主幹,旨在處理來自用戶的請求,與模型層交談並將View中的輸出縫合在一起。您的模型圖層應該對控制器和視圖無效,但僅處理數據操作。這是對MVC模式的過度簡化的解釋(您可以在其他地方找到資源)。

你可以使用這樣的:

class controller1 extends CI_Controller{ 

public function testsample(){ 
     $this->load->model('modal1'); 
     $stations=$this->modal1->getController(); 
     echo "<pre>"; print_r($stations);exit; 
    } 

    public function getData(){ 
     $ta=array(0=>'Sample',1=>'test'); 
     return $ta; 
    } 
} 

模式:

class modal1 extends CI_Model { 
    function getController() 
    { 
     $controllerInstance = & get_instance(); 
     $controllerData = $controllerInstance->getData(); 
     return $controllerData; 
    } 
} 

輸出:

Array 
(
    [0] => Sample test cases 
    [1] => test 
) 

我將訪問我的控制器操作的控制器1/testsample 你也可以使用捲曲以調用控制器動作

+0

你好,很抱歉,我是codeigniter的新手。所以不知道如何在我的情況下使用你的答案?你能多解釋一下嗎?謝謝, – Hardik

+0

你使用哪個框架? – Sucharitha

+1

我使用codeigniter – Hardik