Basecontroller load helpers / library depend on loggedin |
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() ) 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 ;-)
You need to load the auth_helper
PHP Code: // load the Auth helper What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
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'...];}" :-)
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 ...?
i have this in my basecontroller :
Code: protected $theDate; and in my controllers : Code: public function __construct() 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.
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?
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 )
|
Welcome Guest, Not a member yet? Register Sign In |