【问题标题】:Customize dynamic URL.change ?id to name自定义动态 URL.change ?id 来命名
【发布时间】:2015-02-16 09:04:25
【问题描述】:

我正在使用 codeigniter 制作动态网站。在新闻页面中,我使用 url 方法,例如:http://test.com/test/test/news_details?id=27

如何更改我的网址以将新闻标题放入 ?id=27

示例:

http://test.com/test/test/news_details/pass-this-url

“pass-this-url”指的是 id=27。

最好的问候。

【问题讨论】:

    标签: codeigniter codeigniter-url codeigniter-routing


    【解决方案1】:

    使用路线:http://www.codeigniter.com/user_guide/general/routing.html

    在你的情况下,它会是这样的:

    $route['pass-this-url'] = "test/test/news_details?id=27";
    

    所以http://www.example.com/pass-this-url 将被codeigniter 理解为http://www.example.com/test/test/news_details?id=27

    然而,如果你想让它动态化,你将不得不调用你的数据库:

    require_once( BASEPATH .'database/DB'. EXT );
    $db =& DB();
    $query = $db->get( 'news' );
    $result = $query->result();
    foreach( $result as $row )
    {
        $route[$row->title] = "test/test/news_details?id=". $row->id;
        //We suppose here that your title is URL friendly.
    } 
    

    【讨论】:

    • tnx 为您解答,但是,当我使用动态方法并打印所有路线时,将不会添加任何路线。和静态路由不做任何事情并将我重定向到 404 页面:(
    • “打印所有路线”是什么意思?
    • 我使用'print_r($this -> router -> routes);'查看我添加了功能的路线。
    • 那么你应该检查查询是否返回任何结果。
    • 我的查询返回结果。但仍然路由不添加任何东西。
    【解决方案2】:

    转到 application/config/config.php 并检查:

    $config['enable_query_strings'] = FALSE; 
    

    【讨论】:

    • 我检查了这个,但没有改变。 :)
    猜你喜欢
    • 1970-01-01
    • 2015-08-20
    • 2012-10-23
    • 1970-01-01
    • 2011-09-28
    • 2022-12-03
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    相关资源
    最近更新 更多