1

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.

KUMAR
  • 1,993
  • 2
  • 9
  • 26
hari dran
  • 115
  • 3
  • 16

3 Answers3

1

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.

marcogmonteiro
  • 2,061
  • 1
  • 17
  • 26
  • 1
    $routes->get('/', 'Home::index'); $routes->add('aboutus', 'App\Controllers\Home::aboutus'); $routes->add('contactus', 'App\Controllers\Home::contactus'); what is wrong – hari dran Nov 02 '20 at 09:46
  • @haridran That really depends, I don't know if OP is using ci3 or 4. Since there's no info about it. I left that out because of that. – marcogmonteiro Nov 02 '20 at 11:47