Welcome Guest, Not a member yet? Register   Sign In
How to use codeigniter helper, model, library etc from a php file
#1

[eluser]cinewbie81[/eluser]
The following is my code:
Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Test extends Controller {

    function Test()
    {    
        parent::Controller();
        $this->load->helper(array('url','form'));
    }

    function index()
    {    
         exec("nohup php /var/www/myproject/backendprocess.php > /dev/null &");
    }
}
?>

backendprocess.php
Code:
<?php
  // How to use codeigniter helper, model, library file here ???
?>
My question is: How can i call codeigniter helper, model, or library file in my backendprocess.php ??
I try
Code:
$obj =& get_instance();            
    $obj->load->helper(array('url', 'globalsetting'));
it's not working though Sad

Just for you all information, backendprocess.php.php is located in the same path as /system folder. Hope to hear from you guys soon, thanks
#2

[eluser]xwero[/eluser]
Try to write a custom config variable to a file that way you know the get_instance has passed the CI object.
#3

[eluser]cinewbie81[/eluser]
Hi xwero, I dont quite get what do you mean ..
Mind to elobarate ?
#4

[eluser]ejangi[/eluser]
No, I think you need to replicate the index.php file basically - I'm actually working on this same type of thing as we speak - let you know if I have much success.
#5

[eluser]xwero[/eluser]
[quote author="cinewbie81" date="1199967107"]
Code:
$obj =& get_instance();            
    $obj->load->helper(array('url', 'globalsetting'));
[/quote]
instead of this try
Code:
$obj =& get_instance();            
    $filename = 'path/to/file/test.txt';
            if (!$handle = fopen($filename, 'w')) {
                show_error("Cannot open file ($filename)");
                exit;
            }
          $filecontent = $obj->config->item('base_url');
        if (fwrite($handle, $filecontent) === FALSE) {
                show_error("Cannot write to file ($filename)");
                exit;
            }
                        
            fclose($handle);
If nothing gets added to the file the CI object isn't passed on to the $obj variable.
#6

[eluser]ejangi[/eluser]
Okay... So I have worked with Ruby on Rails for a while now and I really miss all my command line goodies - So, last night I decided to try and replicate some stuff in CI. To do that I needed to figure out how to run CI from the command-line. This is very early stages (messy, but working) - I thought I should post it as is so you can get on with your work cinewbie81:

Code:
#!/usr/bin/php
<?php
error_reporting(E_ALL);

// Absolute path to the system folder:
$system_folder = "/Users/ucantblamem/Sites/Codeigniter_1.5.4/system";
// Absolute path to the application folder:
$application_folder = "/Users/ucantblamem/Sites/projector/application";


/*
|===============================================================
| END OF USER CONFIGURABLE SETTINGS
|===============================================================
*/

define('EXT', '.php');
define('FCPATH', __FILE__);
define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));
define('BASEPATH', $system_folder.'/');
define('APPPATH', $application_folder.'/');


if ( ! defined('E_STRICT'))
{
    define('E_STRICT', 2048);
}

define('CI_VERSION',    '1.5.4');

require(BASEPATH.'codeigniter/Common'.EXT);

set_error_handler('_exception_handler');
set_magic_quotes_runtime(0); // Kill magic quotes

$BM =& load_class('Benchmark');
$BM->mark('total_execution_time_start');
$BM->mark('loading_time_base_classes_start');

$EXT =& load_class('Hooks');

$EXT->_call_hook('pre_system');

$CFG =& load_class('Config');
$RTR =& load_class('Router');
$OUT =& load_class('Output');

if ($EXT->_call_hook('cache_override') === FALSE)
{
    if ($OUT->_display_cache($CFG, $RTR) == TRUE)
    {
        exit;
    }
}

$IN        =& load_class('Input');
$URI    =& load_class('URI');
$LANG    =& load_class('Language');

if (floor(phpversion()) < 5)
{
    load_class('Loader', FALSE);
    require(BASEPATH.'codeigniter/Base4'.EXT);
}
else
{
    require(BASEPATH.'codeigniter/Base5'.EXT);
}

$CI =& load_class('Controller');
$BM->mark('loading_time_base_classes_end');

$EXT->_call_hook('pre_cli');
// READY TO GO HERE:
$CI->lang->load('scaffolding');
echo $CI->lang->line('scaff_yes')."\n";
$EXT->_call_hook('post_cli');

if (class_exists('CI_DB') AND isset($CI->db))
{
    $CI->db->close();
}
?&gt;

One important thing to note is that I couldn't get it working with the usual relative paths, nor would it connect to the DB if I just had the "hostname" as localhost. I ended up having to change "hostname" to the socket (i.e. :/Applications/MAMP/tmp/mysql/mysql.sock). I imagine this is a standard PHP CLI thing...
#7

[eluser]Grahack[/eluser]
These may help:
http://codeigniter.com/wiki/dip_into_CI/comma,d
http://codeigniter.com/wiki/Calling_CI_m...de_script/
http://codeigniter.com/wiki/Category:Adv...ronScript/

I'd swear I've already read something about CI through CLI in the wiki but I can't find the article...
#8

[eluser]Peter Ivanov[/eluser]
[quote author="cinewbie81" date="1199967107"]The following is my code:
Code:
&lt;?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Test extends Controller {

    function Test()
    {    
        parent::Controller();
        $this->load->helper(array('url','form'));
    }

    function index()
    {    
         exec("nohup php /var/www/myproject/backendprocess.php > /dev/null &");
    }
}
?&gt;

backendprocess.php
Code:
&lt;?php
  // How to use codeigniter helper, model, library file here ???
?&gt;
My question is: How can i call codeigniter helper, model, or library file in my backendprocess.php ??
I try
Code:
$obj =& get_instance();            
    $obj->load->helper(array('url', 'globalsetting'));
it's not working though Sad

Just for you all information, backendprocess.php.php is located in the same path as /system folder. Hope to hear from you guys soon, thanks[/quote]




It's pretty simple if you have CURL installed in your ssh console, just expose your backend process somewhere where you can access it via browser and change your index() function to this

Code:
function index()
    {    
         exec("curl http://example.com/path_to_backend_function/blabla...");
    }

if you don't have curl you can do this even with wget

Code:
function index()
    {    
         exec("wget -b http://example.com/path_to_backend_function/blabla...");
    }
#9

[eluser]cinewbie81[/eluser]
Hi all,

One question here:
What if i want to pass something with a space as the parameter using wget function, something like following:

function index()
{
exec("wget -b http://example.com/mycontroller/myfucntion/param1/this is param 2");
}
In mycontroller, i can get param1 using $this->uri->segment(3), but i cant get the full string for next param (this is param 2) because the URL contain a blank space..

any idea ??




Theme © iAndrew 2016 - Forum software by © MyBB