【问题标题】:How can I enforce a descriptive URL with CodeIgniter?如何使用 CodeIgniter 强制执行描述性 URL?
【发布时间】:2012-08-25 16:35:09
【问题描述】:

这个问题可能存在于其他地方,如果是这样,我很抱歉。在搜索了一个小时没有成功后,我不禁认为我走错了路。

基本上我正在寻找的是一种在页面 URL 中强制描述或标题的方法。我正在使用 CodeIgniter,因此将漂亮的 URL 放在任何我想要的地方都非常简单。

我可以:

http://mysite.com/controller/function/what-ever-i-want/can-go-here

它总是会去:

http://mysite.com/controller/function/

变量值what-ever-i-wantcan-go-here

如果只给出控制器/函数,我希望自动重写 URL 以包含标题。

所以,如果有人去:

http://mysite.com/controller/function/

它应该自动将 url 重写为

http://mysite.com/controller/function/a-more-descriptive-title/

我所说的功能的一个很好的例子是 SO URL。如果你去https://stackoverflow.com/questions/789439它会自动将它重写为https://stackoverflow.com/questions/789439/how-can-i-parse-descriptive-text-to-a-datetime-object

我怀疑涉及到 mod_rewrite,但我想提出最适合与 CodeIgniter 配合使用的解决方案。

我对漂亮的 url 场景非常陌生,并且迫切地寻求有更多经验的人的建议。提前感谢您提供的任何帮助!

【问题讨论】:

    标签: php codeigniter mod-rewrite friendly-url codeigniter-url


    【解决方案1】:

    我使用 Fiddler2 来了解 Stackoverflow 是如何做到这一点的。

    http://stackoverflow.com/questions/12205510/的部分回复

    HTTP/1.1 301 Moved Permanently
    Location: /questions/12205510/how-can-i-enforce-a-descriptive-url-with-codeigniter
    Vary: *
    Content-Length: 0
    

    所以基本上当我们转到controller/function/ 时,我们需要将用户重定向到controller/function/my-awesome-title。我已经编写了简单的控制器来做到这一点:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Controller extends CI_Controller
    {
        protected $_remap_names = array();
    
        function __construct()
        {
            parent::__construct();
    
            $this->_remap_names['func'] = "My Awesome Title";
        }
    
        function _remap($method, $arguments)
        {
            if(
                isset($this->_remap_names[$method])
                && sizeof($arguments) == 0
                && method_exists($this, $method)
                )
            {
    
                $this->load->helper("url");
    
                $title = str_replace(" ", "-", $this->_remap_names[$method]);
                $title = strtolower($title);
    
                $url   = strtolower(__CLASS__)."/$method/$title";
                $url   = site_url($url);
    
                // if you dont want to have index.php in url
                $url   = preg_replace("/index\.php\//", "", $url);
    
                header("HTTP/1.1 301 Moved Permanently");
                header("Location: $url");
                header("Vary: *");
            }
            else
            {
                call_user_func_array(array($this,$method), $arguments);
            }
        }
    
        function func()
        {
            echo "<h1>";
            echo $this->_remap_names[__FUNCTION__];
            echo "</h1>";
        }
    
    };
    

    CodeIgniters _remap 函数的文档可以在重新映射函数调用部分的 here 中找到。

    【讨论】:

    • 感谢您非常彻底的回复。我想我可以稍微修改一下以使用我的应用程序!
    【解决方案2】:

    我不使用 CodeIgniter,但您应该能够将代码放置在控制器的 __construct 中,或者如果 CI 有这些,则可以放置在某种预操作事件中。

    您只需查找正在查看的实体的相应 URL,并在必要时执行 301 重定向。

    【讨论】:

    • 哇,这实际上非常简单......如果没有给出额外的参数,我可以在 __construct() 中输出Header( "HTTP/1.1 301 Moved Permanently" );Header( "Location: http://www.mysite.com/controller/function/title-to-page" );。谢谢!
    猜你喜欢
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    相关资源
    最近更新 更多