Skip to content

Instantly share code, notes, and snippets.

@ejlocop
Created January 31, 2018 07:32
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ejlocop/2e81cd3134fbc6b7858f28eb6fd4a60f to your computer and use it in GitHub Desktop.
Save ejlocop/2e81cd3134fbc6b7858f28eb6fd4a60f to your computer and use it in GitHub Desktop.
set a default laravel php artisan serve host and port.
<?php
namespace Illuminate\Foundation\Console;
use Illuminate\Console\Command;
use Symfony\Component\Process\ProcessUtils;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\PhpExecutableFinder;
class ServeCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'serve';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Serve the application on the PHP development server';
/**
* Execute the console command.
*
* @return void
*
* @throws \Exception
*/
public function fire()
{
chdir($this->laravel->publicPath());
$host = $this->input->getOption('host');
$port = $this->input->getOption('port');
$base = ProcessUtils::escapeArgument($this->laravel->basePath());
$binary = ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false));
$this->info("Laravel development server started on http://{$host}:{$port}/");
passthru("{$binary} -S {$host}:{$port} {$base}/server.php");
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
$host = env('SERVE_HOST', '127.0.0.1');
$port = env('SERVE_PORT', 8080);
return [
['host', null, InputOption::VALUE_OPTIONAL, 'The host address to serve the application on.', $host],
['port', null, InputOption::VALUE_OPTIONAL, 'The port to serve the application on.', $port],
];
}
}
@ejlocop
Copy link
Author

ejlocop commented Jan 31, 2018

add these two line to ServeCommand.php file
then create a SERVE_HOST and SERVE_PORT in your .env file

@atulmahankal
Copy link

atulmahankal commented Jul 13, 2021

Just Replace the getOptions function in ServeCommand.php with the bellow line

        ['host', null, InputOption::VALUE_OPTIONAL, 'The host address to serve the application on', Env::get('SERVER_HOST')],

and create SERVER_HOST & SERVER_PORT variable into .env fle


       SERVER_HOST=myproject
       SERVER_PORT=80

****** defined SERVER_PORT in .env file not set different port if it already used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment