Silex 微框架和 Twig:启用调试

发布于 2025-01-03 11:44:34 字数 2461 浏览 3 评论 0原文

我的问题:如何允许在 Silex 内的 Twig 模板中使用调试


我正在使用 Silex 微框架(一个利用 Symfony 的 PHP 框架)。

当使用 Twig 模板系统时,我想输出一个特定的对象。通常我会使用 var_dump($app); 来完成此操作,并在 Twig 中使用 {% debug app %} 来完成此操作。

我的问题是进行调试(并将 Silex 自己的调试设置为 true 对 Twig 没有帮助)以与 Silex 一起使用。开箱即用的对 debug 的调用将导致错误消息:

Twig_Error_Syntax: Unknown tag name "debug" in...

对 debug 的调用如下所示:

{% debug app %}

我找到了有关如何配置 Twig 的 config.yml 文件以正确使用 debug 的参考资料 但 Silex 不使用 Twig 的配置文件。

Silex 确实说你可以通过将关联数组传递给 twig.options 来设置选项,而 Twig 文档说你可以传递一个环境选项,例如:

$twig = new Twig_Environment($loader, array('debug' => true));

尝试在 Silex 中传递它,例如:

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.options' => array('debug' => true),
));

不起作用。这是错误的选择吗?只是格式错误?我不知道,我尝试过的任何方法都不起作用。

我感觉自己进入了“车轮旋转”模式,所以我在这里询问,希望今天早上我能继续进行更有成效的工作。 :)

(呃...对于一个超特定的 StackOverflow 问题怎么样?)


解决方案:(所有这些只是为了获得类似 var_dump 的功能...哦天哪):老实说,这有点令人痛苦,Silex 文档对发现这一点没有任何帮助,但这是我必须做的才能让它发挥作用。

首先加载Twig自动加载器:

$app['autoloader']->registerPrefixes(array(
    'Twig_Extensions_'  => array(__DIR__.'/vendor/Twig-extensions/lib')));

为什么一定要这样注册呢?不知道。它实际上是如何找到自动加载器的?不知道。但它有效。

注册提供程序并设置调试选项:

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path'         => __DIR__.'/views',
    'twig.class_path'   => __DIR__.'/vendor/Twig/lib',
    'twig.options'      => array('debug' => true), //<-- this is the key line to getting debug added to the options
));

最后(最好的部分):

$oldTwigConfiguration = isset($app['twig.configure']) ? $app['twig.configure']: function(){};
$app['twig.configure'] = $app->protect(function($twig) use ($oldTwigConfiguration) {
    $oldTwigConfiguration($twig);
    $twig->addExtension(new Twig_Extensions_Extension_Debug());
});

说实话,我认为 Silex 对我来说已经足够了。

此解决方案的功劳归功于 Nerdpress。


*ninja编辑:一年半后我不得不说 Silex 对我来说是个废物。我一直在使用 Slim 来满足所有微框架需求,它非常棒。快速、干净、简单、轻松地完成工作。

My question: How do I permit use of debug in Twig templates within Silex?


I'm playing around with the Silex micro-framework (a PHP framework that leverages Symfony).

When using the Twig template system with it I wanted to output a particular object. Normally I would do this with var_dump($app); and in Twig with {% debug app %}.

My problem is getting debug (and setting Silex's own debug to true does not help with Twig) to work with Silex. Out of the box a call to debug will result in an error message:

Twig_Error_Syntax: Unknown tag name "debug" in...

The call to debug looks like this:

{% debug app %}

I have found references to how to configure Twig's config.yml file to correctly use debug but Silex does not use a config file for Twig.

Silex does say you can set options via passing an associative array to twig.options and while Twig docs say you can pass an environment option like:

$twig = new Twig_Environment($loader, array('debug' => true));

Trying to pass it in Silex like:

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.options' => array('debug' => true),
));

Does not work. Is this the wrong sort of option? Simply incorrect formatting? I've no idea and nothing I have tried works.

I sense myself getting into "wheel spinning" mode so I am asking here on SO in hopes that I can move on to more productive work this morning. :)

(ugh... how's that for a hyper-specific StackOverflow question?)


Solution: (all this just to get var_dump like functionality... oh my): This was a bit of a pain in the butt, to be honest, and the Silex docs were of no help whatsoever in discovering this but here is what I had to do to get this to work.

First load the Twig autoloader:

$app['autoloader']->registerPrefixes(array(
    'Twig_Extensions_'  => array(__DIR__.'/vendor/Twig-extensions/lib')));

Why do you have to register it this way? No idea. How does it actually find the autoloader? No idea. But it works.

Register the provider and set the debug option:

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path'         => __DIR__.'/views',
    'twig.class_path'   => __DIR__.'/vendor/Twig/lib',
    'twig.options'      => array('debug' => true), //<-- this is the key line to getting debug added to the options
));

And finally (the best part):

$oldTwigConfiguration = isset($app['twig.configure']) ? $app['twig.configure']: function(){};
$app['twig.configure'] = $app->protect(function($twig) use ($oldTwigConfiguration) {
    $oldTwigConfiguration($twig);
    $twig->addExtension(new Twig_Extensions_Extension_Debug());
});

To be honest, I think that's enough Silex for me.

Credit for this solution goes to Nerdpress.


*ninja edit: a year and a half later I have to say that Silex was a dud for me. I have been using Slim for all micro-framework needs and it is fantastic. Gets the job done quickly, cleanly, simply and easily.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝不放开 2025-01-10 11:44:34

我完全删除了旧答案,以显示我构建的一个小示例应用程序的输出:

composer.json:

{
  "require": {
    "silex/silex": "1.*",
    "twig/twig": "1.*",
    "twig/extensions": "*"
  }
}

app.php:

require_once __DIR__ . '/../vendor/.composer/autoload.php';

$app = new Silex\Application();

$app['debug'] = true;

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__ . '/views',
    'twig.options' => array('debug' => true)
));
$app['twig']->addExtension(new Twig_Extensions_Extension_Debug());

$app->get('/', function () use ($app) {
    return $app['twig']->render('debug_test.twig', array('app' => $app));
});
$app->run();

I completely removed the old answer to show the output from a little example-app I built:

composer.json:

{
  "require": {
    "silex/silex": "1.*",
    "twig/twig": "1.*",
    "twig/extensions": "*"
  }
}

app.php:

require_once __DIR__ . '/../vendor/.composer/autoload.php';

$app = new Silex\Application();

$app['debug'] = true;

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__ . '/views',
    'twig.options' => array('debug' => true)
));
$app['twig']->addExtension(new Twig_Extensions_Extension_Debug());

$app->get('/', function () use ($app) {
    return $app['twig']->render('debug_test.twig', array('app' => $app));
});
$app->run();
您的好友蓝忘机已上羡 2025-01-10 11:44:34

对于使用 pimple 3 的 silex ^2.2,share() 已被删除,因此请使用:

$app->extend('twig', function($twig, $app) {
    $twig->addExtension(new Twig_Extension_Debug());
    return $twig;
});

for silex ^2.2 using pimple 3 the share() has been removed so use:

$app->extend('twig', function($twig, $app) {
    $twig->addExtension(new Twig_Extension_Debug());
    return $twig;
});
预谋 2025-01-10 11:44:34

已经有一段时间了,所以我对接受的答案做了一个小更新,你可以使用 Pimple 的新扩展方法:

composer.json:

"silex/silex": "~1.3",
"twig/twig": "^1.23",
"symfony/twig-bridge": "^2.7",
"twig/extensions": "^1.3"

index.php(前端控制器)

/*
* config
*/
//...
$app->register(new Silex\Provider\TwigServiceProvider(), array(
        'twig.path' => __DIR__.'/../templets',
        'twig.options' => array('debug' => true),
    )
);
$app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
    $twig->addExtension(new Twig_Extension_Debug());
    return $twig;
}));
//...


/*
* some static page
*/
$app->get('/', function () use($app) {
    return $app['twig']->render('index.twig');
});

$app->run();

It has been a while now so I did a small update to the accepted answer, you can use the new extend method of Pimple:

composer.json:

"silex/silex": "~1.3",
"twig/twig": "^1.23",
"symfony/twig-bridge": "^2.7",
"twig/extensions": "^1.3"

index.php (front controller)

/*
* config
*/
//...
$app->register(new Silex\Provider\TwigServiceProvider(), array(
        'twig.path' => __DIR__.'/../templets',
        'twig.options' => array('debug' => true),
    )
);
$app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
    $twig->addExtension(new Twig_Extension_Debug());
    return $twig;
}));
//...


/*
* some static page
*/
$app->get('/', function () use($app) {
    return $app['twig']->render('index.twig');
});

$app->run();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文