codeigniterurlcodeigniter-4url-helper

codeigniter 4 how to make menu bar links


I am a beginner at CodeIgniter. I want to know how to link one page to another. I tried to like this. I got the error 404 - File Not Found Sorry! I cannot seem to find the page you were looking for.

What I tried so far I attached below. view page:-

<li><a href="<?php echo site_url('aboutus') ?>">Aboutus</a></li>
<li><a href="<?php echo site_url('contactus') ?>">Contactus</a></li>

Controller Page

public function index()
    {
        return view('index');
    }

    public function aboutus()
    {
        return view('about');
    }

    public function contactus()
    {
        return view('contact');
    }

when the page is loaded, called in the index page index, I made the links about I had a problem

What is wrong here.


Solution

  • Your code is missing a very important thing, the name of your controller.

    Lets say your have a controller called site and all those functions are in it. In that case your code should look like this:

    class site extends CI_Controller{
    
        public function index()
        {
            return view('index');
        }
    
        public function aboutus()
        {
            return view('about');
        }
    
        public function contactus()
        {
            return view('contact');
        }
    
    }
    

    Then all your links should look like this:

    <li><a href="<?php echo site_url('site/aboutus') ?>">Aboutus</a></li>
    <li><a href="<?php echo site_url('site/contactus') ?>">Contactus</a></li>
    

    In case you want to remove controller name from your urls your need to look into the routing features codeigniter offers.