• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Autorouting is not working for Controller created inside sub-directory of Controllers

#1
Sad 
I have created a controller in a sub-directory under app/Controllers/Admin but unable to access it via autorouting methods like localhost/admin . The code for that is given below:
 
PHP Code:
<?php namespace App\Controllers\Admin;

use 
App\Controllers\BaseController;

class 
Admin extends BaseController
{
public function 
index()
{
return 
view('welcome_message');
}

//-------------------------------------------------------------------- 
}

I think I have set up everything correctly from the docs. I have installed the ci4 using composer and adjusted the .env for development, added virtual host too. However, only custom route is working and also when I specify the whole directory for the controller like localhost/admin/admin.

Code:
/**
* --------------------------------------------------------------------
* Router Setup
* --------------------------------------------------------------------
*/
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);

Please Help if I made any silly mistakes or is this a bug?

The error I am getting is : 404, the controller cannot be found
Reply

#2
You also need to pass along the sub-directory

localhost/admin/admin
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
(07-24-2020, 10:07 AM)InsiteFX Wrote: You also need to pass along the sub-directory

localhost/admin/admin

But I have expanded the BaseController and also specified it's required namespace. It doesn't make sense to pass the sub-directory in the URL to get the controller. I should be able to gain the controller directly with localhost/admin,
Reply

#4
Your sub-controller needs to be named Home. As the auto route grabs your default controller name.

App\Controllers\Admin\Home

https://codeigniter.com/user_guide/incom...irectories
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.