Welcome Guest, Not a member yet? Register   Sign In
How to set global layout data?
#2

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Messages In This Thread
How to set global layout data? - by forumz - 10-27-2021, 05:32 PM
RE: How to set global layout data? - by InsiteFX - 10-28-2021, 01:51 AM
RE: How to set global layout data? - by forumz - 10-28-2021, 12:08 PM



Theme © iAndrew 2016 - Forum software by © MyBB