Standalone PHP script using Expression Engine

2019-04-11 12:58发布

Is there a way to make a script where I can do stuff like $this->EE->db (i.e. using Expression Engine's classes, for example to access the database), but that can be run in the command line?

I tried searching for it, but the docs don't seem to contain this information (please correct me if I'm wrong). I'm using EE 2.4 (the link above should point to 2.4 docs).

3条回答
Evening l夕情丶
2楼-- · 2019-04-11 13:27

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.

查看更多
爷的心禁止访问
3楼-- · 2019-04-11 13:41

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.
查看更多
beautiful°
4楼-- · 2019-04-11 13:49

@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.

查看更多
登录 后发表回答