• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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

#2
Re: helper function

in CI 4.0.4 what i'm currently doing in BaseController.php

Code:
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use CodeIgniter\I18n\Time;

class BaseController extends Controller
{

public function __construct()
        {
        helper(['text', 'date','uri','html','form','security','number']);

        $this->theTime = now('Europe/London');
        //see the above look how i use it in controller     
        }


then in my controller


Code:
<?php namespace App\Controllers;

use CodeIgniter\Controller;
use \App\Andy\BlogModel;
use \App\Andy\ImgPreCheck;
use CodeIgniter\I18n\Time;

class Blog  extends BaseController
{
     protected $blogId;
     protected $myDate;
     protected $myTime;

                 public function __construct()
                    {
                        parent::__construct();
                        $this->myTime = parent::getTime();
                        $this->myDate= date("d/m/Y",$this->myTime);     
                    
                   }


because all my controllers "extend" BaseController

they get functionality of helper.

So for instance on view i want footer to display Copywrite from : to

I pass date to view in controller's as:
Code:
'date'=>$this->myDate


then in view "footer" i simple quote <?php echo $date ;?>

does this give you any clue ?


i'm sure somebody will tell me there's a more elegant way of doing it but main points of my point are:


1) if you declare helper in BaseController and then extend your Controller from BaseController that saves you to having to repeat helper code in every controller you use. I use a construct in BaseController so that when i use a controller that will be called. I don't call "$this->myDate" in each controller because its a member property of the BAseController but because my controllers extend BAseController they inherit or have access to attribute of BaseController. I probably need to review public, protected at some point but my approach is :get ot working" then refine later
Reply

#3
(09-24-2020, 07:49 AM)captain-sensible Wrote: Re: helper function

in CI 4.0.4 what i'm currently doing in BaseController.php

Code:
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use CodeIgniter\I18n\Time;

class BaseController extends Controller
{

public function __construct()
        {
        helper(['text', 'date','uri','html','form','security','number']);

        $this->theTime = now('Europe/London');
        //see the above look how i use it in controller     
        }


then in my controller


Code:
<?php namespace App\Controllers;

use CodeIgniter\Controller;
use \App\Andy\BlogModel;
use \App\Andy\ImgPreCheck;
use CodeIgniter\I18n\Time;

class Blog  extends BaseController
{
     protected $blogId;
     protected $myDate;
     protected $myTime;

                 public function __construct()
                    {
                        parent::__construct();
                        $this->myTime = parent::getTime();
                        $this->myDate= date("d/m/Y",$this->myTime);     
                    
                   }


because all my controllers "extend" BaseController

they get functionality of helper.

So for instance on view i want footer to display Copywrite from : to

I pass date to view in controller's as:
Code:
'date'=>$this->myDate


then in view "footer" i simple quote <?php echo $date ;?>

does this give you any clue ?


i'm sure somebody will tell me there's a more elegant way of doing it but main points of my point are:


1) if you declare helper in BaseController and then extend your Controller from BaseController that saves you to having to repeat helper code in every controller you use. I use a construct in BaseController so that when i use a controller that will be called. I don't call "$this->myDate" in each controller because its a member property of the BAseController but because my controllers extend BAseController they inherit or have access to attribute of BaseController. I probably need to review public, protected at some point but my approach is :get ot working" then refine later

the helper function called first and in the var $data['sidebar'] I called the sidebar menu..
If i try to use my code into CI4, seems like the helper function doesn't works, it's make the variable $data['sidebar'] return nothing..
Reply

#4
You can also load helpers in the app/Common.php
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
(09-27-2020, 03:54 PM)InsiteFX Wrote: You can also load helpers in the app/Common.php

I try to using my helper function inside Common.php and called inside BaseController.php but still not work..
When I call variable sidebar, still return nothing..
I just wondering, why it's doesn't work on CI4 ? because I try the code above in a new CI3 and it's work.
Reply

#6
Your not calling a helper you need to autoload your Library in Config/Autoload in
the class map at the bottom.

Also there is no more ci instance like ci 3.

For load vars I just call a dummy view first and pass all variables in to that.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.