
在本指南中,我们将逐步完成创建一个基本 php 项目的步骤,该项目将 pokémon api 与 flight 框架以及 zebra_curl 和 latte 等附加包结合使用。我们将探索设置项目、添加路线和渲染视图。
tl;dr:在 flight 中制作一个简单的基于 api 的项目并不难。查看本指南中使用的代码。
首先,我们需要设置一个新的项目文件夹。打开终端,导航到所需位置,然后运行以下命令来创建新目录并输入它。
mkdir flight-pokeapi cd flight-pokeapi
在深入研究代码之前,我们需要确保 composer 已安装。 composer 是 php 的依赖管理器,它将帮助我们包含必要的库。
如果您没有安装 composer,您可以使用以下命令安装它:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
现在 composer 已安装在名为 ./composer.phar 的当前文件中,让我们管理我们的依赖项。
立即学习“PHP免费学习笔记(深入)”;
要管理所需的包,我们只需要使用composer添加它们即可。
./composer.phar require flightphp/core stefangabos/zebra_curl latte/latte
这将安装:
接下来,让我们为应用程序创建入口点:public/index.php。该文件将设置我们的应用程序、配置路由并处理视图的渲染。
创建public目录和index.php文件:
mkdir public touch public/index.php
现在将以下内容添加到index.php:
<?php
use flight\net\router;
use latte\engine;
require __dir__ . '/../vendor/autoload.php'; // autoload the installed packages
// setup latte for view rendering
$latte = new engine;
$latte->settempdirectory(__dir__ . '/../temp');
flight::map('render', function(string $template_path, array $data = []) use ($latte) {
$latte->render(__dir__ . '/../views/' . $template_path, $data);
});
// setup zebra_curl for handling http requests
$curl = new zebra_curl();
$curl->cache(__dir__ . '/../temp');
flight::map('curl', function() use ($curl) {
return $curl;
});
// define a simple route
flight::route('/', function() {
echo 'hello world!';
});
flight::start();
在此文件中:
如果您想测试此设置,您可以从公共目录启动 php 服务器:
php -s localhost:8000 -t public/
现在,在浏览器中访问 http://localhost:8000/,您应该会看到“hello world!”。酷吧?
现在我们已经设置了基本路线,让我们添加一个使用 pokémon api 的更复杂的路线。更新 public/index.php 以包含以下代码:
flight::group('/pokemon', function(router $router) {
// route to list all pokémon types
$router->get('/', function() {
$types_response = json_decode(flight::curl()->scrap('https://pokeapi.co/api/v2/type/', true));
$results = [];
while ($types_response->next) {
$results = array_merge($results, $types_response->results);
$types_response = json_decode(flight::curl()->scrap($types_response->next, true));
}
$results = array_merge($results, $types_response->results);
flight::render('home.latte', [ 'types' => $results ]);
});
});
现在我们正在获取数据,让我们设置视图来显示它。创建views目录并添加latte模板文件以显示神奇宝贝类型。
mkdir views touch views/home.latte
将以下代码添加到views/home.latte:
<p>welcome to the pokemon world!</p>
<p>types of pokemon</p>
<ul>
{foreach $types as $type}
<li><a href="/pokemon/type/{$type->name}">{$type->name|firstupper}</a></li>
{/foreach}
</ul>
在此文件中:
现在,访问 /pokemon 将显示所有神奇宝贝类型的列表!
让我们扩展 pokémon 路线,以获取特定类型和单个 pokémon 的更多详细信息。将以下路线添加到您的 /pokemon 组:
// route to fetch a specific pokémon type and list all associated pokémon
$router->get('/type/@type', function(string $type) {
$curl = flight::curl();
$type_response = json_decode($curl->scrap('https://pokeapi.co/api/v2/type/' . $type, true));
$pokemon_urls = [];
foreach($type_response->pokemon as $pokemon_data) {
$pokemon_urls[] = $pokemon_data->pokemon->url;
}
$pokemon_data = [];
// the little & here is important to pass the variable by reference.
// in other words it allows us to modify the variable inside the closure.
$curl->get($pokemon_urls, function(stdclass $result) use (&$pokemon_data) {
$pokemon_data[] = json_decode($result->body);
});
flight::render('type.latte', [
'type' => $type_response->name,
'pokemons' => $pokemon_data
]);
});
在这条路线中,我们:
接下来,创建 type.latte 视图:
touch views/type.latte
将以下内容添加到type.latte:
<h1>{$type|firstupper}</h1>
<ul>
{foreach $pokemons as $pokemon}
<li><a href="/pokemon/{$pokemon->id}">{$pokemon->name|firstupper}</a></li>
{/foreach}
</ul>
此模板显示与特定类型关联的每个神奇宝贝的名称。
此时,您已经使用 flight php、用于 api 请求的 zebra_curl 和用于视图渲染的 latte 设置了基本的 pokémon api 使用者。您可以通过添加更多路线和完善模板来进一步扩展此项目。
要查看您的项目,请从公共目录启动 php 服务器:
php -s localhost:8000 -t public/
现在,在浏览器中访问 http://localhost:8000/pokemon,您应该会看到 pokémon 类型的列表。
如果您需要帮助或遇到问题,您可以在 github 中查看完整代码,看看您可能在哪里犯了错误。
希望您喜欢这个小教程。如果您有任何疑问或需要帮助,请随时在下面的评论中提问。快乐编码!
以上就是用 PHP 构建 Pokémon API:初学者指南的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号