通过Symfony / Console创建不同类型的类

时间:2019-05-20 19:29:07

标签: php symfony console artisan

我已在自定义php应用程序(不是Symfony应用程序,仅使用Symfony外部的组件)上设置了Symfony控制台,并在根目录下创建了app文件

#!/usr/bin/env php
<?php
// Include framework's dependencies through composer's autoload feature
require __DIR__ . '/vendor/autoload.php';

$application = new Symfony\Component\Console\Application();

// ... register commands
$application->add(new TestCommand());

$application->run();

这是我设置的测试课程

<?php

namespace Mvc\Commands;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class TestCommand extends Command
{
    /**
     * The name of the command (the part after "bin/console")
     * 
     * @var string
     */
    protected $command_name = 'app:test';

    /**
     * The description of the command (the part after "bin/console")
     * 
     * @var string
     */
    protected $command_description = "Try to echo Hello World for testing if it works.";

    protected function configure()
    {
        $this->setName($this->command_name)
             ->setDescription($this->command_description);
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $name = $input->getArgument($this->command_argument_name);
    }
}

如何创建命令来生成不同类型的类,例如Laravel如何生成控制器,模型等(php artisan make:controller TestController等)?

1 个答案:

答案 0 :(得分:0)

您必须使用composer require symfony/maker-bundle --dev安装maker-bundle

此后,输入php bin/console make,然后按Enter键以显示所有可用命令。