Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Artisan command instead of "php artisan serve"

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.

like image 443
GameO7er Avatar asked Aug 21 '19 08:08

GameO7er


People also ask

Which artisan command makes another artisan command?

To create a new artisan command, we can use the make:command artisan command. This command will make a new command class within the app/Console/Commands catalog. In case the directory does not exist in our laravel project, it'll be automatically made the primary time we run the artisan make:command command.

How do I get out of artisan serve in php?

Press Ctrl + Shift + ESC. Locate the php process running artisan and kill it with right click -> kill process. Reopen the command-line and start back the server. Note that you should be able to kill the process just by sending it a kill signal with Ctrl + C.

Why php artisan serve not working?

Reasons why php artisan serve not working You are running php artisan serve command in wrong directory. php artisan serve is laravel command only work with laravel project. Check project directory and run in root directory of your project.


1 Answers

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.

like image 180
nakov Avatar answered Oct 09 '22 03:10

nakov