Php Phalcon CLI任务名称
我有maintash.php文件夹任务和其他一些task.php文件 如何仅运行SomeOther任务?Php Phalcon CLI任务名称,php,phalcon,Php,Phalcon,我有maintash.php文件夹任务和其他一些task.php文件 如何仅运行SomeOther任务? use Phalcon\Cli\Task; class SomeTask extends Task { public function MainAction() { echo "This is the main action\r\n"; } public function OtherAction() { echo
use Phalcon\Cli\Task;
class SomeTask extends Task
{
public function MainAction()
{
echo "This is the main action\r\n";
}
public function OtherAction()
{
echo "This is other action\r\n";
}
}
要运行主任务,我只需要编写
php cli.php
,但我想要一些严肃的文件名,而不是MainTask
这是我的另一项任务:
<?php
use Phalcon\Cli\Task;
class DeleteExpiredAuthTokens extends Task{
public function mainAction()
{
echo "Hello world war Z";
}
}
目录设置
<?php
use Phalcon\Di\FactoryDefault\Cli as CliDI;
use Phalcon\Cli\Console as ConsoleApp;
use Phalcon\Loader;
// Using the CLI factory default services container
$di = new CliDI();
$arrBasePath = preg_split("/\//", __DIR__);
$limit = sizeof($arrBasePath);
$strBasePath = "";
for ($count = 0; $count < $limit - 1; $count++) {
$strBasePath .= $arrBasePath[$count] . '/';
}
define('BASE_PATH', $strBasePath);
/**
* Register the autoloader and tell it to register the tasks directory
*/
$loader = new Loader();
$loader->registerDirs(
[
__DIR__ . "/tasks",
__DIR__ . "/models",
__DIR__ . "/controllers"
]
);
$loader->register();
// Create a console application
$console = new ConsoleApp();
$console->setDI($di);
//Process the console arguments
$arguments = [];
foreach ($argv as $k => $arg) {
if ($k == 1) {
$arguments["task"] = $arg;
} elseif ($k == 2) {
$arguments["action"] = $arg;
} elseif ($k >= 3) {
$arguments["params"][] = $arg;
}
}
try {
// Handle incoming arguments
$console->handle($arguments);
} catch (\Phalcon\Exception $e) {
echo $e->getMessage();
exit(255);
}
use Phalcon\Cli\Task;
class SomeTask extends Task
{
public function MainAction()
{
echo "This is the main action\r\n";
}
public function OtherAction()
{
echo "This is other action\r\n";
}
}
您需要在app/
中创建一个tasks/
目录,然后您可以创建适当的文件名;比如,
app/
├── tasks/
│ ├── SomeTask.php
│ ├── SomeOtherTask.php
│
├── cli.php
$ php cli.php some
This is the main action
现在,您需要创建一个cli.php
文件,命令行将调用该文件。在这里,您需要查看调用参数并将其传递到$console->handle()
方法中。比如,
app/
├── tasks/
│ ├── SomeTask.php
│ ├── SomeOtherTask.php
│
├── cli.php
$ php cli.php some
This is the main action
cli.php
<?php
use Phalcon\Di\FactoryDefault\Cli as CliDI;
use Phalcon\Cli\Console as ConsoleApp;
use Phalcon\Loader;
// Using the CLI factory default services container
$di = new CliDI();
$arrBasePath = preg_split("/\//", __DIR__);
$limit = sizeof($arrBasePath);
$strBasePath = "";
for ($count = 0; $count < $limit - 1; $count++) {
$strBasePath .= $arrBasePath[$count] . '/';
}
define('BASE_PATH', $strBasePath);
/**
* Register the autoloader and tell it to register the tasks directory
*/
$loader = new Loader();
$loader->registerDirs(
[
__DIR__ . "/tasks",
__DIR__ . "/models",
__DIR__ . "/controllers"
]
);
$loader->register();
// Create a console application
$console = new ConsoleApp();
$console->setDI($di);
//Process the console arguments
$arguments = [];
foreach ($argv as $k => $arg) {
if ($k == 1) {
$arguments["task"] = $arg;
} elseif ($k == 2) {
$arguments["action"] = $arg;
} elseif ($k >= 3) {
$arguments["params"][] = $arg;
}
}
try {
// Handle incoming arguments
$console->handle($arguments);
} catch (\Phalcon\Exception $e) {
echo $e->getMessage();
exit(255);
}
use Phalcon\Cli\Task;
class SomeTask extends Task
{
public function MainAction()
{
echo "This is the main action\r\n";
}
public function OtherAction()
{
echo "This is other action\r\n";
}
}
例如,您可以通过在参数中传递任务来调用任务
app/
├── tasks/
│ ├── SomeTask.php
│ ├── SomeOtherTask.php
│
├── cli.php
$ php cli.php some
This is the main action
或者,通过传递它来调用另一个方法
$ php cli.php some other
This is other action
注释
use Phalcon\Cli\Task;
class SomeTask extends Task
{
public function MainAction()
{
echo "This is the main action\r\n";
}
public function OtherAction()
{
echo "This is other action\r\n";
}
}
- 您的类名需要以
结尾。调用此类时,忽略它。查看我如何调用Task
类someTask
- 在您的示例中,将
重命名为DeleteExpiredAuthTokens
,否则您将收到以下错误消息:DeleteExpiredAuthTokensTask
DeleteExpiredAuthTokensTask处理程序类无法加载