Welcome Guest, Not a member yet? Register   Sign In
How Autoload function on base controller ?
#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


Messages In This Thread
RE: How Autoload function on base controller ? - by iblisious - 09-27-2020, 12:15 AM



Theme © iAndrew 2016 - Forum software by © MyBB