简体   繁体   中英

Slim 3 and Twig - global variable from a db query

here I am today with another question:

I'm working with Slim 3 framework and Twig template engine, I'm building a mini-CMS that allows the user (among other things) to edit the navigation menu. Now, the menu elements are, basically, all the categories that I've stored in a MySQL table called "categories", the problem is that I don't know how to tell slim (and eloquent, since I'm using it) to execute a query to pass to the twig container, cause I already know that if I want the menu array available in all views I have to inject it globally, I just don't know where to declare the function. I've seen similar questions where the only answer is how to inject the variable, not where to write the function. Should I just write it inside dependencies.php and add:

use App\Model\Category;

or is there another way? I wanna write a function that takes all the categories and stores them in a variable that I can pass to the container globally

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;

};

folders structure

- 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

Let's assume you have a method named getNestedCategories in App\\Model\\Category which is responsible for retrieving categories from database. I would call it to set a global variable for twig where you are setting the other global variable currentUrl , that means in dependencies.php, something like this:

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

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