【问题标题】:How to redirect to an URL by a re-routed URL in Codeigniter?如何通过 Codeigniter 中的重新路由 URL 重定向到 URL?
【发布时间】:2017-07-25 23:34:49
【问题描述】:

我有以下路线:

$route['new-products'] = 'products/latest';

我想在我的控制器中的某个地方使用这样的东西:

//this will go to /new-products
redirect_route('products/latest');

【问题讨论】:

  • 你尝试过什么吗?
  • 是的。我试图扩展CI_Route 以通过routes.php 中定义的值获取路由的键,但我无法得到它。

标签: php codeigniter redirect routes


【解决方案1】:

随便用

redirect('products/latest');

redirect('new-products');

确保从 URL 中删除 index.php

【讨论】:

  • 这是可行的,因为它被路由到它的控制器和方法,但我不想通过 2 个不同的 URL 访问该页面。我想最终将 URL 设为 exwebsite/new-products
  • @Dumitru 如果有帮助,请Mark it as ACCEPT。因此VOTE UP will lead my TAG count
【解决方案2】:
<?php
    redirect(site_url('new-products'));
?>

【讨论】:

    【解决方案3】:

    将此规则放入您的.htaccess 文件中:

    RewriteRule    "^/products/latest"  "new-products"  [R]
    

    Docs.

    【讨论】:

      【解决方案4】:

      你可以用这个重定向:

      redirect(set_url('new-products'));
      

      【讨论】:

        【解决方案5】:

        您必须加载 url 帮助程序,将其添加到您的 autoload.php 中,或者您可以将其添加到您要使用它的实际 php 文件中。

        $this->load->helper('url');
        

        之后你可以通过重定向

        redirect('controller/action');
        

        这会重定向到带有您的扩展程序的 base_url。如果你想在没有 index.php 的情况下使用漂亮的 url,你必须清除 config.php 中的 index_page。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-03-23
          • 2018-01-12
          • 2015-09-14
          • 2020-04-27
          • 1970-01-01
          • 2014-04-01
          • 1970-01-01
          相关资源
          最近更新 更多