如何在 Slim 中使用 MVC 'View'?
How to use a MVC 'View' in Slim?
$app->get('/', function () {
// Initial page load.
include 'body-index.php';
return $response;
});
我的 /index.php 上有上面的代码。我将如何调用和修改 body-index.php 中的函数?由于我现在正在自己学习 MVC 和框架,所以我宁愿这样做,也不愿脱离 Slim 并使用页面代码执行 get('/body-index.php',
。这可能吗?
谢谢。
来自 Slim Framework 文档:
Most often, you’ll need to write to the PSR 7 Response object. You can write content to the StreamInterface instance with its write() method like this:
$body = $response->getBody();
$body->write('Hello');
You can also replace the PSR 7 Response object’s body with an entirely new StreamInterface instance. This is particularly useful when you want to pipe content from a remote destination (e.g. the filesystem or a remote API) into the HTTP response. You can replace the PSR 7 Response object’s body with its withBody(StreamInterface $body) method. Its argument MUST be an instance of \Psr\Http\Message\StreamInterface.
$newStream = new \GuzzleHttp\Psr7\LazyOpenStream('/path/to/file', 'r');
$newResponse = $oldResponse->withBody($newStream);
$app->get('/', function () {
// Initial page load.
include 'body-index.php';
return $response;
});
我的 /index.php 上有上面的代码。我将如何调用和修改 body-index.php 中的函数?由于我现在正在自己学习 MVC 和框架,所以我宁愿这样做,也不愿脱离 Slim 并使用页面代码执行 get('/body-index.php',
。这可能吗?
谢谢。
来自 Slim Framework 文档:
Most often, you’ll need to write to the PSR 7 Response object. You can write content to the StreamInterface instance with its write() method like this:
$body = $response->getBody();
$body->write('Hello');
You can also replace the PSR 7 Response object’s body with an entirely new StreamInterface instance. This is particularly useful when you want to pipe content from a remote destination (e.g. the filesystem or a remote API) into the HTTP response. You can replace the PSR 7 Response object’s body with its withBody(StreamInterface $body) method. Its argument MUST be an instance of \Psr\Http\Message\StreamInterface.
$newStream = new \GuzzleHttp\Psr7\LazyOpenStream('/path/to/file', 'r');
$newResponse = $oldResponse->withBody($newStream);