【问题标题】:filter a string to remove disallowed characters to compose an URL in CodeIgniter过滤字符串以删除不允许的字符以在 CodeIgniter 中构成 URL
【发布时间】:2010-10-15 00:53:25
【问题描述】:

我正在尝试为我的投资组合中的博客创建 URL 友好的链接。 所以我想获得类似 site/journal/post/{title}

的链接

显然 Journal 是我的控制者,但假设我的标题是“mysite.com 上线!”我想要一个有效的 url,比如 site/journal/post/mysitecom-goes-live,其中所有不允许的字符都被删除。

我将如何转换“mysite.com 上线!”根据 $config['permitted_uri_chars'] 中的字符到 CodeIgniter 中的 'site/journal/post/mysitecom-goes-live'

【问题讨论】:

    标签: codeigniter codeigniter-url


    【解决方案1】:

    使用网址助手

    $this->load->helper('url');
    
    $blog_slug = url_title('Mysite.com Goes live!');
    
    echo $blog_slug //mysitecom-site-goes-live 
    // might differ slightly, but it'll do what you want.
    

    生成对 url 友好的链接。

    将此值存储在博客表 (url_title/url_slug) 的字段中。

    创建一个函数:

    class Journal extends controller
    {
       //make your index/constructor etc
    
       function view($post)
       {
         $this->blog_model->get_post($post);
         // etc - your model returns the correct post,
         // then process that data and pass it to your view
       }
    }
    

    您的 blog_model 有一个使用 CI 的方法 get_post

    $this->db->where('url_title', $post);

    希望这是有道理的。

    那么当你访问页面时:

    site.com/journal/view/mysite-goes-live

    该函数将获取“mysite-goes-live”并将其传递给 view() 函数,该函数进而在数据库中查找相应的博客条目。

    【讨论】:

    • 非常感谢,没有必要如此彻底地解释一切(尽管我非常感谢您的努力)
    • 很好的答案,很全面
    • 我仍然收到“您提交的 URI 包含不允许的字符。”即使我使用 url_title() 也会出错,这意味着它不会删除配置中的 allowed_uri_chars 中指定的所有特殊字符。我能做什么??
    猜你喜欢
    • 2012-06-26
    • 1970-01-01
    • 2011-05-09
    • 2019-07-16
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    相关资源
    最近更新 更多