Standalone PHP script using Expression Engine

ⅰ亾dé卋堺 提交于 2019-12-04 12:12:15
Zenbuman

The following article seems to have a possible approach: Bootstrapping EE for CLI Access

  • Duplicate your index.php file and name it cli.php.
  • Move the index.php file outside your DOCUMENT_ROOT. Now, technically, this isn’t required, but there’s no reason for prying eyes to see your hard work so why not protect it.
  • Inside cli.php update the $system_path on line 26 to point to your system folder.
  • Inside cli.php update the $routing['controller'] on line 96 to be cli.
  • Inside cli.php update the APPPATH on line 96 to be $system_path.'cli/'.
  • Duplicate the system/expressionengine directory and name it system/cli.
  • Duplicate the cli/controllers/ee.php file and name it cli/controllers/cli.php.
  • Finally, update the class name in cli/controllers/cli.php to be Cli and remove the methods.
  • By default EE calls the index method, so add in an index method to do what you need.

@Zenbuman This was useful as a starting point although I would add I had issues with all of my requests going to cli -> index, whereas I wanted some that went to cli->task1, cli->task2 etc

I had to update *system\codeigniter\system\core\URI.php*so that it knew how to extract the parameters I was passing via the command line, I got the code below from a more recent version of Codeigniter which supports the CLI

// Is the request coming from the command line?
if (php_sapi_name() == 'cli' or defined('STDIN'))
{
    $this->_set_uri_string($this->_parse_cli_args());
    return;
}

// Let's try the REQUEST_URI first, this will work in most situations

and also created the function in the same file

private function _parse_cli_args()
{
    $args = array_slice($_SERVER['argv'], 1);

    return $args ? '/' . implode('/', $args) : '';
}

Also had to comment out the following in my cli.php file as all routing was going to the index method in my cli controller and ignoring my parameters

/*
 *  ~ line 109 - 111 /cli.php
 * ---------------------------------------------------------------
 *  Disable all routing, send everything to the frontend
 * ---------------------------------------------------------------
 */
$routing['directory'] = '';
$routing['controller'] = 'cli';
//$routing['function'] = '';

Even leaving

$routing['function'] = '';

Will force requests to go to index controller

In the end I felt this was a bit hacky but I really need to use the EE API library in my case. Otherwise I would have just created a separate application with Codeigniter to handle my CLI needs, hope the above helps others.

I found @Zenbuman's answer after solving my own variation of this problem. My example allows you to keep the cron script inside a module, so if you need your module to have a cron feature it all stays neatly packaged together. Here's a detailed guide on my blog.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!