【问题标题】:Codeigniter Query Strings URL IssueCodeigniter 查询字符串 URL 问题
【发布时间】:2019-05-15 17:01:40
【问题描述】:

我已经设置了具有如下 URL 的项目。

index.php?c=controllerName&m=methodName

上面网址中的参数如下所示。

index.php?c=controllerName&m=methodName&selDeg=manager

我想为我新创建的模块使用如下网址。

admin/Items/1

但想使用第一种类型的 url,因为它用于以前开发的模块。

是否可以将带有index.php 的url 用于旧模块而没有index.php 用于新模块。

【问题讨论】:

  • 不要认为可以做到。这是在您的 config.php 中全局配置到您的应用程序的,而不仅仅是它的某些部分。
  • @marcogmonteiro 是否可以使用 htaccess 来定义所有以前的 url 都可以使用 index.php 而新的没有 index.php
  • @Yogendrasinh 是的,可能但不是 100% 确定这一点。
  • @SherifSalah 我已经完成了我的配置文件并添加了以下行。 $config['allow_get_array'] = TRUE; $config['enable_query_strings'] = TRUE; $config['controller_trigger'] = 'c'; $config['function_trigger'] = 'm'; $config['directory_trigger'] = 'd'; 也将$config['uri_protocol']= 'AUTO'; 更改为其他值并且 PATH_INFO 生效但我仍然没有得到结果。该配置不允许同时使用两种类型的 url。它允许旧网址或新网址。

标签: php codeigniter query-string clean-urls


【解决方案1】:

你可以像这样准备你的配置:

$config['base_url'] = 'your_base_url';
$config['index_page'] = ''; // empty string
$config['enable_query_strings'] = FALSE; // keep it to default FALSE, you can use it even without enabling it here

和你的.htaccess 像这样:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

我想就是这样,现在您可以像这样同时使用segmentsquery_strings

http://localhost/codeigniter/?c=welcome&m=index
http://localhost/codeigniter/welcome/index
http://localhost/codeigniter/index.php/welcome/index
http://localhost/codeigniter/index.php?c=welcome&m=index
http://localhost/codeigniter/index.php/?c=welcome&m=index

我已经在我的环境中进行了测试,并且效果很好。


编辑:在我的 codeigniter 模板中,我扩展了我的核心路由器并稍微更改了 _set_routing() 方法,如下所示:

当然改核心系统是不好的做法,所以需要扩展核心路由器文件,在application/core下创建MY_Router.php,并使其扩展CI_Router如下:

class MY_Router extends CI_Router
{
protected function _set_routing()
{
    if (file_exists(APPPATH.'config/routes.php'))
    {
        include(APPPATH.'config/routes.php');
    }

    if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
    {
        include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
    }

    if (isset($route) && is_array($route))
    {
        isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
        isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
        unset($route['default_controller'], $route['translate_uri_dashes']);
        $this->routes = $route;
    }

    $_c = trim($this->config->item('controller_trigger'));
    if ( ! empty($_GET[$_c]))
    {
        $this->uri->filter_uri($_GET[$_c]);
        $this->set_class($_GET[$_c]);

        $_f = trim($this->config->item('function_trigger'));
        if ( ! empty($_GET[$_f]))
        {
            $this->uri->filter_uri($_GET[$_f]);
            $this->set_method($_GET[$_f]);
        }

        $this->uri->rsegments = array(
            1 => $this->class,
            2 => $this->method
        );
    }
    else
    {
        if ($this->uri->uri_string !== '')
        {
            $this->_parse_routes();
        }
        else
        {
            $this->_set_default_controller();
        }
    }
}
}

现在您拥有两全其美的优势,您将保持您的细分市场不受影响,但始终会寻找获取c=&m=,我希望它足够清楚。

我们将所有内容保持为默认值,并使其使用 cm 检查获取请求,这与 enable_query_strings 所做的非常相似,但没有启用它并弄乱了任何东西以继续处理分段。.

【讨论】:

  • 您能告诉我您使用的是哪个版本吗?我已经设置了新的 codeigniter 3.1.9 并做了你上面提到的更改,但我仍然无法在我的设置中使用这两个 URL。
  • 我也在使用 codeigniter 3.1.9,并且我已经添加了我使用过的所有 url,并且所有这些都可以正常工作开箱即用配置。
  • 让我检查我的 ci_template 中是否有额外的配置
  • 好的。使用此配置对我来说,它只运行像 codeigniter/welcome/index 这样的干净 URL,但是当我传递像 codeigniter/?c=welcome&m=index 这样的 URL 时,它总是会转到默认路由控制器。我已将默认路由更改为$route['default_controller'] = 'test';。所以当我用cm 传递url 时,它总是调用我的test 控制器的index 方法。
  • 看看编辑,现在它工作得很好但保持$config['enable_query_strings'] = FALSE;@Yogendrasinh
猜你喜欢
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多