Laravel is the currently most used framework within the PHP ecosystem. But for those who don't know him, they will hardly know that he has a younger, but no less interesting, brother called Lumen.
Lumen is aimed at creating APIs. In fact, it is a micro-framework with a codebase very close to its older brother, but with an important difference: Lumen sacrifices some features in favor of better performance.
Among the features you will miss when using Lumen are:
The last point was what really caught my attention because the lack of some features in Artisan do not directly impact the application's performance.
If you've never heard of Artisan, it's worth noting that it's a powerful command-line utility that interacts with Laravel or Lumen, helping you develop your applications.
The absence of these resources directly impacts developers' productivity.
In my first contact with Lumen I missed the command:
$ php artisan serve
In the absence of the "serve" command, the alternative is to use PHP's own built-in server, using the command:
$ php -S localhost:8000 -t public/
Apparently simple but impractical.
And it was with this in mind, about avoiding typing this command every time you upload the server, that I created the necessary adjustment to bring the "serve" command back to Lumen.
Let's go step by step.
<?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 { protected $name = 'serve'; protected $description = "Serve the application on the PHP development server"; 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\""); } 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 ? $host : 'localhost'; $port = $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 { protected $commands = [ // Add Support to Artisan Serve Commands\ServeCommand::class, ]; }
Ready!! Now just use it.
$ 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
The above is the detailed content of Artisan Serve no Lumen. For more information, please follow other related articles on the PHP Chinese website!