Welcome Guest, Not a member yet? Register   Sign In
ArgumentCountError Too few arguments to function ::__construct()
#1

I am trying to implement hexagonal architecture in CI4, I want to make a method to save in the database which actions were executed, for this I am doing the following:
app/PunkCore/Shared/Interfaces/Repository/UserActivityRepositoryInterface.php

PHP Code:
<?php
namespace App\PunkCore\Shared\Interfaces\Repository;

use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;

interface 
UserActivityRepositoryInterface
{
    public function saveActivity(
        string $method,
        bool $succeed,
        RequestInterface $request,
        ResponseInterface $response): void;


app/PunkCore/Shared/Infrastructure/Repository/UserActivityRepository.php

PHP Code:
<?php

namespace App\PunkCore\Shared\Infrastructure\Repository;

use 
App\PunkCore\Shared\Interfaces\Repository\UserActivityRepositoryInterface;

use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;

use 
App\Models\UserActivity;

class 
UserActivityRepository implements UserActivityRepositoryInterface
{

    public function saveActivity(
        string $method,
        bool $succeed,
        RequestInterface $request,
        ResponseInterface $response): void
    
{
        $user auth()->user();
        UserActivity::create([
            'method' => $method,
            'succeed' => $succeed,
            'user' => $user->username,
            'ip' => $request->getIPAddress(),
            'date' => date('Y-m-d H:i:s'),
            'request' => $request->getBody(),
            'response' => $response->getBody(),
        ]);
    }

app/PunkCore/Shared/Interfaces/Services/UserActivityServiceInterface.php
PHP Code:
<?php
namespace App\PunkCore\Shared\Interfaces\Services;

use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;

interface 
UserActivityServiceInterface
{
    public function logUserActivity(
        string $method,
        bool $succeed,
        string $request,
        string $response
    
): void;

app/PunkCore/Shared/Application/UserActivityService.php

PHP Code:
<?php
namespace App\PunkCore\Shared\Application;

use  App\PunkCore\Shared\Interfaces\Services\UserActivityServiceInterface;
use  App\PunkCore\Shared\Interfaces\Repository\UserActivityRepositoryInterface;

use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;

class 
UserActivityService implements UserActivityServiceInterface
{
    private $userActivityRepository;

    public function __construct(UserActivityRepositoryInterface $userActivityRepository)
    {
        $this->userActivityRepository $userActivityRepository;
    }

    public function logUserActivity(
        string $method,
        bool $succeed,
        RequestInterface $request,
        ResponseInterface $response): void
    
{
        $this->userActivityRepository->saveActivity($method$succeed$request$response);
    }

But when I want to configure the controller it returns this error:

ArgumentCountError

Too few arguments to function App\Controllers\Admin\AdminController::__construct(), 0 passed in /mnt/d/html/punkframework/system/CodeIgniter.php on line 906 and exactly 4 expected
ArgumentCou
ArgumentCountError
Too few arguments to function App\Controllers\Admin\AdminController::__construct(), 0 passed in /mnt/d/html/punkframework/system/CodeIgniter.php on line 906 and exactly 4 expected
APPPATH/Controllers/Admin/AdminController.php at line 42
Code:
35         $this->response = $response;
36         $this->logger = $logger;
37
38         //$this->userActivityRepository = new UserActivityRepository();
39         //$this->userActivityService = new UserActivityService($this->userActivityRepository);
40     }*/
41
42     public function __construct(RequestInterface $request, ResponseInterface $response, UserActivityService $userActivityService, UserActivityRepositoryInterface $userActivityRepository)
43     {
44         $this->request = $request;
45         $this->response = $response;
46         $this->userActivityService = $userActivityService;
47         $this->userActivityRepository = $userActivityRepository;
48     }
49     
  1. SYSTEMPATH/CodeIgniter.php : 906   —  App\Controllers\Admin\AdminController->__construct ()

    Code:
    899      *
    900      * @return Controller
    901      */
    902     protected function createController()
    903     {
    904         assert(is_string($this->controller));
    905
    906         $class = new $this->controller();
    907         $class->initController($this->request, $this->response, Services::logger());
    908
    909         $this->benchmark->stop('controller_constructor');
    910
    911         return $class;
    912     }
    913



app/Controllers/Admin/AdminController.php
PHP Code:
<?php

namespace App\Controllers\Admin;

use 
Psr\Log\LoggerInterface;

use 
CodeIgniter\API\ResponseTrait;
use 
App\Controllers\BaseController;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;

use 
App\PunkCore\Shared\Interfaces\Repository\UserActivityRepositoryInterface;
use 
App\PunkCore\Shared\Infrastructure\Repository\UserActivityRepository;
use 
App\PunkCore\Shared\Interfaces\Services\UserActivityServiceInterface;
use 
App\PunkCore\Shared\Application\UserActivityService;

class 
AdminController extends BaseController
{
    use ResponseTrait;

    protected $request;
    protected $response;
    protected $logger;
    protected $userActivityService;
    protected $userActivityRepository;  

    
public function __construct(RequestInterface $requestResponseInterface $responseUserActivityService $userActivityServiceUserActivityRepositoryInterface $userActivityRepository)
    {
        $this->request $request;
        $this->response $response;
        $this->userActivityService $userActivityService;
        $this->userActivityRepository $userActivityRepository;
    }
    

    
public function index()
    {
        if (!auth()->loggedIn()) {
            return view('admin/Auth/login');
        }
        
    
}

    public function dashboard()
    {
        return view('admin/Auth/dashboard');
    }

    public function login()
    {
        echo'<pre>';print_r($this->request->getBody());echo'</pre>';
       
        $this
->userActivityService->logUserActivity(__CLASS__ '-' __METHOD__true$this->request$this->response);
        
    
}

    public function register()
    {

    }

    public function logout()
    {
        auth()->logout();
    }




and I don't know where I am failing.
Reply
#2

(This post was last modified: 05-19-2023, 05:13 PM by kenjis.)

The controller in CI4 does not have the __construct() method.
See https://github.com/codeigniter4/CodeIgni...roller.php
So if you add __construct(), no parameters are passed by the framework.
Reply
#3

thanks!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB