Welcome Guest, Not a member yet? Register   Sign In
Repository Pattern in CodeIgniter 4
#1

How to apply, for example, Repository Pattern (or other pattern) in CodeIgniter 4, is it possible?
Reply
#2

What do you mean?
You study the desired pattern and implement it.
Reply
#3

(04-04-2022, 06:23 AM)iRedds Wrote: What do you mean?
You study the desired pattern and implement it.

I couldn't implement in my controller, which extends from ResourceController or BaseController...

I tried in the __construct method and in the index to use like this: TesteRepositoryInterface $testeRepository

I have already created the files below:
App\Models\TesteModel
App\Repositories\AbstractRepository
App\Interfaces\TesteRepositoryInterface

Where the TesteRepository class extends AbstractRepository implements TesteRepositoryInterface, but I get an error like this:

ArgumentCountError
Too few arguments to function App\Controllers\TesteController::__construct(), 0 passed in /home/fabiano/api/vendor/codeigniter4/framework/system/CodeIgniter.php on line 809 and exactly 1 expected
Reply
#4

(04-04-2022, 07:54 AM)fsouzaweb Wrote: I tried in the __construct method and in the index to use like this: TesteRepositoryInterface $testeRepository

Why do you inject it?
There is no functionality to inject an object to a controller automatically in CI4.

If you really want to do it, you need to extend the framework.
Add a DI container you like when the framework creates a controller.

This is my example:
https://github.com/kenjis/ci4-tettei-app...er.php#L11
https://github.com/kenjis/ci4-tettei-app...es.php#L34
Reply




Theme © iAndrew 2016 - Forum software by © MyBB