Welcome Guest, Not a member yet? Register   Sign In
Can I load CI instance outside the system folder
#1

[eluser]ganjasensation00[/eluser]
hi guys, i'm making a validation through ajax and I placed it outside the system folder "/site_name/assets/ajax/ajax.php". Can I load the CI instance there?
#2

[eluser]WanWizard[/eluser]
Not if that is a standalone script that bypasses CI's index.php.

I solved this (for both ajax and cron calls) by making a copy of index.php (to p.e. ajax.php). In the routes.php config file, I use
Code:
// deal with CGI mode
if ( isset($_SERVER['ORIG_SCRIPT_NAME']) )
{
    $_SERVER['SCRIPT_NAME'] = $_SERVER['ORIG_SCRIPT_NAME'];
}

// make sure we have a script name. for some reason we sometimes don't...
if ( empty($_SERVER['SCRIPT_NAME']) )
{
    if ( ! empty($_SERVER['SCRIPT_FILENAME']) )
    {
        $_SERVER['SCRIPT_NAME'] = '/'. basename($_SERVER['SCRIPT_FILENAME']);
    }
    elseif ( ! empty($_SERVER['argv'][0]) )
    {
        $_SERVER['SCRIPT_NAME'] = '/'. basename($_SERVER['argv'][0]);
    }
    else
    {
        die("ExiteCMS front controller configuration error: can not determine the front controller loader script!\n");
    }
}

// check which front controller script is used to access ExiteCMS
if ( basename($_SERVER['SCRIPT_NAME']) == 'ajax.php' )
{
    // default, everything is handled by the ajax controller
    $route['default_controller'] = "ajax/index";
    $route['(.*)'] = "ajax/index/$1";
}
elseif ( basename($_SERVER['SCRIPT_NAME']) == 'cron.php' )
{
    // for cron, everything is handled by the cron controller
    $route['default_controller'] = "cron/index";
    $route['(.*)'] = "cron/index/$1";
}
elseif ( basename($_SERVER['SCRIPT_NAME']) == 'index.php' )
{
    // standard application routing here.
    // make sure the cron and ajax controllers are not accessable!
}
else
{
    die("ExiteCMS front controller configuration error: invalid front controller loader script!\n");
}
to route to a fixed controller based on the front controller accessed. In the ajax controller you can deal with the URI segments of the ajax request, with full CI access.

And don't forget to modify your .htacess to make sure requests to ajax.php and cron.php are not rewritten...
#3

[eluser]Nedim ARABACI[/eluser]
Or other way;

Include Codeigniter index.php in ajax.php and modify $system_folder/codeigniter/CodeIgniter.php

Code:
// add sample ajax.php file - same location with main index.php

<?php
    define('OUTSIDE_FIX', TRUE);
    include 'index.php';
    echo $CI->config->item('some_item'); // $CI is global.
?>

Code:
// edit $system_folder/codeigniter/CodeIgniter.php file lines 235:238

<?php
    if (!defined('OUTSIDE_FIX')):
        call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
    endif;
?>
#4

[eluser]PV-Patrick[/eluser]
I am trying your approach Nedim, however I am receiving the following error with CI 2.0.0

Code:
Fatal error: Call to a member function item() on a non-object in /../../CI_system_2.0.0/core/Utf8.php on line 47
#5

[eluser]Nedim ARABACI[/eluser]
[quote author="PV-Patrick" date="1297139666"]I am trying your approach Nedim, however I am receiving the following error with CI 2.0.0

Code:
Fatal error: Call to a member function item() on a non-object in /../../CI_system_2.0.0/core/Utf8.php on line 47
[/quote]

My method is also working with CI 2.0.0. There is no problem.

Check your config class is loaded successfully.
#6

[eluser]PV-Patrick[/eluser]
Maybe I am not doing something correct, here is what I have.

In CI_system_2.0.0/core/CodeIgniter.php Lines: 295-301
Code:
// Call the requested method.
        // Any URI segments present (besides the class/function) will be passed to the method for convenience
        //call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
        if(! defined('OUTSIDE_FIX'))
        {
            call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
        }

The only modified items in my index.php is pointing to the application and system folders.

This is from Functions.php
Code:
function test() {
        
        define('OUTSIDE_FIX', TRUE);
        include "/path/to/index.php";
}

When I run that function I receive the error I posted. Is there something more you changed or edited?
#7

[eluser]PV-Patrick[/eluser]
If anyone else has any suggestions on calling CI superobject from an outside PHP class please let me know, thanks!
#8

[eluser]Nedim ARABACI[/eluser]
Where is your Functions.php? Which path?
#9

[eluser]PV-Patrick[/eluser]
The Functions.php is located in the same folder as the index.php
#10

[eluser]PV-Patrick[/eluser]
Could you possibly post the steps you took for CI 2.0.0? I really appreciate your help Nedim, thank you!




Theme © iAndrew 2016 - Forum software by © MyBB