【问题标题】:Codeigniter, Routes. Keep the URLSCodeigniter,路线。保留网址
【发布时间】:2020-08-13 04:59:02
【问题描述】:

我的网站太旧,无法重新创建。 没有来源,但所有的链接看起来都像

http://example.com/about.html

http://example.com/contact.html

http://example.com/countryname.html

http://example.com/countryname/cityname.html

店主说:“这是 Joomla”, 但我想知道为什么链接如此简单, 就像它是单独的“.html”文件的数量一样。

我根本不使用 Joomla,我更喜欢 Codeigniter。 但是有什么方法可以保持 URL 不变?

例如,在我看来

<a href="<?php echo base_url('/welcome/countrieslist/germany');?>" class="menu__link">Germany</a>

这给了 http://example.com/welcome/countrieslist/germany

我需要 http://example.com/germany.html

感谢您的帮助。

【问题讨论】:

    标签: php codeigniter routes url-routing


    【解决方案1】:

    给url添加一个标识符,比如:

    http://example.com/germany-c.html
    

    基于 c 标识符,您可以在知道该 URL 代表什么的同时路由任何 URL。在这种情况下,c 代表国家/地区。

    然后在config/routes.php中添加路由:

    $route['(:any)-c.html'] = 'welcome/countrieslist/(:any)';
    

    【讨论】:

    • @Vadym K. 你提出的方式是硬编码的。对于许多 URL 或其他人来说,这是不可行的。在您的路由中添加 .html 以使用后缀。
    【解决方案2】:

    谢谢你,Donboo。

    一段时间后我认为最简单的方法是

    $route['what-I-would-like-to-have-in-url'] = 'mycontroller/mymethod';

    然后我需要重新定义 base_url 函数,以便将 .html 添加到所有 url。

    奇怪,但是

    $config['url_suffix'] = '.html';

    不添加任何东西。

    【讨论】:

      【解决方案3】:

      如果您已经从框架 url CodeIgniter removing index.php from url 中删除了 /index.php,最简单的方法是为所有 Url 添加前缀

      `$config['url_suffix'] = '.html'; //Config.php`
      

      //Routes.php

      `$route['about'] = 'Controller/function';// example`
      
      `$route['germany'] = 'welcome/countrieslist/germany';`
      

      然后将其附加到视图中的链接

      `<a href="<?php echo base_url('germany');?>" class="menu__link">Germany</a>`
      

      路由同样可以通过许多参数进行扩展。随便看看主题下的框架官方手册

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-08
        • 1970-01-01
        • 2014-12-20
        • 1970-01-01
        • 2010-12-11
        相关资源
        最近更新 更多