php - ReflectionException Class Home does not exist Codeigniter 4?

I had installed the fresh Codeigniter4 with , and configured the basic steps

Step 1

{-code-2}

Step 2

.env file renamed and set the development mode

{-code-3}

Step 3

from public folder > moved index.php and .htaccess file to main folder

{-code-4}

whereas the index.php file

{-code-5}

Error Images are attached here.

enter image description here enter image description here

I am trying to install it in subfolder that is faculty_portal. Respected community please guide me where I am doing wrong. You guidance/help will be highly appreciated.

Answer

Answer

Answer

-------Question Updated

Answer

Answer

Answer

-------

Routes Code

namespace Config;
$routes = Services::routes();

if (file_exists(SYSTEMPATH . 'Config/Routes.php')) {
    require SYSTEMPATH . 'Config/Routes.php';
}

$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);


$routes->get('/', 'Home::index');
$routes->get('/test', 'Home::test');


if (file_exists(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php')) {
    require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php';
}

Here I want to access the /test route

with the url access of http://121.52.XXX.XXX/faculty_portal/public/test

Home Controller

<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index()
    {
        return view('home_view');
    }

    public function test(){
        echo "hello";exit;
    }

}

The index.php file in public folder

$minPhpVersion = '7.4'; // If you update this, don't forget to update `spark`.
if (version_compare(PHP_VERSION, $minPhpVersion, '<')) {
    $message = sprintf(
        'Your PHP version must be %s or higher to run CodeIgniter. Current version: %s',
        $minPhpVersion,
        PHP_VERSION
    );

    exit($message);
}

define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR);

chdir(FCPATH);
require FCPATH . '../app/Config/Paths.php';
$paths = new Config\Paths();
require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';

require_once SYSTEMPATH . 'Config/DotEnv.php';
(new CodeIgniter\Config\DotEnv(ROOTPATH))->load();
$app = Config\Services::codeigniter();
$app->initialize();
$context = is_cli() ? 'php-cli' : 'web';
$app->setContext($context);
$app->run();

Here is the error message looks like

enter image description here enter image description here

Answer

Solution:

For the [v4.2.1] You don't need to move index.php in main directory. Keep it in public folder.

And line 20 in index.php must be

require FCPATH . '../app/Config/Paths.php';

For App -> Config -> App.php

public $uriProtocol = 'REQUEST_URI';

For App -> Config -> Routes.php

$routes->setAutoRoute(false);

For App -> Config -> Feature.php

 public bool $autoRoutesImproved = true;

For .htaccess in main directory

RewriteEngine on
RewriteCond %{HTTP_HOST} ^sitename.com$
RewriteRule (.*) https://www.sitename.com/$1 [R=301,L] 

DirectoryIndex /public/index.php
RewriteCond $1 !^(index\.php|images|assets|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R]
RewriteRule ^(.*)$ ./public/index.php/$1 [L]

For the public folder .htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sitename.com$
RewriteRule (.*) https://www.sitename.com/$1 [R=301,L]
RewriteCond $1 !^(index\.php|images|assets|doc|data|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*[^/])$ /$1/ [L,R]
</IfModule>

Source