Welcome Guest, Not a member yet? Register   Sign In
Filters & Helper Controller load
#1

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.
Reply
#2

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

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

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

(This post was last modified: 07-26-2022, 11:13 PM by kenjis.)

(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/CodeIgnit...ontrollers

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/CodeIgnit...vices.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.
Reply
#6

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

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB