Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing variable value from library to controller and then to view in CI 3.0

I'm trying to pass variable value from library function to controller and then view to get output.

For that 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_controller from core file.

My Controller code is :

<?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 is :

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

</body>
</html>

I won't get desired output. for this I refereed :

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

When I'm running this code I got an error

This page isn’t working localhost is currently unable to handle this request. HTTP ERROR 500

As per my understanding this error comes if there is any problem in server configuration, but the other pages from same application runs perfectly.

edited : Previous error solved, some linking problem, but when I'm extended that class, gives new error:

Unable to locate the specified class: Session.php

is it possible to extend core file class into library class ?

Please help me to solve this problem. Any kind of help is appreciated. Thanks in advance.

like image 839
Ganesh Aher Avatar asked Oct 18 '22 15:10

Ganesh Aher


2 Answers

Solved my problem, just forgot to include Admin_controller file in Myoperator controller file.

here is my updated code :

<?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);
   }
}
like image 143
Ganesh Aher Avatar answered Oct 21 '22 06:10

Ganesh Aher


You can not extend core class of CI from library.But

You can create entirely new libraries.

You can extend native libraries.

You can replace native libraries.

for more see Codeigniter Library

to use core CI files in library.Create CI global instance using

$CI =& get_instance();

Then load required core files using $CI.

like image 20
Hikmat Sijapati Avatar answered Oct 21 '22 04:10

Hikmat Sijapati