skip to Main Content

Routes and Controller problem on Codeigniter 4, can’t access

I created Controller and Routes, and I created a Navbar.php file in Controller with methods and GET in Routes, then can not be accessed with localhost CI. I need a solution because I’m just learning CI4, thank you.

This is the code.

Routes setting

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/', 'Home::index');
$routes->get('/about', 'Navbar::about');
$routes->get('/contact', 'Navbar::contact');
$routes->get('/faqs', 'Navbar::faqs');

Navbar.php

<?php namespace AppControllers;

class Page extends BaseController
{
    public function about()
    {
        echo "about page";
    }
    
    public function contact()
    {
        echo "contact page";
    }
    
    public function faqs()
    {
        echo "faqs page";
    }

}

I want to know where’s error, need to problem solving with help.

2

Answers


  1. As your filename is navbar.php, class name must be Navbar with first capital letter.

    navbar.php

    <?php namespace AppControllers;
    
    class Navbar extends BaseController
    {
       public function about()
       {
           echo "about page";
       }
    
       public function contact()
       {
          echo "contact page";
       }
    
       public function faqs()
       {
          echo "faqs page";
       }
    
    }
    
    Login or Signup to reply.
  2. You should be change your class name in Navbar.php file where your controller name is Navbar.php but your class name is Page.

    thanks

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search