CodeIgniter Forums
Filters & Helper Controller load - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Filters & Helper Controller load (/showthread.php?tid=82570)



Filters & Helper Controller load - momox19 - 07-25-2022

In my controller i try to load helpers like that :
PHP Code:
class User extends BaseController
{
    protected $helpers = ['asset_helper'];

    public function my_function(){
        methode_in_my_helper();
    
}
In my filter i use :
PHP Code:
class UserFilter implements FilterInterface
{
    public function before(RequestInterface $request$arguments null)
    {
        $utilisateur_test = new User();

        $test $utilisateur_test->my_function();
    }

    public function after(RequestInterface $requestResponseInterface $response$arguments null)
    {
        // TODO: Implement after() method.
    }



And i have one error :

Call to undefined function User\Controllers\methode_in_my_helper();

If I try to call my method from a filter, are the helpers automatically loaded by the controller of this class ignored?

Thx for your time.

Ronan.


RE: Filters & Helper Controller load - kenjis - 07-25-2022

Don't use Controllers in Filters.
It is completely mistake.
Filters are Controller Filters. It runs code before and after a controller execution.

Code:
before Filters --> Controller --> after Filters



RE: Filters & Helper Controller load - momox19 - 07-25-2022

Ok... I understand. So, what now ? In fact, I was using my filter to call a method of User that allows to detect that the user is really connected. To do this, it executes a set of tests such as the existence of a session, the writing of the connection in a .txt file, etc...

Do you advise me in my filter to call a helper or a service that would do these tests?


RE: Filters & Helper Controller load - momox19 - 07-25-2022

Well, I solved my problem by using a library that does the different tests. And i call my library in my filter.

In fact, in Codeigniter, I always have trouble understanding where to put my methods. Between services, helpers, libraries... It's never very clear in my head.


RE: Filters & Helper Controller load - kenjis - 07-26-2022

(07-25-2022, 11:56 PM)momox19 Wrote: In fact, in Codeigniter, I always have trouble understanding where to put my methods. Between services, helpers, libraries... It's never very clear in my head.

Controllers are usually not reusable.
Make your controllers thin. What you should do in controllers is very limited.
See https://codeigniter4.github.io/CodeIgniter4/concepts/mvc.html#controllers

If you should not put the code in Models, probably it is put in Libraries.

Helpers are PHP functions, not classes, so you don't need to create it.
All things that can do in a function can be done by a class.

Services just create objects or return existing objects.
https://codeigniter4.github.io/CodeIgniter4/concepts/services.html
So if you use your own service, you write code in your library,
and the Service you add returns the instance of the library class.

In the end, most of the code will be models or libraries.


RE: Filters & Helper Controller load - momox19 - 07-26-2022

Thanks for your time Kenjis and for your clear and precise answers. Have a nice day.


RE: Filters & Helper Controller load - ozornick - 07-26-2022

(07-25-2022, 11:56 PM)momox19 Wrote: Well, I solved my problem by using a library that does the different tests. And i call my library in my filter.

In fact, in Codeigniter, I always have trouble understanding where to put my methods. Between services, helpers, libraries... It's never very clear in my head.
Create self layer service. 
UserController->show($user_id) init $UserService
This run $UserService->getUser($user_id) with some function, helper, libraries, model..
In service use $UserModel->find($user_id)
Profit!
In controller one line: 
PHP Code:
$user $UserService->getUser($user_id
Next step, use this service method in other controller types: api, cli. Anywhere one line reuse for get User