Search code examples
phplaravellaravel-artisan

Set default 'host' value for `php artisan serve`


I am building a Laravel site, and want to test it on other devices as I build it (phone, ipad etc).

As I understand it, the way to do this is to run php artisan serve --host=0.0.0.0.

My question is... is there a way to define a default value for the host, and set it to 0.0.0.0, so that I can simply run php artisan serve and it will automatically run on 0.0.0.0?


Solution

  • You can do next thing:

    1. php artisan make:command CustomServeCommand
    2. Then delete all from file and use this code:

      <?php
      
      namespace App\Console\Commands;
      
      use Illuminate\Foundation\Console\ServeCommand;
      use Symfony\Component\Console\Input\InputOption;
      
      class CustomServeCommand extends ServeCommand
      {
          /**
           * Get the console command options.
           *
           * @return array
           */
          protected function getOptions()
          {
              return [
                  ['host', null, InputOption::VALUE_OPTIONAL, 'The host address to serve the application on.', '0.0.0.0'],//default 127.0.0.1
                  ['port', null, InputOption::VALUE_OPTIONAL, 'The port to serve the application on.', 8000],
              ];
          }
      }
      
    3. php artisan serve

    Link to the core file.

    Basically, you will extend default class and adapt method to fit your own needs. This way you can set host and port per requirements.