Skip to content

Instantly share code, notes, and snippets.

@palaueb
Created November 24, 2025 13:09
Show Gist options
  • Select an option

  • Save palaueb/3941d72fc317f99f1184503c792764a9 to your computer and use it in GitHub Desktop.

Select an option

Save palaueb/3941d72fc317f99f1184503c792764a9 to your computer and use it in GitHub Desktop.
automatic port patch for php artisan serv
// It reads the local.env file and uses the SERVER_PORT to handle the requests
/**
* Get the port for the command.
*
* @return string
*/
protected function port()
{
$port = $this->input->getOption('port');
if (is_null($port)) {
[, $port] = $this->getHostAndPort();
}
if (is_null($port)) {
$basePath = base_path(); // ruta arrel del projecte
if (file_exists($basePath . '/.env')) {
$dotenv = Dotenv::createImmutable($basePath);
$envContent = $dotenv->parse(file_get_contents($basePath . '/.env'));
$port = $envContent['SERVER_PORT'] ?? null;
}
}
$port = $port ?: 8000;
return $port + $this->portOffset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment