Welcome Guest, Not a member yet? Register   Sign In
ExecScript, run a controler from shell
#1

[eluser]XMadMax[/eluser]
ExecScript can run any controller from command line (windows or linux), and can be the same for development and production environments.

Instructions:

I normally install codeigniter as follows:

application
system
www

Inside www, I modify index.php to refer system_path and application_folder as :
Code:
$system_path = '../system';
$application_folder = '../application';

With this configuration, create the directory 'scripts' under your base directory:

application
www
system
scripts

Inside scripts directory, create a execScript.php file with :

Code:
#!/usr/local/bin/php
<?php

/**
* execScript.php param1 param2
*
* param1 = controller/method as typed in url
* param2 = host
*
* Examples:
*
*   php execScript.php welcome myhost.com
*   php execScript.php mymodule/mymethod myhost.com
*   php execScript.php mycontroller/mymethod myhost.com
*/

// Set host
$HTTP_HOST = $argv[2];

// Unset second parameter
unset ($argv[2]);

// Set all parameters in argv
$_SERVER["argv"] = $argv;

// If not set host, exit
if ($HTTP_HOST == '')
        return -1;

// Set time limit and memory for script execution
set_time_limit(0);
ini_set('memory_limit', '512M');

// If this script are called from any other source, exit
if (isset($_SERVER['REMOTE_ADDR'])) die('Permission denied.');

// Define commmand line script, your can protect your controller checking this.
// First line of your controller (if only allowed to be executed from commandline):  
//  <?php if (!defined("COMMAND_LINE_SCRIPT")) die('Bad request');
define('COMMAND_LINE_SCRIPT', 1);

// Unset argv for this php
unset($argv[0]);

// Set necessary server info
$_SERVER['PATH_INFO'] =  '/' . implode('/', $argv) . '/';
$_GET = ''; // Required for some installations
$_SERVER['REQUEST_URI'] =  $_SERVER['PATH_INFO'];
$_SERVER['HTTP_HOST'] = $HTTP_HOST;

// Include index.php as normal call
include_once('../www/index.php');

You can go to command line to the scripts directory and type:

Code:
php -q execScript.php welcome myhost.com

This is exactlly the same than call:
Code:
http://myhost.com/welcome
Remember to change myhost.com to an appropiate vhost






Theme © iAndrew 2016 - Forum software by © MyBB