【问题标题】:How to pass parameters to the index function of default controller in Codeigniter如何将参数传递给 Codeigniter 中默认控制器的索引函数
【发布时间】:2019-06-27 09:39:42
【问题描述】:

在我的 Codeigniter 项目中,我需要生成像 example.com/username 这样的 URL。这里的 'username' 是默认控制器 'Home' 的参数。

我正在使用 remap 将参数传递给默认控制器的索引函数。

 function _remap($param)
 {
    $this->index($param);
 }

我正在开发一个 CodeIgniter 项目。我有一个默认控制器作为“主页”。 我的网站 URL 是 example.com。我需要做的是将参数传递给默认控制器“Home”,这样我就可以显示如下配置文件:

example.com/username

但是我不能使用这个 URL。我可以将参数传递给默认控制器并使其工作的唯一方法是在 URL 中显示默认控制器。 比如 example.com/home/username。这不是我需要的。

我需要默认控制器不在 URL 中显示并生成像 example.com/username 这样的 URL。

【问题讨论】:

    标签: php codeigniter url parameters


    【解决方案1】:

    index() 在默认的Home 控制器中会像

     public function index($params = ''){
        echo $params;
     }
    

    在这里打电话给index()

     function _remap($param)
     {
        $this->index($param);
     }
    

    【讨论】:

    • 感谢您的尝试,但现在就是这样。
    • 那么,您面临的问题在哪里?
    • 当我这样做时,URL 变成了 example.com/home/username。当我删除主页(默认控制器)时,它会引发 404 错误。我需要 URL 为 example.com/username。
    • 你为什么不使用路由?
    【解决方案2】:

    在您的 config/config.php 文件中。定义你的 url。然后定义参数的路由。

     $config['base_url'] = 'example.com';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多