CodeIgniter 4:URL 中显示的两个控制器名称

CodeIgniter 4: Two controller name shown in URL

我是新手,我想知道为什么我的控制器在 URL 上出现两次。所以我有一个导航栏,当我单击一个菜单时,URL 看起来像 http://localhost/Home/index,但是当我单击另一个菜单时,URL 变为 http://localhost/Home/Home/stud_admit 并显示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/";
$routes->get('/', 'Home::index');
$routes->get('/', 'Home::stud_admit');

Home.php - 控制器

<?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);
    }
}

因为您打开了自动路由,只需从您的路由文件中删除:

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

你不需要它们。