Last active
September 27, 2021 22:45
-
-
Save rafael-neri/4134736bd6e721ac59983f34649990e9 to your computer and use it in GitHub Desktop.
Add Support to "php artisan serve" in Lumen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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], | |
]; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, | |
]; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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