下面由laravel教程栏目给大家介绍实现 laravel 的artisan 的方法,希望对需要的朋友有所帮助!
laravel 的 artisan 命令行太好用了,换个框架没有这个功能,于是自己学习实现一些,直接上代码
新建目录
-artisan
--bin
--src
进入artisan composer init
composer require symfony/console
#!/usr/bin/env php<?phpuse symfony\component\console\application;use symfony\component\console\input\inputinterface;use symfony\component\console\output\outputinterface;require_once __dir__.'/../vendor/autoload.php';$app = new application('artisan','1.1.1');$app->register('artisan')->setcode(function(inputinterface $input, outputinterface $output){ $output->writeln('artisan start');});$app->run();exit();以上是简单的实现
#!/usr/bin/env php<?phpuse symfony\component\console\application;use symfony\component\console\input\inputinterface;use symfony\component\console\output\outputinterface;use symfony\component\console\input\inputargument;require_once __dir__ . '/../vendor/autoload.php';$app = new application('artisan', '1.1.1');$app->register('artisan') ->setdescription('myself artisan description') ->setcode( function (inputinterface $input, outputinterface $output) { $name = $input->getargument('name'); $output->writeln("hello {$name}"); } )->addargument('name', inputargument::required, 'please input your name');$app->run();exit();这里演示了如何接收参数
#!/usr/bin/env php<?phpuse symfony\component\console\application;use symfony\component\console\input\inputinterface;use symfony\component\console\output\outputinterface;use symfony\component\console\input\inputargument;use symfony\component\console\input\inputoption;require_once __dir__ . '/../vendor/autoload.php';$app = new application('artisan', '1.1.1');$app->register('artisan') ->setdescription('myself artisan description') ->setcode( function (inputinterface $input, outputinterface $output) { $string = $input->getoption('string'); $name = $input->getargument('name'); if($string == 'lower'){ $name = strtolower($name); } if($string == 'upper'){ $name = strtoupper($name); } $output->writeln("hello {$name}"); } )->addargument('name', inputargument::required, 'please input your name') ->addoption('string',null,inputoption::value_optional,'转换字符串大小','lower');$app->run();exit();这里演示了如何给命令行添加选项 ./bin/artisan.php artisan ffff --string='upper' echo ffff
$output->writeln("<info>hello {$name}</info>");$output->writeln("<error>hello {$name}</error>");$output->writeln("<comment>hello {$name}</comment>");$output->writeln("hello {$name}");可以给它们加上颜色
接下来将命令行拆分为文件
bin/artisan.php
artisancommand.php
#!/usr/bin/env php<?phpuse symfony\component\console\application;use artisan\artisancommand;require_once __dir__ . '/../vendor/autoload.php';$app = new application('artisan', '1.1.1');$app->add(new artisancommand());$app->run();exit();artisancommand.php
<?phpnamespace artisan;use symfony\component\console\application;use symfony\component\console\input\inputinterface;use symfony\component\console\output\outputinterface;use symfony\component\console\input\inputargument;use symfony\component\console\input\inputoption;use symfony\component\console\command\command;class artisancommand extends command{ public function configure() { $this->setname('artisan'); $this->setdescription('myself artisan description') ->addargument('name', inputargument::required, 'please input your name') ->addoption('string',null,inputoption::value_optional,'转换字符串大小','lower'); } public function execute(inputinterface $input, outputinterface $output) { $string = $input->getoption('string'); $name = $input->getargument('name'); if($string == 'lower'){ $name = strtolower($name); } if($string == 'upper'){ $name = strtoupper($name); } $output->writeln("<info>hello {$name}</info>"); $output->writeln("<error>hello {$name}</error>"); $output->writeln("<comment>hello {$name}</comment>"); $output->writeln("hello {$name}"); }}
composer.json
{ "name": "baidu/artisan", "authors": [ { "name": "gaobingbing", "email": "v_gaobingbing01@baidu.com" } ], "require": { "symfony/console": "^4.3" }, "autoload": { "psr-4": { "artisan\\": "src" } }}至此大功告成,还有其他功能可以去看symfony文档
以上就是如何实现 laravel 的artisan的详细内容。