函数 App\Models\Task::__construct() 的参数太少,第 26 行传递了 0 个且正好是一个
Too few arguments to function App\Models\Task::__construct(), 0 passed on line 26 and exactly one
我正在尝试使用 php slim 在控制器中访问我的任务模型,但我收到此错误
Too few arguments to function App\Models\Task::__construct(), 0 passed on line 26 and exactly one on 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');
}
如果这对我有帮助,这是我的 Task.php
<?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]);
}
}
我也看过另一个类似的例子like this,但是还是不明白我做错了什么。
public function index($req, $res, $args)
{
$tasks = new Task($this->container);
$tasks->getTodos($req, $res, $args);
}
我正在尝试使用 php slim 在控制器中访问我的任务模型,但我收到此错误
Too few arguments to function App\Models\Task::__construct(), 0 passed on line 26 and exactly one on 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');
}
如果这对我有帮助,这是我的 Task.php
<?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]);
}
}
我也看过另一个类似的例子like this,但是还是不明白我做错了什么。
public function index($req, $res, $args)
{
$tasks = new Task($this->container);
$tasks->getTodos($req, $res, $args);
}