代码之家  ›  专栏  ›  技术社区  ›  oiram16

(codeigner)我可以使用带参数的默认控制器吗?

  •  0
  • oiram16  · 技术社区  · 6 年前

    你好,朋友们,我正在和CodeIgniter合作,但我需要你们的帮助。

    我使用的默认控制器如下:

    $route['default_controller'] = 'generals/view/index';
    

    但是当我进入本地主机时,我看到404错误。

    我给我的控制器看下面。

    public function view($page){
            $this->load->view('templates/header');
            $this->load->view('sections/'.$page);
            $this->load->view('templates/footer');
        }
    

    我将非常感谢你的帮助

    1 回复  |  直到 6 年前
        1
  •  0
  •   vasilenicusor    6 年前

    根据 Codeigniter Documentation , 默认\控制器 是保留的路由:

    This route points to the action that should be executed if the URI contains no data,
    which will be the case when people load your root URL. The setting accepts a 
    controller/method value and index() would be the default method if you don’t specify 
    one. In the above example, it is Welcome::index() that would be called.
    

    请更新您的 默认\控制器 到:

    $route['default_controller'] = 'generals/view';
    

    在你的控制器里

    public function view($page = 'index'){
            $this->load->view('templates/header');
            $this->load->view('sections/'.$page);
            $this->load->view('templates/footer');
        }
    

    对于其余的URI,您需要定义另一个路由。