Welcome Guest, Not a member yet? Register   Sign In
Controller or its method is not found: \\App\\Controllers\\Home::index
#1

(This post was last modified: 02-27-2022, 05:09 AM by spreaderman.)

This is such a beginner problem but I just cannot figure it out.  Trying to build a simple api.

Here is my controller for creating an employee...

PHP Code:
<?php
namespace App\Controllers\Api\v1;
use 
CodeIgniter\RESTful\ResourceController;
use 
App\Models\EmployeeModel;

class 
EmployeeController extends ResourceController
{

    public function create_employee()
    {
        $rules = [ 
            'employee_name'    => 'required|max_length[100]',
            'employee_email'    => 'required|is_unique[employees.employee_email]|min_length[5]|max_length[100]'
        ];
        
        
if (!$this->validate($rules))
        {
            $response = [
                'status'    => 500,
                'message'  => $this->validator->getErrors(),
                'error'    => true,
                'data'      => []
            ];
            
        
} else {
            $file $this->request->getFile('employee_profile_image');
            
            
if (!empty($file)){
                // if employee_profile_image exists, create a new file name and upload it.
                $employee_profile_image_name $file->getName();
                $old_employee_profile_image_name explode('.'$employee_profile_image_name);
                $new_employee_profile_image_name round(microtime(true)).'.'.end($old_employee_profile_image_name);
                if ($file->move('user_public/images'$new_employee_profile_image_name)){
                    // need to add error handling
                    $employee_profile_image '/user_public/images/'.$new_employee_profile_image_name;
                }else{
                    // failed to upload
                    $response = [
                        'status'    => 500,
                        'message'  => 'Failed to Upload Image',
                        'error'    => true,
                        'data'      => []
                    ];
                }
            } else {
                // if employee_profile_image doesnt exist, set a standard non image.
                $employee_profile_image =  '/user_public/images/noimage-760x460.png'
            }
        
            $employees_object 
= new EmployeeModel();
            $employee_data = [
                'employee_name' => $this->request->getVar('employee_name'),
                'employee_email' => $this->request->getVar('employee_email'),
                'employee_profile_image' => $employee_profile_image
            
];
            
            
if ($employees_object->insert($employee_data)){
                // success
                $response = [
                    'status'    => 200,
                    'message'  => 'Employee Created',
                    'error'    => false,
                    'data'      => []
                ];
            } else {
                // failed
                $response = [
                    'status'    => 500,
                    'message'  => 'Failed to Create Employee',
                    'error'    => true,
                    'data'      => []
                ];
            }  
        
}
        return $this->respondCreated($response);    
    
}



The error is;

Code:
<b>Fatal error</b>:  Cannot declare class App\Controllers\Api\v1\EmployeeController, because the name is already in use in <b>/var/www/example.com/api.example.com/app_dir/app/Controllers/Api/v1/EmployeeController.php</b> on line <b>0</b><br />
{
    "title": "ErrorException",
    "type": "ErrorException",
    "code": 500,
    "message": "Cannot declare class App\\Controllers\\Api\\v1\\EmployeeController, because the name is already in use",
    "file": "/var/www/example.com/api.example.com/app_dir/app/Controllers/Api/v1/EmployeeController.php",
    "line": 0,
    "trace": [
        {
            "function": "shutdownHandler",
            "class": "CodeIgniter\\Debug\\Exceptions",
            "type": "->"
        }
    ]
}

I am using POSTMAN.  There is no line 0.  How is it possible to debug this?  I have looked everywhere but I don't see a duplicate.

Also, here is my route;

PHP Code:
$routes->group('api', function($routes){

    $routes->post('create-employee''/Api/v1/EmployeeController::create_employee');

}); 
Reply


Messages In This Thread
Controller or its method is not found: \\App\\Controllers\\Home::index - by spreaderman - 02-27-2022, 05:07 AM



Theme © iAndrew 2016 - Forum software by © MyBB