函数Slim \ Views \ TwigExtension :: __ construct()的参数太少,0通过,预期正好2

时间:2017-10-30 16:39:15

标签: php slim

我正在尝试使用urlFor帮助器生成一个删除方法的链接,我正在使用twig扩展来利用urlFor方法,但我得到了错误

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

不确定参数中包含哪些内容。

最终我试图点击删除链接删除任务。

任何建议,提前谢谢。

以下是 dependencies.php 文件。

<?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;
};

这是我的删除方法 TodosController.php 的部分

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');


}

这是我的删除方法的路由链接,它位于路径文件

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

这里是 todos.twig ,其中我显示删除链接

{% 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 %}

1 个答案:

答案 0 :(得分:1)

删除额外的垃圾代码。

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

        return $view;
    };