- Home
- Articles
- PHP
- How to change the host and port on the Laravel Artisan Serve command
Create an artisan command to help serve your local environment from a custom port
Create a file within your App\Console\Commands directory. I called the file within my system 'ServeMe.php'.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\ProcessUtils;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\PhpExecutableFinder;
class ServeMe extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:serve {--host=127.0.0.1} {--port=8000}';
/**
* 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.
*
*/
public function handle()
{
chdir($this->laravel->publicPath());
$this->line("<info>Laravel development server started:</info> <http://{$this->host()}:{$this->port()}>");
passthru($this->serverCommand());
}
protected function serverCommand()
{
return sprintf('%s -S %s:%s %s/server.php',
ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false)),
$this->host(),
$this->port(),
ProcessUtils::escapeArgument($this->laravel->basePath())
);
}
/**
* Get the host for the command.
*
* @return string
*/
protected function host()
{
return env( 'APP_HTTP_HOST' ) != "" ? env( 'APP_HTTP_HOST' ) : $this->input->getOption('host');
}
/**
* Get the port for the command.
*
* @return string
*/
protected function port()
{
return env( 'APP_HTTP_PORT' ) != "" ? env( 'APP_HTTP_PORT' ) : $this->input->getOption('port');
}
}
Update your .env file to have the following two lines. This is where you can specify any host and port.
APP_HTTP_HOST=localhost
APP_HTTP_PORT=8080
Run with the following artisan command.
php artisan command:serve
If you want to have it called by something else, just change the signature line.
comments powered by Disqus
PHP
artisan
laravel
host
port
Views: 10365