I'm trying to access my task models within a controller using php slim but im getting this error
Too few arguments to function App\\Models\\Task::__construct(), 0 passed on line 26 and exactly one on TodoController.php
Any suggestions ? thanks in advance, i referenced similar questions like this but i couldn't tie it into what i had.
Ultimately i want to pull in methods from the Task.php model and call it in the controller when needed.
the 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');
}
And if this helps here is my 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]);
}
}
I also viewed another similar example like this , however still couldn't understand what i was doing wrong.
public function index($req, $res, $args)
{
$tasks = new Task($this->container);
$tasks->getTodos($req, $res, $args);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.