5

I want to make an alias like

php artisan go

instead of

php artisan serve

I will appreciate any other idea :-) .I also read this link and search a lot but it wasn't so clear and other questions were about making class or .env files and etc.

Thanks in advance

Update This question is not duplicate of this because it's not contain calling php artisan itself.

2
  • Possible duplicate of PHP Artisan Custom Command Commented Aug 21, 2019 at 8:05
  • 1
    this is not duplicate because that question doesn't contains calling artisan itself. Commented Aug 21, 2019 at 8:16

1 Answer 1

7

Create the command using:

php artisan make:command GoCommand

Add this in the class:

<?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();
    }
}

Use the command:

php artisan go

Visit: http://127.0.0.1:8000/

and see the output in your console.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry I can't upvote your answer because of my daily vote limit. I will do it tomorrow. it works but how can I print output like this Laravel development server started: <http://127.0.0.1:8000>
@GameO7er take a look at my edited class. Make sure you mark the answer, don't know if that part of the limit as well :) happy coding!
I did mark :-) . It works like a charm :-) thanks a billion @nakov

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.