Home > front end >  CodeIgniter 4: Two controller name shown in URL
CodeIgniter 4: Two controller name shown in URL

Time:03-11

I'm new to this, and I was wondering why my controller appears twice on my URL. So I have a navbar, and when I click a menu, the URL looks like http://localhost/Home/index, but when I click another menu, the URL changes to http://localhost/Home/Home/stud_admit and shows the 404 - File Not Found, Controller method is not found: Home.

Routes.php

$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);

$route['(:any)'] = "Home/$1";
$routes->get('/', 'Home::index');
$routes->get('/', 'Home::stud_admit');

Home.php - Controller

<?php
namespace App\Controllers;
use AppControllersBaseController;

class Home extends BaseController
{

  public function index() {
    $data = [];
    $data['title']   = 'SMS';
    $data['heading']    = 'Welcome to SMS';
    $data['main_content']   = 'home';   // page name
    echo view('innerpages/template', $data);
  }

    public function stud_admit()
    {
      $data = [];
      $data['title']        = 'SMS | ADMIT STUDENT';
      $data['heading']  = 'Welcome to SMS';
      $data['main_content'] = 'stud-admit'; // page name
      echo view('innerpages/template', $data);
    }
}

CodePudding user response:

Because you have auto routing turned on, simply remove from your routes file:

$route['(:any)'] = "Home/$1"; $routes->get('/', 'Home::index'); $routes->get('/', 'Home::stud_admit');

You don't need them.

  • Related