【问题标题】:Using segments and and URI for Get variables using Codeigniter [closed]使用 Codeigniter 使用段和 URI 获取变量 [关闭]
【发布时间】:2017-05-11 12:51:24
【问题描述】:

好的,

所以我想做一些类似于 index.php/show?p=1 的事情,但是我知道默认情况下在 Codeigniter 中禁用使用 GET。所以我想知道如何使用 URI 和 Segments 在控制器中设置它,所以它看起来像 index.php/show/1。或者这不可能?

显然,“1”将根据数据库中的值进行更改。

我希望这是有道理的,如果没有,请告诉我。

【问题讨论】:

标签: php codeigniter uri codeigniter-3


【解决方案1】:
  1. $_GET 默认不禁用。
  2. 执行 URI 段操作就像添加单个路由一样简单,tutorial in the official documentation 涵盖了这一点。

【讨论】:

    【解决方案2】:

    要让 URL http://example.com/index.php/show/1 按照您的要求进行操作,您需要修改文件 application/config/routes.php 以完成任务。

    假设 show 是一个控制器,并且您希望控制器的 index() 方法处理上述 URL,您需要做几件事。

    定义接受参数的索引方法

    public function index($arg) 
    {
        ...
    

    将以下内容添加到 application/config/routes.php

    $route['show/(:any)'] = 'show/index/$1';
    

    如果您想使用index() 以外的其他方法来处理琐事 - 比如说doShow(),那么路线如下所示。

    $route['show/(:any)'] = 'show/doShow/$1';
    

    方法是

    public function doShow($arg) 
    { 
       ...
    

    无论使用什么方法 - index() 或 doShow() - 如果您没有在第二个 URI 段中传递参数,您将得到一个 404 - Page Not Found 屏幕。

    【讨论】:

      猜你喜欢
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 2012-09-12
      • 2017-12-21
      • 1970-01-01
      相关资源
      最近更新 更多