Senin, 10 September 2018

required, required_once, include Problem in Codeigniter

I had a problem integrating a library downloaded from github in codeigniter. One of the problem is the "required_once" function.

I'm sure there are many ways to solve this problem. I have two ways to share in this post.

Problem:

In every library downloaded from github, we need to call the autoload.php file at the start using "required_once". In some chases, required_once will be used almost in every php file. It is an old way to integrate github library in your code.

First Way:
Load it as a view.
For example, you need to include the autoload.php file.
Put the whole directory of the library inside the directory of 'views' and do this in the controller

controller.php
<?php

public function myFunc()
{
    $this->load->view('yourLib/autoload');
    $this->load->view('yourPageFile');
}
?>

Or you can do that inside the model as well'

aModel.php
<?php

public function modelFunc()
{
    $var=$this->load->view('yourLib/autoload','',true);
    
    your code ....
}
?>

Take a look at the load views. There are 3 parameters of the view function. The first parameter is the file of the page that you want to show, second parameter is for the variables that will be used in the page and the third parameter is set to TRUE. It will tell the CI that you want to return a value from view. If you don't set it to TRUE, it will return an CI-Object.

Second Way:
Use APPPATH macro. APPPATH is a macro referring your application Path.

If I do this
include_once APPATH."views/myLib/autoload.php";

that will give you  
include_once "c:\xampp\htdocs\myApp\application\views\myLib\autoload.php";

I hope this can help you somehow

Tidak ada komentar:

Posting Komentar