我正在尝试从外壳运行任何控制器操作。
下面的代码有效
$object = new TestController();
$object->index();
但是下面的那个不起作用
// planed to change $controller dynamically from command line arguments
$controller = "TestController";
$object = new $controller();
$object->index();
我收到错误消息:找不到类'TestController'。
下面是我完整的命令文件代码
<?php
namespace App\Command;
use Cake\Console\Arguments;
use Cake\Console\Command;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use App\Controller\TestController;
class ControllerCommand extends Command
{
protected function buildOptionParser(ConsoleOptionParser $parser)
{
$parser
->addArgument('controllername', [
'help' => 'controllername',
'required' => true
])
->addArgument('actionname', [
'help' => 'actionname',
'required' => true
]);
return $parser;
}
public function execute(Arguments $args, ConsoleIo $io)
{
$controller=$args->getArgument('controllername');
$action=$args->getArgument('actionname');
//$object = new TestController();
$controller = "TestController";
$object = new $controller();
$object->index();
}
}