Welcome Guest, Not a member yet? Register   Sign In
Why controller under admin subdirectory not found?
#1

(This post was last modified: 10-01-2023, 11:36 PM by ozornick. Edit Reason: Format font )

In codeigniter  4.3.7  I created new controller file
under subdirectory app/Controllers/admin/Category.php with defined :



PHP Code:
    <?php

    
namespace App\Controllers\admin// Not sure - is it correct ?

    class Category extends BaseController
    
{
        public function index()
        {
            echo '<pre>::Category::'.print_r(-1,true).'</pre>';
            return view('admin/categories/index', []);
        

But urls


Code:
http://localhost/CIProducts/admin/categories/index

not opens  my controller, as I expected and returns 404 error

while url



Code:
    http://localhost/CIProducts

returns default homepage

I added in file Routes.php line with "admin/" url:





PHP Code:
    <?php

    
use CodeIgniter\Router\RouteCollection;

    /**
    * @var RouteCollection $routes
    */
    $routes->get('/''Home::index'); // This link works ok

    $routes->get('products-by-category''ProductsByCategory::index');  // This link works ok

    $routes->get('admin/category/index''Category::index');  // This link DOES NOT works with 404 error

    // $routes->get('admin/category/index', 'admin\Category::index'); // uncommented THAT RETURNS 404 

How to reference controller in file app/Controllers/admin/Category.php with "admin/" in url?


Thanks in advance!
Reply
#2

Try App\Controllers\admin\Category::index
And use "Admin" naming for directory
Reply
#3

(This post was last modified: 10-02-2023, 01:07 AM by kenjis.)

Read https://codeigniter4.github.io/CodeIgnit...-namespace
and you can check your routes with `spark routes` command:
https://codeigniter4.github.io/CodeIgnit...ing-routes
Reply




Theme © iAndrew 2016 - Forum software by © MyBB