Skip to content

Instantly share code, notes, and snippets.

@rafael-neri
Last active September 27, 2021 22:45
Show Gist options
  • Save rafael-neri/4134736bd6e721ac59983f34649990e9 to your computer and use it in GitHub Desktop.
Save rafael-neri/4134736bd6e721ac59983f34649990e9 to your computer and use it in GitHub Desktop.
Add Support to "php artisan serve" in Lumen
<?php
// File: app/Console/Commands/ServeCommand.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
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
*/
public function handle(): void
{
$base = $this->laravel->basePath();
$host = $this->input->getOption('host');
$port = $this->input->getOption('port');
$this->info("Lumen development server started on http://{$host}:{$port}/");
passthru('"' . PHP_BINARY . '"' . " -S {$host}:{$port} -t \"{$base}/public\"");
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions(): array
{
$url = env('APP_URL', '');
$host = parse_url($url, PHP_URL_HOST);
$port = parse_url($url, PHP_URL_PORT);
// Defaults
$host = $host ?? 'localhost';
$port = $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],
];
}
}
<?php
// File: app/Console/Kernel.php
namespace App\Console;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
// Add Support to Artisan Serve
Commands\ServeCommand::class,
];
}
$ php artisan serve
---------------------------
Lumen development server started on http://localhost:8080/
[Mon Sep 27 19:38:07 2021] PHP 8.1.0RC2 Development Server (http://localhost:8080) started
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment