![]() |
In my controller i try to load helpers like that :
PHP Code: class User extends BaseController In my filter i use : PHP Code: class UserFilter implements FilterInterface 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.
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
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?
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. (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.
Thanks for your time Kenjis and for your clear and precise answers. Have a nice day.
(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.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) |
Welcome Guest, Not a member yet? Register Sign In |