2

I have controllers in subfolders

--Controllers
----Admin
--------UserController
--------AccountController
----User
--------UserController
--------AccountController

I write routes for it

$routes->group('user', function ($routes){
    $routes->get('dashboard', 'UserDashboard::index');
    $routes->get('changePassword', 'User\AccountController::changePassword');
});

It give me 404 even though I have method and have something output as well

Controller code

<?php namespace App\Controllers;

use App\Controllers\BaseController;

class AccountController extends BaseController
{
    public function index()
    {
        echo "Hello";
        exit();
        return view('user/account/changePassword');
    }

    public function changePassword()
    {
        echo "Change Password View";
        exit();
        return view('user/account/changePassword');
    }
}

enter image description here

  • I am curious as to why you have "public" in your URL. Can you explain why that is there? – TimBrownlaw Dec 30 '20 at 02:27
  • I agree with @TimBrownlaw public shouldn't be in the url. Actually it would be best to not even work with localhost and work with a proper local domain like http://foo.local so you can better mimic the real environment. – marcogmonteiro Dec 30 '20 at 12:13
  • It is solved because of using wrong namespace. @TimBrownlaw the public is because the index.php file is in the that folder it is not in the root folder – Syed Suhaib Zia Dec 31 '20 at 12:32
  • @marcogmonteiro thank you for the advice i followed the same you told and it is good now and very easy – Syed Suhaib Zia Dec 31 '20 at 12:33

1 Answers1

3

You need to change the namespace of your controllers.

User controllers

<?php namespace App\Controllers\User;

use App\Controllers\BaseController;

class AccountController extends BaseController
{
    
}

Admin controllers

<?php namespace App\Controllers\Admin;

use App\Controllers\BaseController;

class AccountController extends BaseController
{
    
}
marcogmonteiro
  • 2,061
  • 1
  • 17
  • 26