Welcome Guest, Not a member yet? Register   Sign In
A new way of using CodeIgniter? Want to know if this is possible....
#1

[eluser]swhitlow[/eluser]
Ok - I need to use CodeIgniter a little different than it is intended to be used. But, not that far from it's original path.

Instead of calling an initial page view like this: http://localhost/sitename/test

I need to be able to call the class functions from within my PHP application that is already built. Basically, I need to include the index page to fire off the initial processes, and then change some code in the CodeIgniter.php file to look for an array. And then be able to instantiate the object. For example:

I have a controller named test.php in the appplication/test/ folder. It has this:
Code:
class Test extends Controller {
    function Test()
    {
        parent::Controller();
    }

    function get_contacts(){
        $temp = '';
        $this->load->database();
        $this->load->library('Table');
        $this->db->select('first_name, last_name');
        $this->db->order_by('last_name');
                
        $ret_val = $this->db->get('contacts');
        
        return $this->table->generate($ret_val);
    }

}

Then, I have a PHP file called testci.php in my root directory that has this code:
Code:
<?php
    
    $controller = array('uri_string' => 'test','controller' => 'test');
    require_once $_SERVER['DOCUMENT_ROOT'] . '/new_app/index.php';
    
    $temp = new Test();
    echo $temp->get_contacts();
?>

Then, I have changed the CodeIgniter.php file to look for the $controller array if it exists:
Code:
$CFG =& load_class('Config');
$URI =& load_class('URI');
$RTR =& load_class('Router');
$OUT =& load_class('Output');

/***** BEGIN - CUSTOM CODE - by SWHITLOW - Wed May 28 00:59:09 EDT 2008 *****/
if(array_key_exists('controller',$controller)) {
    $uri = $controller['uri_string'];
    $uri_string = "/" . $uri . "/" . $uri;
    $URI->uri_string = $uri_string;
    $URI->segments[1] = $uri;
    $URI->segments[2] = $uri;
    $URI->rsegments[1] = $uri;
    
    $RTR->class = $controller['controller'];
    if(!array_key_exists('method', $controller)) {
        $RTR->method = ucwords($controller['controller']);
    } else {
        $RTR->method = $controller['method'];
    }
    $RTR->directory = $uri . '/';
    $RTR->uri_string = $uri_string;
    $RTR->segments[1] = $uri;
    $RTR->segments[2] = $uri;
    $RTR->rsegments[1] = $uri;
    
}

However, when I view this page, I am getting this error:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Test::$table

Filename: test/test.php

Line Number: 24

Fatal error: Call to a member function generate() on a non-object in /Applications/MAMP/htdocs/pharma/dp_framework/libs/controllers/DPFramework/system/application/controllers/test/test.php on line 24

It's almost like it is not loading the table library. However, I turned on debug and checked the log file. And it says that everything is loaded fine:
Quote:DEBUG - 2008-05-28 14:40:56 --> Config Class Initialized
DEBUG - 2008-05-28 14:40:56 --> Hooks Class Initialized
DEBUG - 2008-05-28 14:40:56 --> URI Class Initialized
DEBUG - 2008-05-28 14:40:56 --> No URI present. Default controller set.
DEBUG - 2008-05-28 14:40:56 --> Router Class Initialized
DEBUG - 2008-05-28 14:40:56 --> Output Class Initialized
DEBUG - 2008-05-28 14:40:56 --> Input Class Initialized
DEBUG - 2008-05-28 14:40:56 --> Global POST and COOKIE data sanitized
DEBUG - 2008-05-28 14:40:56 --> Language Class Initialized
DEBUG - 2008-05-28 14:40:56 --> Loader Class Initialized
DEBUG - 2008-05-28 14:40:56 --> Database Driver Class Initialized
DEBUG - 2008-05-28 14:40:56 --> Table Class Initialized
DEBUG - 2008-05-28 14:40:56 --> Controller Class Initialized
DEBUG - 2008-05-28 14:40:56 --> Controller Class Initialized
DEBUG - 2008-05-28 14:40:56 --> Final output sent to browser
DEBUG - 2008-05-28 14:40:56 --> Total execution time: 0.1615
DEBUG - 2008-05-28 14:40:56 --> Controller Class Initialized
DEBUG - 2008-05-28 14:40:56 --> Database Driver Class Initialized
DEBUG - 2008-05-28 14:40:56 --> Table class already loaded. Second attempt ignored.
ERROR - 2008-05-28 14:40:56 --> Severity: Notice --> Undefined property: Test::$table /Applications/MAMP/htdocs/pharma/dp_framework/libs/controllers/DPFramework/system/application/controllers/test/test.php 24

So, my question is (sorry about the long post) is there a better way to do what I am wanting to do? And if so, how? If not, what can I try to do to get this to work?

Thanks!
#2

[eluser]louis w[/eluser]
Little confused, what are you trying to do? Send all requests to one central class/method and deal with it in there? If so, this can be done with routes.
#3

[eluser]swhitlow[/eluser]
I guess what I need to do is be able to call the objects created in the controllers from any outside php file. What would be the best way to do that?

I guess, think about the more classic php file scenario like this:
<?php

require_once('classes/objects/exampleClass.php');

$obj = new ExampleClass();
echo $obj->some_function();
?>

Does this help at all? I am wanting to bascially be able to use all the DB connections, helpers, etc. but by calling all of these things from any php file outside of the structure of CodeIgniter.
#4

[eluser]louis w[/eluser]
I think this would be very hard to accomplish. Firstly you would have to set all the variables which index.php sets. Then you would have to manually include all the CI files. And finally would have to reference everything through the &get;_instance() function.

Why don't you want to do this all inside of a controller in CI?
#5

[eluser]swhitlow[/eluser]
Because I am wanting to use CodeIgniter as a framework for another PHP application that has already been built. The only problem with other PHP application is that extending it can sometimes prove to be problematic. So, I was thinking about using CodeIgniter as the heart of the framework to use for development. Basically, it would provide me with all the basics I needed (DB CRUD, Table Generations, Forms, etc.) And allow me to work for effectively.

Would there be any hints or examples you could provide me to at least try? It seems like I almost have it since I am including the index.php file and throwing an array of the controller and method I need to instantiate. And with the debugger messages it sure seems like I almost have it since it is initializing all the classes.

Could you look at my example on the first post of this message and see if there is anything else I would need to do?

I would really appreciate it!
#6

[eluser]louis w[/eluser]
Could the other application be loaded as a library into CI? Is there one central class?
#7

[eluser]swhitlow[/eluser]
Nope. It would require a major, major overhaul to do it that way. I feel like I almost have it with CI since I am able to get data back with my example above. It just gave me an error when I tried to work with the table generator. I'm sooo close.... Sad
#8

[eluser]louis w[/eluser]
Just curious, but have you considered re-writing it to fit into CI?

Seems that since you have already built it once you know exactly how it should function so recreating it into a CI application would not take much time at all. It's quite amazing how fast development under CI is.
#9

[eluser]swhitlow[/eluser]
I know. I love CI and how fast the development is. But this application is not held by me but another company. I am just working with it. So, I would have to re-write everything, every time a new update came out for the new application. It is just not feasible at all to think about going that route.

So, would you happen to have any suggestions I could try at all?
#10

[eluser]swhitlow[/eluser]
Just thought I would let everyone know that I got this to work. I am now able to call CI Controller's methods and get results back much fast than I was able to in the other PHP application I am working with. I figured it out by looking at this thread:
http://ellislab.com/forums/viewthread/49423/




Theme © iAndrew 2016 - Forum software by © MyBB