Shashikant
Shashikant

Reputation: 183

Redirection issue in Codeigniter4

I have done admin controller and put that in a sub folder named 'Admin'

Controller
  Admin
   -login.php

Now I want to fetch that by router file where I wrote this

$routes->get('admin', 'Admin/Login::index');

But it is showing me "Not found" error and redirects to "http://localhost/admin".

Could there be some .htaccess issue?

Upvotes: 1

Views: 611

Answers (2)

Welrico Dcosta
Welrico Dcosta

Reputation: 131

replace this

$routes->get('admin', 'Admin/Login::index');

with

$routes->get('admin', 'Admin\Login::index');

also make sure you add namespace in your login.php

namespace App\Controllers\Admin;

Upvotes: 1

Tsefo
Tsefo

Reputation: 409

If you keep CI4's directory structure intact you could in fact use sub-folders for Controllers, Models, Views, etc.

For example app/Controllers/Admin/Login.php is a valid place to put a Controller class. Make sure to add the appropriate namespace in Login.php - namespace App\Controllers\Admin; Also in routes - $routes->get('admin', 'App\Controllers\Admin\Login::index'); It is quite possible to work without the prefix of App\Controllers, but I never extensively tested it and I think there was a problem in some versions of CI4 before.

Another issue could be your app/Config/App.php class. If you did not change anything in your .htaccess file (the one in public directory!), $baseURL should be set to your public directory address - http://localhost/myproject/public/ . Or if you wish to make it easier - set up virtual hosts.

Just a thing to add - get() method in $routes allow only GET requests, meaning if you are trying to POST something (or use any other HTTP request method) it will fail and redirect.

Upvotes: 0

Related Questions