CodeIgniter Forums
How to set global layout data? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to set global layout data? (/showthread.php?tid=80402)



How to set global layout data? - forumz - 10-27-2021

Is there a way to set layout data in a controller, so that the data is available every time when the layout is rendered?
CodeIgniter 3 had this functionality available and it worked well. The new layout system is more like one Django uses, and I absolutely hate it. Maybe I'm just missing something here ...
Help on this greatly appreciated!


RE: How to set global layout data? - InsiteFX - 10-28-2021

I createed a method in the BaseController to merge a global array with the Controllers data array.

BaseController:

PHP Code:
<?php

namespace App\Controllers;

use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\CLIRequest;
use 
CodeIgniter\HTTP\IncomingRequest;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
CodeIgniter\Services;
use 
Psr\Log\LoggerInterface;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *    class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 */
class BaseController extends Controller
{
    /**
    * Instance of the main Request object.
    *
    * @var CLIRequest|IncomingRequest
    */
    protected $request;

    /**
    * An array of helpers to be loaded automatically upon
    * class instantiation. These helpers will be available
    * to all other controllers that extend BaseController.
    *
    * @var array
    */
    protected $helpers = [];

    /**
    * @var \CodeIgniter\Session\Session
    */
    protected $session;

    /**
    * @var array - Global data array for views.
    *
    * Fill in your global view data here that will be
    * used in all views.
    */
    protected static $globalViewData = [
        'test' => 'This works!',
    ];

    /**
    * Constructor.
    */
    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request$response$logger);

        // Preload any models, libraries, etc, here.

        // E.g.: $this->session = \Config\Services::session();

        // Ensure that the session is started and running
        if (session_status() == PHP_SESSION_NONE)
        {
            $this->session Services::session();
        }

    }

    /**
    * mergeGlobalData ()
    * -------------------------------------------------------------------
    *
    * You must set the globalViewData array above with your global data
    * for all Controllers.
    *
    * Usage:
    *
    * $data = $this->mergeGlobalData($data);
    * return view('your_view', $data);
    * 
    * @param  array $viewData
    * @return array
    */
    protected function mergeGlobalData(array $viewData) : array
    {
        if (is_array(self::$globalViewData))
        {
            return array_merge($viewDataself::$globalViewData);
        }

        return [];
    }

    // --------------------------------------------------------------------




Read the comments.
Some times you will need to create an empty view_data.php view file to call first to load global data.


RE: How to set global layout data? - forumz - 10-28-2021

Thank for the reply.
I looked closer at how templates are rendered in the View class of CI4 and decided not to use them at all. Instead I just render views.
In order to pass common data to views like header or footer I added a simple static variable in BaseController class. The variable is just an associative array. It contains keys and values.
PHP Code:
public static $globals = []; 

If I need to add some global data somewhere, like in the BaseController, I just create a key and assign a value.
PHP Code:
self::$globals['key'] = 'value'

Then in my view:
PHP Code:
esc(\App\Controller\BaseController::$globals['key']); 
It is ugly quick fix, but dealing with the mess made with templates in CI4 is even worse.