lumen开发api

tech2022-09-01  97

lumen开发api

If you’ve been using Laravel for a while, you know that it sometimes feels a little heavy for a small application or service. For that same purpose, Taylor, the creator of Laravel built Lumen. A small micro-framework built on top of Laravel Illuminate components, it doesn’t load all the components by default like Eloquent, Blade, Middleware, etc, remaining light as it boots up. We will explore all of that this short tutorial.

如果您已经使用Laravel一段时间了,您就会知道对于小型应用程序或服务而言,有时会感到有些沉重。 出于同样的目的,Laravel的创造者泰勒(Taylor)建造了流明(Lumen) 。 基于Laravel Illuminate组件的小型微型框架 ,默认情况下它不会加载所有组件,例如Eloquent,Blade,Middleware等,并且在启动时会保留剩余的光线。 我们将在本简短教程中探讨所有这些内容。

为什么选择流明? (Why Lumen?)

If you’re already a Laravel user, you may be in love with Laravel’s syntax and shortcuts. But, when working on just a small application, you don’t want to have a full stack framework with all the packages wired up. Lumen provides a good starting point for your small application, stripping out all the parts that you probably won’t need.

如果您已经是Laravel用户,那么您可能会喜欢Laravel的语法和快捷方式。 但是,当只在一个小型应用程序上工作时,您并不想拥有一个完整的堆栈框架,其中所有的程序包都连接在一起。 流明为您的小型应用程序提供了一个很好的起点,去除了您可能不需要的所有部分。

我们正在建设 (What We’re Building)

To illustrate a practical use case for the micro framework, we will be creating a Markdown parser API application where the user can submit a Markdown text and get back the parsed content as JSON. I will be using the league/commonmark package from the PHP League. You can check the final result on Github.

为了说明微框架的实际用例,我们将创建一个Markdown解析器API应用程序,用户可以在其中提交Markdown文本并以JSON格式返回解析后的内容。 我将使用PHP League中的League / Commonmark软件包。 您可以在Github上查看最终结果。

安装 (Installation)

If you’re creating new applications often, you can use the global installer (composer global require "laravel/lumen-installer=~1.0") and take advantage of short syntax like lumen new my-project, or you can use composer to create a new project with composer create-project laravel/lumen my-project --prefer-dist. You can read more about the installation process in the documentation.

如果您经常创建新的应用程序,则可以使用全局安装程序( composer global require "laravel/lumen-installer=~1.0" )并利用lumen new my-project类的composer global require "laravel/lumen-installer=~1.0" ,也可以使用composer创建一个带有composer create-project laravel/lumen my-project --prefer-dist的新项目composer create-project laravel/lumen my-project --prefer-dist 。 您可以在文档中阅读有关安装过程的更多信息。

After Lumen is installed, you can go ahead and require the commonmark package using composer require league/commonmark. Visit the documentation for more info about it.

安装Lumen后,您可以继续使用composer require league/commonmark要求commonmark软件包。 请访问文档以获取有关此信息的更多信息。

资料夹结构 (Folder Structure)

The first thing that you’ll notice after installing Lumen is the folder structure. A lot of things are stripped out, like the config, database and resources folders, which can be brought back when necessary using some scaffolding commands, or manually.

安装Lumen后您会注意到的第一件事是文件夹结构。 剥夺了很多东西,例如config , database和resources文件夹,可以在必要时使用一些脚手架命令或手动将其恢复。

Lumen provides you with the php artisan make command to help you scaffold the missing folders. If you want to work with view templates, you can run php artisan make views to create the resources/views folder for your templates. The available commands can be found in the Laravel\Lumen\Console\Commands\MakeCommand class.

Lumen为您提供了php artisan make命令,以帮助您搭建丢失的文件夹。 如果要使用视图模板,可以运行php artisan make views来为模板创建resources/views文件夹。 可用的命令可以在Laravel\Lumen\Console\Commands\MakeCommand类中找到。

php artisan make foundation: scaffold the resources and database folders.

php artisan make foundation :搭建resources和database文件夹。

php artisan make resources: scaffold the resources folder.

php artisan make resources :搭建resources文件夹。

php artisan make database: scaffold the database folder.

php artisan make database :搭建database文件夹。

php artisan make lang: scaffold the resources/lang folder.

php artisan make lang :脚手架resources/lang文件夹。

php artisan make views: scaffold the resources/views folder.

php artisan make views :脚手架resources/views文件夹。

解析降价 (Parsing Markdown)

// app/Http/routes.php $app->get('/parse', 'App\Http\Controllers\MarkdownController@parse'); // app/Http/Controllers/MarkdownController.php public function parse(Request $request, CommonMarkConverter $parser) { $text = $request->get('text'); $result = $parser->convertToHtml($text); return ['html' => $result]; }

After the user submits the Markdown text to our /parse endpoint, we will retrieve the content from the request object and parse it. You noticed that we took advantage of the service container type hinting to resolve the markdown and request objects. If you decide to use Facades (Request::get('text')), just make sure to tell your application to load them (see next section).

在用户将Markdown文本提交到我们的/parse端点之后,我们将从请求对象中检索内容并进行解析。 您注意到,我们利用了服务容器类型提示来解决markdown和request对象。 如果您决定使用Facades( Request::get('text') ),只需确保告诉您的应用程序加载它们(请参阅下一节)。

外墙,口才和中间件 (Facades, Eloquent and Middleware)

Because Facades, Eloquent ORM and Middleware are commonly used in Laravel applications, we have them added by default inside our bootstrap/app.php file and we only need to uncomment them before starting to use them.

由于Facades,Eloquent ORM和Middleware经常在Laravel应用程序中使用,因此默认情况下,我们在bootstrap/app.php文件中添加了它们,我们只需要在开始使用它们之前取消注释即可。

// bootstrap/app.php $app->withFacades(); $app->withEloquent(); $app->middleware([ //... ]);

If your application needs to include other services, you can use the $app->register('AppServiceProvider'); syntax to register the list of providers.

如果您的应用程序需要包含其他服务,则可以使用$app->register('AppServiceProvider'); 语法以注册提供程序列表。

Generally speaking, most of the Lumen parts work the same as in Laravel, they are either not loaded by default or need to be installed and wired into the application. For example, you can use the Laravel FileSystem package as usual (Storage::disk('local')->put('file.txt', 'Content here');) and it will work as expected. However, if you want to work with Rackspace, you need to pull the league/flysystem-rackspace package using Composer and specify your credentials inside the .env file.

一般来说,大多数Lumen部件的工作方式与Laravel中的相同,默认情况下它们要么未加载,要么需要安装并连接到应用程序中。 例如,您可以照常使用Laravel FileSystem软件包( Storage::disk('local')->put('file.txt', 'Content here'); ),它将按预期工作。 但是,如果要使用Rackspace,则需要使用Composer league/flysystem-rackspace软件包,并在.env文件中指定您的凭据。

使用API (Using the API)

After our service is up and running, we can consume our API using Guzzle or any other package or method you prefer.

服务启动并运行后,我们可以使用Guzzle或您喜欢的任何其他包或方法使用我们的API。

$client = new GuzzleHttp\Client(); $response = $client->get('http://vaprobash.dev/parse', [ 'query' => ['text' => file_get_contents('myFile.md')] ])->json(); $html = $response->html;

If a part of your service is getting a lot of traffic, it makes sense to have light micro-framework to handle this part of the application without having to load all the domain login and packages to complete a simple task.

如果您的服务的一部分流量很大,那么使用轻巧的微框架来处理应用程序的这一部分就很有意义,而不必加载所有域登录名和程序包即可完成一个简单的任务。

结论 (Conclusion)

Lumen will bring back some Laravel fans who don’t like to use the full framework for small tasks. You can check the documentation for more information. If you’ve already tried Lumen, I would love to know your first impressions of it, and about the advantages you think it has over the other micro frameworks?

Lumen将带回一些不喜欢将完整框架用于小任务的Laravel粉丝。 您可以查看文档以获取更多信息。 如果您已经尝试过Lumen,那么我想知道您对Lumen的第一印象,以及您认为它相对于其他微框架的优势?

翻译自: https://www.sitepoint.com/building-micro-markdown-api-app-lumen/

lumen开发api

相关资源:jdk-8u281-windows-x64.exe
最新回复(0)