我正在尝试将变量值从库函数传递到 Controller ,然后查看以获取输出。
为此,我的库代码:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* @description : Library to access MyOperator Public API
*/
Class My_Operator extends Admin_controller{
protected $developers_url = 'https://developers.myoperator.co/';
protected $token = 'XXXXXXXXX';
function __construct() {
}
public function run() {
# request for Logs
$url = $this->developers_url . 'search';
$fields = array("token" => $this->token);
$result = $this->_post_api($fields, $url);
$this->log("result");
$this->log($result);
}
private function _post_api(Array $fields, $url) {
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
} catch (Exception $e) {
return false;
}
$this->log("url");
$this->log($url);
$this->log("fields");
$this->log($fields);
curl_close($ch);
if ($result)
return $result;
else
return false;
}
private function log($message) {
print_r($message);
echo "\n";
}
}
扩展 从核心文件扩展 Admin_controller
。
我的 Controller 代码是:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Myoperator extends Admin_controller
{
public function __construct()
{
parent::__construct();
//$this->load->model('myoperator_model');
}
public function index()
{
try {
$this->load->library('my_operator');
$data = $this->my_operator->run();
} catch (Exception $e) {
var_dump($e->getMessage());
}
$this->load->view('admin/myoperator/view', $data);
}
}
我的 View 代码是:
<?php init_head(); ?>
<div id="wrapper">
<div>
<?php
echo $this->MY_Operator->run();
?>
</div>
</div>
<?php init_tail(); ?>
</body>
</html>
我不会得到想要的输出。 为此,我引用了:
codeigniter data passing controller->library->view
Pass data from library to controller and then to view in CodeIgniter 2
passing parameter from view to library and return after process Code igniter
当我运行这段代码时出现错误
This page isn’t working localhost is currently unable to handle this request. HTTP ERROR 500
根据我的理解,如果服务器配置有任何问题,就会出现此错误
,但同一应用程序的其他页面运行完美。
已编辑: 以前的错误已解决,一些链接问题,但是当我扩展该类时,出现新错误:
Unable to locate the specified class: Session.php
是否可以将核心文件类扩展为库类?
请帮我解决这个问题。任何形式的帮助表示赞赏。提前致谢。
最佳答案
解决了我的问题,只是忘了在 Myoperator
Controller 文件中包含 Admin_controller
文件。
这是我更新的代码:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH. 'core/Admin_controller.php'; //forgot this line of code
class Myoperator extends Admin_controller
{
public function __construct()
{
parent::__construct();
//$this->load->model('myoperator_model');
}
public function index()
{
try {
$this->load->library('my_operator');
$data = $this->my_operator->run();
} catch (Exception $e) {
var_dump($e->getMessage());
}
$this->load->view('admin/myoperator/view', $data);
}
}
关于php - 将变量值从库传递到 Controller ,然后在 CI 3.0 中查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42802665/