Php 函数App\Models\Task::_construct()的参数太少,第26行传递了0,正好传递了一个
我试图使用php slim在控制器中访问我的任务模型,但我遇到了这个错误Php 函数App\Models\Task::_construct()的参数太少,第26行传递了0,正好传递了一个,php,slim,Php,Slim,我试图使用php slim在控制器中访问我的任务模型,但我遇到了这个错误 函数App\Models\Task::\uu construct()的参数太少,在第26行传递了0,在TodoController.php上正好传递了一个 有什么建议吗?提前感谢,我引用了类似的问题,但我无法将其与我所拥有的联系起来 最终,我希望从Task.php模型中引入方法,并在需要时在控制器中调用它 TodoController.php <?php namespace App\Controllers; u
函数App\Models\Task::\uu construct()的参数太少,在第26行传递了0,在TodoController.php上正好传递了一个
有什么建议吗?提前感谢,我引用了类似的问题,但我无法将其与我所拥有的联系起来
最终,我希望从Task.php模型中引入方法,并在需要时在控制器中调用它
TodoController.php
<?php
namespace App\Controllers;
use Slim\Http\Request;
use Slim\Http\Response;
use App\Models\Task;
class TodosController extends BaseController
{
// 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]);
// }
public function index()
{
$tasks = new Task();
$tasks->getTodos();
}
public function deleteTodo($request, $response, $args)
{
$sth = $this->db->prepare("DELETE FROM tasks WHERE id=:id");
$sth->bindParam("id", $args['id']);
$sth->execute();
$todos = $sth->fetchObject();
$url = urlFor($todos);
var_dump($url);
return $this->response->withJson($todos)->withRedirect('/todos');
}
将某物作为参数传递给新任务()
比如什么?很抱歉,我对这个有点陌生。public function\uuu构造(ContainerInterface$container)
@nogad不知道在parameters@nogad我仍然感到困惑。必须提供一些解释,以便OP有机会学习。我最终使用了雄辩的模型,这要简单得多。不过还是谢谢你
<?php
namespace App\Models;
use Slim\Views\Twig as View;
use Interop\Container\ContainerInterface;
class Task
{
protected $c;
public $db;
public function __construct(ContainerInterface $container)
{
$this->c = $container;
$this->db = $container['db'];
}
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]);
}
}
public function index($req, $res, $args)
{
$tasks = new Task($this->container);
$tasks->getTodos($req, $res, $args);
}