Passing the value of a variable from the library to the controller, and then for viewing in CI 3.0

I am trying to pass the value of a variable from a library function to a controller and then look through to get the result.

For this, my library code:

<?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";
    }

}

extended extends Admin_controllerfrom the main file.

My controller code:

<?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);
   }
}

And my view code:

<?php init_head(); ?>
    <div id="wrapper">
        <div>
            <?php
                echo $this->MY_Operator->run();
            ?>
        </div>
    </div>
<?php init_tail(); ?>

</body>
</html>

I will not get the desired result. for this I replied:

codeigniter passes data to the controller> library->

Transferring data from the library to the controller and then viewing in CodeIgniter 2

pass the parameter from the view to the library and return the code igniter after the process

When I run this code, I received an error message

This page does not work. localhost currently cannot handle this. inquiry. HTTP ERROR 500

, error , - , .

: , , , :

: Session.php

?

, . . .

+4
2

, Admin_controller Myoperator.

:

<?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);
   }
}
+2

CI core .

.

.

.

. Codeigniter

core CI files library.Create CI global instance using

$CI =& get_instance();

$CI.

+1

Source: https://habr.com/ru/post/1672322/


All Articles