장인 서브 노 루멘

王林
풀어 주다: 2024-07-16 21:21:11
원래의
912명이 탐색했습니다.

Artisan Serve no Lumen

Laravel은 현재 PHP 생태계에서 가장 많이 사용되는 프레임워크입니다. 하지만 그를 모르는 사람들은 그에게 루멘이라는 더 젊지만 그다지 흥미롭지 않은 형제가 있다는 사실을 거의 알지 못할 것입니다.

Lumen은 실제로 API 생성을 목표로 하며 코드베이스가 형과 매우 비슷하지만 중요한 차이점이 있습니다. Lumen은 더 나은 성능을 위해 일부 기능을 희생합니다. . Lumen을 사용할 때 놓칠 수 있는 기능은 다음과 같습니다.

템플릿 엔진
  • ORM(Eloquent는 기본적으로 비활성화되어 있습니다)
  • 외관(기본적으로 비활성화되어 있음)
  • 세션 관리 메커니즘
  • 장인의 특징
  • 마지막 요점은 Artisan의 일부 기능 부족이 애플리케이션 성능에 직접적인 영향을 미치지 않기 때문에 제가 주목했던 점이었습니다.

Artisan에 대해 들어본 적이 없다면 Laravel 또는 Lumen과 상호 작용하여 애플리케이션 개발을 돕는 강력한 명령줄 유틸리티라는 점에 주목할 필요가 있습니다.

이러한 리소스가 없으면 개발자의 생산성에 직접적인 영향을 미칩니다.

루멘과의 첫 접촉에서 다음 명령을 놓쳤습니다:


"serve" 명령이 없는 경우 대안은 다음 명령을 사용하여 PHP 자체 내장 서버를 사용하는 것입니다.
$ php artisan serve
로그인 후 복사


간단하지만 실용적이지 않습니다.
$ php -S localhost:8000 -t public/
로그인 후 복사

그리고 이를 염두에 두고 서버를 업로드할 때마다 이 명령을 입력하지 않도록 "serve" 명령을 Lumen으로 다시 가져오는 데 필요한 조정을 만들었습니다.

한걸음씩 나아가자.

ServeCommand.php 파일 생성
<?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],
        ];
    }

}

로그인 후 복사
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,
    ];
}

로그인 후 복사


$ 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
로그인 후 복사

위 내용은 장인 서브 노 루멘의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
저자별 최신 기사
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!