Slim 3和Twig - 来自db查询的全局变量

时间:2017-05-24 22:32:57

标签: php twig slim

今天我今天又提出了另一个问题:

我正在使用Slim 3框架和Twig模板引擎,我正在建立一个迷你CMS,允许用户(除其他外)编辑导航菜单。现在,菜单元素基本上都是我存储在名为"类别"的MySQL表中的所有类别,问题是我不知道怎么讲苗条(和雄辩,因为我正在使用它来执行查询以传递到树枝容器,因为我已经知道如果我想在所有视图中都可以使用菜单数组我必须全局注入它,我只是不要'知道 where 来声明该功能。我看过类似的问题,唯一的答案是如何注入变量,而不是写函数的位置。我应该在dependencies.php中编写它并添加:

use App\Model\Category;

还是有另一种方式?我想编写一个函数来获取所有类别并将它们存储在一个我可以传递给全局容器的变量中

routes.php文件

$app->get('/', 'HomeController:index')->setName('homepage');
$app->get('brands', 'MainController:index')->setName('all_brands');
$app->get('/{category}', 'MainController:single_category');
$app->get('/{category}/{brand}', 'MainController:single_showbrand');

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

// Twig
$container['view'] = function ($c) {
    $view = new \Slim\Views\Twig('templates');

    // Instantiate and add Slim specific extension
    $basePath = rtrim(str_ireplace('index.php', '', $c['request']->getUri()->getBasePath()), '/');
    $view->addExtension(new Slim\Views\TwigExtension($c['router'], $basePath));
    $view->getEnvironment()->addGlobal('currentUrl',$c->get('request')->getUri());

    return $view;
};

// Controllers
// -- HomeController.php

$container['HomeController'] = function ($container) {
 return new \App\Controllers\HomeController($container);
};

// -- MainController.php
$container['MainController'] = function ($container) {
 return new \App\Controllers\MainController($container);
};

// Eloquent Database Injection

$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection($container['settings']['db']);
$capsule->setAsGlobal();
$capsule->bootEloquent();

$container['db'] = function ($container) use ($capsule) {
    return $capsule;
};

// Validator

$container['validator'] = function ($container) {
 return new App\Validation\Validator;

};

文件夹结构

- application
 - Controllers
  - HomeController.php
  - MainController.php
 - Models
  - User.php
  - Category.php
  - Brand.php
 - Validation
 - assets
  - css
  - js
  - ...
 - logs
 - node_modules
  - ...
  - ...
 - public
  - index.php
 - src
  - dependencies.php
  - middleware.php
  - routes.php
  - settings.php
 - templates

1 个答案:

答案 0 :(得分:0)

假设您在getNestedCategories中有一个名为App\Model\Category的方法,该方法负责从数据库中检索类别。我会称之为为你设置另一个全局变量currentUrl的twig设置一个全局变量,这意味着在dependencies.php中,如下所示:

// Twig
$container['view'] = function ($c) {
    $view = new \Slim\Views\Twig('templates');

    // Instantiate and add Slim specific extension
    $basePath = rtrim(str_ireplace('index.php', '', $c['request']->getUri()->getBasePath()), '/');
    $view->addExtension(new Slim\Views\TwigExtension($c['router'], $basePath));
    $view->getEnvironment()->addGlobal('currentUrl',$c->get('request')->getUri());
    // Add categories
    $category = new \App\Model\Category;
    $view->getEnfironment()->addGlobal('categories',$category->getNestedCategories());

    return $view;
};