Welcome Guest, Not a member yet? Register   Sign In
CI indepandant Libraries
#1

[eluser]majidmx[/eluser]
Hi everyone,

We have the main application written on top of codeigniter, but because of some constraints we have to write some parts of the codes as a stand alone PHP scripts, without loading the CI core.

Some tasks are common between the CI main and these stand alone scripts, We don't want to have all the libraries duplicated in both places.

Is there a way which we can have a library in CodeIgniter, while it can be accessible from the outside world without CI and the need to load the core?

Thanks,
MajiD
#2

[eluser]bretticus[/eluser]
[quote author="majidmx" date="1256781996"]
Is there a way which we can have a library in CodeIgniter, while it can be accessible from the outside world without CI and the need to load the core[/quote]

Not sure by what you mean by "accessible from the outside world" but just leave any CI code out of your libraries and you can call them in the normal fashion from another script outside of the framework (ie. CI &= get_instance() .) All you have to do is include the library file with your class definition.
#3

[eluser]majidmx[/eluser]
Thanks for your quick reply,

That's what I'm trying to achieve.
The following lines are only recognized by CI :
Code:
$this->load->model('some_model');
$this->some_model->some_func();

And they're almost inevitable when you're coding based on CI and honestly that's the beauty of CI.
But let's say I want to have a library which works both when I load it in CI, and when I include it in a script Independent than CI.

Consider the following library :

Code:
class TEST_Log{
    var $_ci;
    
    function TEST_Log(){        
        $this->_ci =& get_instance();
        $this->_ci->load->model("Log_model");
    }

    function Log($message = '', $message_type = 'Error' ){
        $this->_ci->Log_model->insertLog($message_type , $message);
    }
}

Now let's say I have a PHP file as follow :
Code:
<?PHP
    include("system/application/libraries/TEST_log.php");
?>

I can not simply use this :
Code:
$my_log = new TEST_log();

because it has no clue about get_instance and the CI structure. And actually I don't want it load the CI, because the whole point for us is not to load CodeIgniter, and be independent of it.

Let me know if you have any idea,

Thanks,
MajiD
#4

[eluser]bretticus[/eluser]
Pass those values to the class instead of calling them in the constructor. However in the example you have shown you are using a CI model. Not sure how you'd ever plan on using that class without using the CI framework.
Code:
class TEST_Log{
    var $_ci;
    
    function TEST_Log(){        
        $this->_ci =& get_instance();
        $this->_ci->load->model("Log_model");
    }

    function Log($message = '', $message_type = 'Error' ){
        $this->_ci->Log_model->insertLog($message_type , $message);
    }
}

In other words, if you want the class to be instantiate-able outside of the framework, you cannot use framework objects within the class. I suggest if you need to use database functionality in your uncoupled classes that you instantiate a database connection within that class instead. Otherwise, you can pass variables to classes in CI via the second parameter (see the manual.) And as for any outside-framework code, call it in the usual way (new Object(param,...) )
#5

[eluser]Phil Sturgeon[/eluser]
You can use a library anywhere as it is just a PHP class, but of course if you use CodeIgniter code within the library you are creating dependencies.

Models outside of CodeIgniter do not work at all, as they require the Model class, the DB driver classes, the ActiveRecord class, etc.

If you wish to use CodeIgniter Models and have your business logic central to your CodeIgniter applications, but also wish for other PHP apps to interact with these models, consider looking into REST instead.




Theme © iAndrew 2016 - Forum software by © MyBB