CodeIgniter Forums
Basecontroller load helpers / library depend on loggedin - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Basecontroller load helpers / library depend on loggedin (/showthread.php?tid=92242)



Basecontroller load helpers / library depend on loggedin - 4usol - 12-29-2024

Hi,

i want to load different helper in case of a user is logged in (shield) or not.

For this i try to do it like this in my basecontroller:
Code:
if( auth()->loggedIn() )

        // Load helpers for logged-in users
        protected $helpers = ['helper1','helper2',...];
}
else
{
        protected $helpers = ['helper1'];
}

Result: "syntax error, unexpected identifier "auth", expecting "function" or "const""

It seems auth is not avivable at this part/moment?

Is there a way to do what i want - maybe also in the autoload-file?

Also i need to include a own library only if a user is logged in, if i have to do it in another way please let me know whats the difference to do it too.

Thanks to help and understanding ;-)


RE: Basecontroller load helpers / library depend on loggedin - InsiteFX - 12-29-2024

You need to load the auth_helper

PHP Code:
// load the Auth helper
helper('auth');

if(
auth()->loggedIn()){
    // Load helpers for logged-in users
    helper(['helper1','helper2',...]);
}
else
{
    helper('helper1');




RE: Basecontroller load helpers / library depend on loggedin - 4usol - 01-03-2025

in my Autoload if've: 'public $helpers = ['auth', 'setting','error'];' already defined.

it seems this works only inside a function.

What i want to do is to setup dynamically the helpers on top of the basecontroller.

/**
    * An array of helpers to be loaded automatically upon
    * class instantiation. These helpers will be available
    * to all other controllers that extend BaseController.
    *
    * @var list<string>
    */
    //protected $helpers = ['auth'];
//my way to define it by logged-in-status

o  i find the way
in basecontroller let the var defination
protected $helpers = [];

Inside the"function initController"
i can change it for my needs.
"if(auth()->loggedIn()) {$helpers = ['error'...];}"
:-)


RE: Basecontroller load helpers / library depend on loggedin - 4usol - 01-03-2025

one gerneral question.

If i put something in the base_controller, will this loaded on every single request again and again or only one-time an stored in session or mem ...?


RE: Basecontroller load helpers / library depend on loggedin - captain-sensible - 01-03-2025

i have this in my basecontroller :

Code:
protected $theDate;
     protected $theTime;
    
    
     public function __construct()
     {
        
        helper (['text','date','uri','html','form','security','number']);
        $this->theTime =now('Europe/London');
        
    }
    
     protected function getTime()
     {
         return $this->theTime;
        
     }


and in my controllers :


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


now when i instantiate a controller class i can get the time property which i pass to my main template view. I have found that

Code:
$this->myDate

is available for any method of that class , so the property and helpers are there for duration of use of instantiated class, thus data is being maintained by server and whilst browser active.


RE: Basecontroller load helpers / library depend on loggedin - 4usol - 01-03-2025

Great thats explain a lot, i see "parent::__construct();" what does it effects? How does it works?



Is there a way to make a function automatically runs to beginneing of each controller - without coding "$this->myDate" like in your example?

Create some function on init basecontroller wont do that (this will only runs first time)

Also is a way to make this in the base_controller accessable in all other controler without do it again?


RE: Basecontroller load helpers / library depend on loggedin - InsiteFX - 01-04-2025

Yes, in CodeIgniter 4, helpers essentially act like global functions once loaded,
meaning that after you load a specific helper file, the functions within it can be
accessed from anywhere in your application, including controllers and views,
just like a global function; you can call them directly without needing to
instantiate an object first.

Key points about CodeIgniter helpers:

Function collections:
Each helper file is a collection of related functions focused on a specific task,
like form creation, URL manipulation, or text formatting.

Loading required:
Unlike built-in functions, you need to explicitly load a helper file before using
its functions.

Access anywhere:
Once loaded, the helper functions become accessible throughout your application,
similar to global functions.