Welcome Guest, Not a member yet? Register   Sign In
Basecontroller load helpers / library depend on loggedin
#1

(This post was last modified: 12-29-2024, 10:18 PM by 4usol.)

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 ;-)
Reply
#2

(This post was last modified: 12-29-2024, 11:56 PM by InsiteFX.)

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');

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 01-03-2025, 12:21 AM by 4usol.)

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'...];}"
:-)
Reply
#4

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 ...?
Reply
#5

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.
CMS CI4 A CMS system, runs out of the box written on top of CI4
Arch Book  CodeIgniter4 on Apache(pages 92-114) 
Github  
Reply
#6

(This post was last modified: 01-03-2025, 03:35 AM by 4usol.)

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?
Reply
#7

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

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB