如何使用 PHP slim 在 twig 中呈现数据库中的数据
How to render data from database in twig using PHP slim
我正在尝试将数据从我的数据库渲染到控制器中的树枝中。有点像 mvc 结构,但没有模型。
现在它只是呈现
我的待办事项
我很惊讶 slim documentation 不包含此类说明
任何建议,在此先感谢。
TodosController.php
<?php
namespace App\Controllers;
use Slim\Http\Request;
use Slim\Http\Response;
class TodosController extends BaseController
{
public function index($request, $response)
{
}
public function getTodos($request, $response, $args)
{
$sth = $this->db->prepare("SELECT * FROM tasks ORDER BY task");
$sth->execute();
$todos = $sth->fetchAll();
return $this->c->view->render($response, 'todos.twig', $todos);
}
}
todos.twig
{% extends "templates/layout.html" %}
{% block content %}
<h1>My Todos</h1>
<ul>
{% for task in todos %}
<li><span>{{ task.id}}</span> {{ task.task}}</li>
{% endfor %}
</ul>
{% endblock %}
您需要将您的 todos
放入数组
$this->c->view->render($response, 'todos.twig', ['todos' => $todos]);
我正在尝试将数据从我的数据库渲染到控制器中的树枝中。有点像 mvc 结构,但没有模型。
现在它只是呈现
我的待办事项
我很惊讶 slim documentation 不包含此类说明
任何建议,在此先感谢。
TodosController.php
<?php
namespace App\Controllers;
use Slim\Http\Request;
use Slim\Http\Response;
class TodosController extends BaseController
{
public function index($request, $response)
{
}
public function getTodos($request, $response, $args)
{
$sth = $this->db->prepare("SELECT * FROM tasks ORDER BY task");
$sth->execute();
$todos = $sth->fetchAll();
return $this->c->view->render($response, 'todos.twig', $todos);
}
}
todos.twig
{% extends "templates/layout.html" %}
{% block content %}
<h1>My Todos</h1>
<ul>
{% for task in todos %}
<li><span>{{ task.id}}</span> {{ task.task}}</li>
{% endfor %}
</ul>
{% endblock %}
您需要将您的 todos
放入数组
$this->c->view->render($response, 'todos.twig', ['todos' => $todos]);