【问题标题】:Add rest api with website添加带有网站的rest api
【发布时间】:2023-03-29 08:40:01
【问题描述】:

我刚刚为我的网站创建了一个精简的 rest api,我想围绕同一个域(服务器)创建我的网站。

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';
//require '../src/config/db.php';

$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response) {
    $name = $request->getAttribute('name');
    $response->getBody()->write("Hello, $name");

    return $response;
});

#Customer Routes
//require '../src/routes/customer.php';
$app->run();
?>

<!DOCTYPE html>
<html>
    <head></head>
    <body></body>
</html>

现在我的网页没有显示,但是当我删除 html 文本时它可以正常工作。

我只希望我的网络服务(使用 slim api)和我的网站在同一台服务器上。请帮忙。

【问题讨论】:

  • 为此添加路线。

标签: php apache web-services web slim


【解决方案1】:

要为您的 API 制作视图,您可以使用 Twig

使用它您将使用此模板引擎和 Slim API 路由构建您的 html 模板,并以简单的方式返回视图响应遵循此官方 Slim docs

【讨论】:

    猜你喜欢
    • 2014-04-21
    • 2013-07-27
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    相关资源
    最近更新 更多