【问题标题】:Adding a new global variable in twig在树枝中添加一个新的全局变量
【发布时间】:2016-12-26 17:02:11
【问题描述】:

我正在尝试获取登录页面中添加的新字段的数据。我做了什么:

  1. 修改AccountController.php登录功能增加新参数:$this->_app->login($user, $client, !empty($data['rememberme']))
  2. Userfrosting.php登录功能我已经在应用程序中设置它:$this->client = $client;
  3. setupTwigUserVariables 函数中添加了全局树枝:$twig->addGlobal("client", $this->client);

问题是在模板中,{{client.id}} 什么都不返回。任何帮助将不胜感激。

【问题讨论】:

  • 当您将$client 的值传递给$this->_app->login() 时,您最初是如何设置它的?
  • $client = Client::where('client_id', $data['client_id']) ->where('userid', $user->id) ->first();
  • 你能确认这实际上加载了一个Client 对象吗?也许使用error_log($client->id)
  • error_log($this->client); 就在addGlobal 返回{"id":3,"client_name":"Client2","client_id":2,"dbtable":"aaa","userid":3} 之前
  • 确定它是一个对象。问题似乎与数据无关,因为 '$twig->addGlobal('test', 'Slim test');` 也不起作用

标签: twig userfrosting


【解决方案1】:

在 UserFrosting 4 中,您应该在 Sprinkle 的 src/Twig/ 目录中创建一个 Twig 扩展,并将变量添加到 getGlobals 的返回值中。

您的情况有点棘手,因为我不确定client 如何成为全局变量,但同时取决于$data['client_id'] - 这似乎是一个请求参数。现在,我假设您在提交此参数时包含任何需要 client 变量的请求。

<?php
/**
 * Stack Overflow
 *
 * @link      https://stackoverflow.com
 */
namespace UserFrosting\Sprinkle\Site\Twig;

use Interop\Container\ContainerInterface;
use UserFrosting\Sprinkle\Site\Database\Models\Client;

/**
 * Extends Twig functionality for the Site sprinkle.
 *
 * @author Jose Luis
 */
class Extension extends \Twig_Extension
{

    protected $services;
    protected $config;

    public function __construct(ContainerInterface $services)
    {
        $this->services = $services;
        $this->config = $services->config;
    }

    public function getName()
    {
        return 'myproject';
    }

    public function getGlobals()
    {
        try {
            $currentUser = $this->services->currentUser;

            // Assumes the client_id is being submitted as a query string (url) parameter
            $clientId = $this->services->request->getQueryParam('client_id');

            $client = Client::where('client_id', clientId)->where('userid', $currentUser->id)->first();
        } catch (\Exception $e) {
            $client = null;
        }

        return [
            'client' => $client
        ];
    }
}

然后您需要在 Sprinkle 的 service provider class 中注册此扩展程序:

<?php
/**
 * Stack Overflow
 *
 * @link      https://stackoverflow.com
 */
namespace UserFrosting\Sprinkle\Site\ServicesProvider;

use UserFrosting\Sprinkle\Site\Twig\Extension as JoseExtension;

/**
 * Services provider for the Site sprinkle.
 *
 * @author Jose Luis
 */
class ServicesProvider
{
    /**
     * Register extended user fields services.
     *
     * @param Container $container A DI container implementing ArrayAccess and container-interop.
     */
    public function register($container)
    {
        /**
         * Extends the 'view' service with Jose's Twig Extension.
         */
        $container->extend('view', function ($view, $c) {
            $twig = $view->getEnvironment();
            $extension = new JoseExtension($c);
            $twig->addExtension($extension);

            return $view;
        });
    }
}

是的,我知道这里有很多样板。但是,一旦您第一次设置这些,以后就可以很容易地向 Twig 环境添加新的变量/函数/过滤器,并在将来向 Sprinkle 添加新的服务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    • 2015-05-11
    相关资源
    最近更新 更多