Skip to content

Instantly share code, notes, and snippets.

@brcontainer
Last active Feb 22, 2021
Embed
What would you like to do?
Lumen php artisan serve improved (with support for .env - APP_HOST)

Copy ServeCommand.php to app/Console/Commands/ folder and edit app/Console/Kernel.php and put ServerCommand, like this:

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    protected $commands = [
        Commands\ServeCommand::class,
    ];

If you run the command:

php artisan serve

will start the local server based on APP_HOST (from your .env)

You can change the host or port:

php artisan serve --host 0.0.0.0 --port 8000
<?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()
{
$host = $this->input->getOption('host');
$port = $this->input->getOption('port');
$base = $this->laravel->basePath();
$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()
{
$url = env('APP_URL');
return [
[
'host', null, InputOption::VALUE_OPTIONAL, 'The host address to serve the application on.', parse_url($url, PHP_URL_HOST)
], [
'port', null, InputOption::VALUE_OPTIONAL, 'The port to serve the application on.', parse_url($url, PHP_URL_PORT)
],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment