Artisan Serve no Lumen

王林
Release: 2024-07-16 21:21:11
Original
906 people have browsed it

Artisan Serve no Lumen

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:

  • Template engine
  • ORM (Eloquent is disabled by default)
  • Facades (Disabled by default)
  • Session management mechanism
  • Artisan Features

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
Copy after login

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/
Copy after login

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.

  1. Create the ServeCommand.php file
<?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],
        ];
    }

}

Copy after login
  1. Include the call within Kernel.php
<?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,
    ];
}

Copy after login

Ready!! Now just use it.

$ php artisan serve 
Copy after login
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
Copy after login

The above is the detailed content of Artisan Serve no Lumen. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!