Welcome Guest, Not a member yet? Register   Sign In
How Autoload function on base controller ?
#1

I try to migrate from CI3 to CI4 and my problem is; in CI3 I can call helper function inside MY_Controller but in CI4 it doesn't work.

in MY_Controller.php
PHP Code:
<?php

defined
('BASEPATH') or exit('No direct script access allowed');

class 
MY_Controller extends CI_Controller
{
    public function 
__construct()
    {
        
parent::__construct();
        
init_sidebar_menu();
        
$data['sidebar'] = $this->sideMenu->getSideMenu();
        
$this->load->vars($data);
    }


in my sideMenu Libraries
PHP Code:
<?php

defined
('BASEPATH') or exit('No direct script access allowed');

class 
sideMenu
{
    private $ci;

    private $items = [];

    private $child = [];

    public function __construct()
    {
        $this->ci = &get_instance();
    }

    public function getSideMenu()
    {
        return $this->get('sidebar');
    }

    public function addSidebarMenuItem($slug$item)
    {
        $this->add($slug$item'sidebar');
        return $this;
    }

    public function add($slug$item$group)
    {
        $item = ['slug' => $slug] + $item;

        $this->items[$group][$slug] = $item;
    }

    public function get($group)
    {
        $items = isset($this->items[$group]) ? $this->items[$group] : [];

        foreach ($items as $parent => $item) {
            $items[$parent]['children'] = $this->getChild($parent$group);
        }

        $items hooks()->apply_filters("{$group}_menu_items"$items);

        return $items;
    }

    public function getChild($parent_slug$group)
    {
        $children = isset($this->child[$group][$parent_slug]) ? $this->child[$group][$parent_slug] : [];

        $children hooks()->apply_filters("{$group}_menu_child_items"$children$parent_slug);

        return $children;
    }


in my menu helper
PHP Code:
<?php

defined
('BASEPATH') or exit('No direct script access allowed');

function 
init_sidebar_menu()
{
    $CI = &get_instance();

    $CI->sideMenu->addSidebarMenuItem('dashboard', [
        'name'     => 'Dashboard',
        'href'     => base_url(),
        'position' => 1,
        'icon'     => 'fa fa-desktop',
    ]);


How can I call my helper function inside BaseController ?
Because if I use the code above and try to making adjustments to CI4 construction then calling $sidebar var from view, is just show nothing.
Thank you before.
Reply


Messages In This Thread
How Autoload function on base controller ? - by iblisious - 09-24-2020, 02:21 AM



Theme © iAndrew 2016 - Forum software by © MyBB