1

我是 Codeigniter 的新手,我正在研究 API 类,以下是 API 代码

<?php
require_once("WiZiQService.php");
class ScheduleClass{

    function ScheduleClass($secretAcessKey,$access_key,$webServiceUrl)
    {
        require_once("AuthBase.php");
        $authBase = new AuthBase($secretAcessKey,$access_key);
        $method = "create";
        $requestParameters["signature"]=$authBase->GenerateSignature($method,$requestParameters);
        #for teacher account pass parameter 'presenter_email'
                //This is the unique email of the presenter that will identify the presenter in WizIQ. Make sure to add
                //this presenter email to your organization’s teacher account. ’ For more information visit at: (http://developer.wiziq.com/faqs)
        $requestParameters["presenter_email"]=$By_PresenterEmail;//"kerrygun@gmail.com";
        #for room based account pass parameters 'presenter_id', 'presenter_name'
        //$requestParameters["presenter_id"] = "40";
        //$requestParameters["presenter_name"] = "vinugeorge";  
        $requestParameters["start_time"] = $By_StartTime;//"2011-12-25 11:55";
        $requestParameters["title"]=$By_ClassTitle;//"my php-class"; //Required
        $requestParameters["duration"]=$By_ClassDuration; //optional
        $requestParameters["time_zone"]=$By_TimeZone; //optional
        $requestParameters["attendee_limit"]=$By_AttendeeLimit; //optional
        $requestParameters["control_category_id"]=$By_ControlCategory; //optional
        $requestParameters["create_recording"]=$By_CreateRecording; //optional
        $requestParameters["return_url"]=$By_ReturnUrl; //optional
        $requestParameters["status_ping_url"]=$By_StatusPingUrl; //optional
                $requestParameters["language_culture_name"]=$By_LanguageCulture;
        $httpRequest=new HttpRequest();
        try
        {
            $XMLReturn=$httpRequest->wiziq_do_post_request($webServiceUrl.'?method=create',http_build_query($requestParameters, '', '&')); 
        }
        catch(Exception $e)
        {   
            echo $e->getMessage();
        }
        if(!empty($XMLReturn))
        {
            try
            {
              $objDOM = new DOMDocument();
              $objDOM->loadXML($XMLReturn);

            }
            catch(Exception $e)
            {
              echo $e->getMessage();
            }
        $status=$objDOM->getElementsByTagName("rsp")->item(0);
        $attribNode = $status->getAttribute("status");
        if($attribNode=="ok")
        {
            $methodTag=$objDOM->getElementsByTagName("method");
            echo "method=".$method=$methodTag->item(0)->nodeValue;
            $class_idTag=$objDOM->getElementsByTagName("class_id");
            echo "<br>Class ID=".$class_id=$class_idTag->item(0)->nodeValue;
            $recording_urlTag=$objDOM->getElementsByTagName("recording_url");
            echo "<br>recording_url=".$recording_url=$recording_urlTag->item(0)->nodeValue;
            $presenter_emailTag=$objDOM->getElementsByTagName("presenter_email");
            echo "<br>presenter_email=".$presenter_email=$presenter_emailTag->item(0)->nodeValue;
            $presenter_urlTag=$objDOM->getElementsByTagName("presenter_url");
            echo "<br>presenter_url=".$presenter_url=$presenter_urlTag->item(0)->nodeValue;
        }
        else if($attribNode=="fail")
        {
            $error=$objDOM->getElementsByTagName("error")->item(0);
            echo "<br>errorcode=".$errorcode = $error->getAttribute("code");    
            echo "<br>errormsg=".$errormsg = $error->getAttribute("msg");   
        }
     }//end if  
   }//end function


}
?>

所以我的问题是,我如何将值从控制器传递到附加的外部类,并且输出应该返回到控制器,然后将其发送到视图。

我正在尝试像这样使用它:

class Form extends CI_Controller {
    public function index() {
        $this->load->library('Create');
        $this->load->view('form_view');
    }
    public function wizprocess() {
        $this->load->library('Create');
        $this->Create->ScheduleClass();
    }
}

您对上述问题的解决方案将不胜感激

4

1 回答 1

1

基本上有两种方法可以做到这一点,Codeigniter 方式:

$this->load->library('scheduleclass');
$this->scheduleclass->__construct($secretAcessKey, $access_key, $webServiceUrl);

我不太喜欢这样,CI 假设您的“库”构造函​​数采用一个参数:一组配置。如果你改变你的类来配合这个,你可以像这样加载它:

$config = array(
    'secretAcessKey'    =>  $secretAcessKey,
    'access_key'        =>  $access_key,
    'webServiceUrl'     =>  $webServiceUrl
);
$this->load->library('scheduleclass', $config);

然后你的类可以extract($config)或其他东西来确保定义了正确的变量。

我的建议是直接使用 PHP:

// Either include the file somewhere or use an autoloader
include APPPATH.'third_party/ScheduleClass.php';

然后一切如常,只是你没有使用 CI 的加载器,只是用普通的 PHP 创建新对象。

// In your Controller
function mymethod()
{
    $class = new ScheduleClass($secretAcessKey, $access_key, $webServiceUrl);
    $this->load->view('myview', array(
        'ScheduleClass' =>  $class
    ));
}

一个大问题是你的类的糟糕设计,它只有一个方法(构造函数)并且它直接echo是所有东西而不是return一个值,所以当你创建它的那一刻 - 所有的输出都将被发送到浏览器。

于 2012-10-15T16:31:22.907 回答