Welcome Guest, Not a member yet? Register   Sign In
call a class from outside the systemfolder
#1

[eluser]pumuckel[/eluser]
Hi everybody!

I like to call a class which is defined in a file located in system/application/controllers/ from outside the system folder.

for example:

my test.php
Code:
require_once( 'system/application/controllers/Myclass.php' );

$myclass = new Myclass;

Myclass.php
Code:
class Myclass extends Controller
{
   function Myclass()
    {
       parent::Controller();
        $this->load->library( 'zip' );
        $this->load->library('parser');
    }

}


if i call the test.php this error appears:
Code:
Fatal error: Class 'Controller' not found in
OK in this case the BASPATH is not correct.
I tried to put the code from the index.php into the 'system/application/controllers/Myclass.php' file.
When i call the test.php again i get the welcome-page an an other error appears:

Code:
Fatal error: Call to a member function append_output() on a non-object in /var/www/codeigniter/htdocs/ci/system/libraries/Loader.php on line 708

if i call it on the normal way "http://localhost/ci/index.php/myclass" everything is ok.
#2

[eluser]Brandon Beasley[/eluser]
Why wouldn't you load the class as a library?
#3

[eluser]pumuckel[/eluser]
[quote author="Brandon Beasley" date="1284748397"]Why wouldn't you load the class as a library?[/quote]
Because i cannot user CI functions outside the CI folders.

the file which should call the class can be everywhere.
#4

[eluser]WanWizard[/eluser]
CodeIgniters's functionality is built around the CI superobject, which is created by the front controller (starting with index.php). You can't load and call any CI file directly and expect it to work.
#5

[eluser]pumuckel[/eluser]
[quote author="WanWizard" date="1284749147"]CodeIgniters's functionality is built around the CI superobject, which is created by the front controller (starting with index.php). You can't load and call any CI file directly and expect it to work.[/quote]

You mean the Controller.php
if i include it the error appears "No direct script access allowed". OK, this is logical.

Could you tell me what i have to do in the Myclass.php?
#6

[eluser]pumuckel[/eluser]
[quote author="WanWizard" date="1284749147"]CodeIgniters's functionality is built around the CI superobject, which is created by the front controller (starting with index.php). You can't load and call any CI file directly and expect it to work.[/quote]
I had not read correctly your post. But how can I realize this. Call a my own Class from outsite CI folders?
#7

[eluser]WanWizard[/eluser]
The way you phrase your question is quite confusing.

Do you have:
a) a non-CI application that wants to call a CI class
b) a CI application that wants to call a non-CI class

I have understood that the question is about a). To which I replied that it is not possible, any CI class depends on the CI superobject that is created by the front controller (which is started by index.php).

What you can do, is write a library. Libraries in CI are standard classes. If you make sure that this library doesn't have any CI dependencies (i.e. doesn't use get_instance(), $this->load, etc...), you can use the code in CI by loading the class as a library, and from another application by manually including it, and then instantiate it using 'new'.
#8

[eluser]pumuckel[/eluser]
[quote author="WanWizard" date="1284757242"]The way you phrase your question is quite confusing.

Do you have:
a) a non-CI application that wants to call a CI class
b) a CI application that wants to call a non-CI class

I have understood that the question is about a). To which I replied that it is not possible, any CI class depends on the CI superobject that is created by the front controller (which is started by index.php).

What you can do, is write a library. Libraries in CI are standard classes. If you make sure that this library doesn't have any CI dependencies (i.e. doesn't use get_instance(), $this->load, etc...), you can use the code in CI by loading the class as a library, and from another application by manually including it, and then instantiate it using 'new'.[/quote]

OK!

At first thank's for your answers.

I have an easy solution for my problem.
The function "get_instance()"

for example:
Code:
require_once("index.php");
$CI = get_instance();

Then you have the Superobject "$CI".
Look at
http://ellislab.com/codeigniter/user-gui...aries.html
"Utilizing CodeIgniter Resources within Your Library"




Theme © iAndrew 2016 - Forum software by © MyBB