简体   繁体   中英

Too few arguments to function Slim\Views\TwigExtension::__construct(), 0 passed and exactly 2 expected

I'm trying to generate a link to a delete method using the urlFor helper, and i am using twig extensions to utilize the urlFor method but im getting the error

Too few arguments to function Slim\\Views\\TwigExtension::__construct(), 0 passed in /Applications/MAMP/htdocs/eli35/src/dependencies.php on line 47 and exactly 2 expected

not sure what to include in the parameters.

Ultimately im trying to click on the delete link to delete a task.

Any suggestions, thanks in advance.

Here is the dependencies.php file.

<?php
// DIC configuration




$container = $app->getContainer();


// view renderer
$container['renderer'] = function ($c) {
    $settings = $c->get('settings')['renderer'];
    return new Slim\Views\PhpRenderer($settings['template_path']);
};

// monolog
$container['logger'] = function ($c) {
    $settings = $c->get('settings')['logger'];
    $logger = new Monolog\Logger($settings['name']);
    $logger->pushProcessor(new Monolog\Processor\UidProcessor());
    $logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], $settings['level']));
    return $logger;
};


$container['db'] = function($c){
    $settings = $c->get('settings')['db'];
    $pdo = new PDO("mysql:host=" . $settings['host'] . ";dbname=" . $settings['dbname'] . ";port=" . $settings['port'],
    $settings['user'], $settings['pass']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    return $pdo;
};



$container['view'] = function ($container) {
    $view = new \Slim\Views\Twig( 'views', [
        'cache' => false,
    ]);
    $view->addExtension(new \Slim\Views\TwigExtension(
        $container->router,
        $container->request->getUri()
    ));

    $view->parserExtensions = array(
        new \Slim\Views\TwigExtension(),
    );

    return $view;
};

here is the section of the TodosController.php for my delete method

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->fetchAll();


    return $this->response->withRedirect('/todos');


}

here is the route link for my delete method, its in the routes file

$app->delete('/todo/[{id}]', '\\App\\Controllers\\TodosController:deleteTodo')->setName("deletetask");

here is todos.twig where im displaying the delete link

{% extends "templates/layout.html" %}

{% block content %}
<h1>My Todos</h1>

<ol>
  {% for task in todos %}
        <div class="myl">
        <li><h4>{{ task.task}}</h4></li>
        <small style="font-style:italic">{{task.created_at |date("m/d/Y")}}</small></br>


        <a href="{{ urlFor('deletetask', {'id': task.id}) }}">Delete</a>



        </div>
    {% endfor %}

</ol>
{% endblock %}

Remove the extra junk code.

$container['view'] = function ($container) {
        $view = new \Slim\Views\Twig( 'views', [
            'cache' => false,
        ]);
        $view->addExtension(new \Slim\Views\TwigExtension(
            $container->router,
            '/'
        ));

        return $view;
    };

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM