生成Artisan命令代替"php artisan serve"

5
我想创建一个类似于 php artisan serve 的别名 php artisan go。如果有其他建议也欢迎提供 :-) 。我阅读了这个链接,并进行了大量搜索,但并不是很清楚,其他问题都涉及到创建 class.env 文件等。谢谢。 更新 这个问题不是重复的这个,因为它不包含调用php artisan本身。

可能是PHP Artisan Custom Command的重复问题。 - Nico Haase
1
这是不重复的问题,因为该问题并未包含调用 artisan 本身。 - GameO7er
1个回答

7
使用以下方式创建命令:
php artisan make:command GoCommand

将以下内容添加到类中:
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Symfony\Component\Console\Output\ConsoleOutput;

class GoCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'go';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $output = new ConsoleOutput;
        $output->writeln("Laravel development server started: <http://127.0.0.1:8000>");
        Artisan::call('serve');
        Artisan::output();
    }
}

使用以下命令:

php artisan go

访问:http://127.0.0.1:8000/

并在您的控制台中查看输出。


1
抱歉,由于我的每日投票限制,我无法为您的答案点赞。我明天会点赞。它可以工作,但是如何打印输出像这样 Laravel development server started: <http://127.0.0.1:8000> - GameO7er
1
@GameO7er,请看一下我编辑过的类。确保你标记答案,不知道这部分是否也受到限制 :) 祝编码愉快! - nakov
我打了个勾 :-)。它像魔法一样运行 :-) 感谢 @nakov 亿万次。 - GameO7er

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接