duanliu8998 2013-04-07 14:36
浏览 45
已采纳

如何在codeigniter中创建链接?

sorry I do not speak good English, use translator. How can I create links in codeigniter?

What if I want to use permalinks after?

I would have to change the whole system code every time you make a change in permalinks?

Is there a library that manufactures the links from these arguments and behave as configured dynamically?

For example if I want to change the system directory from:

http://testing/webapp/index.php

to:

http://production/index.php

Do I need to change all the code? What is the best practice for this?

  • 写回答

2条回答 默认 最新

  • drde3456 2013-04-07 14:44
    关注

    You can use codeingiter's methods base_url() and site_url().

    At first, you need to load the URL helper in your controller:

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

    See their definitions below (According to CI documentation, http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html)


    site_url()

    Returns your site URL, as specified in your config file. The index.php file (or whatever you have set as your site index_page in your config file) will be added to the URL, as will any URI segments you pass to the function, and the url_suffix as set in your config file.

    You are encouraged to use this function any time you need to generate a local URL so that your pages become more portable in the event your URL changes.

    Segments can be optionally passed to the function as a string or an array. Here is a string example:

    echo site_url("news/local/123"); The above example would return something like:

    http://example.com/index.php/news/local/123
    

    Here is an example of segments passed as an array:

    $segments = array('news', 'local', '123');
    
    echo site_url($segments);
    

    base_url()

    Returns your site base URL, as specified in your config file. Example:

    echo base_url();
    

    This function returns the same thing as site_url, without the index_page or url_suffix being appended.

    Also like site_url, you can supply segments as a string or an array. Here is a string example:

    echo base_url("blog/post/123");
    

    The above example would return something like:

    http://example.com/blog/post/123
    

    This is useful because unlike site_url(), you can supply a string to a file, such as an image or stylesheet. For example:

    echo base_url("/_user_guide_src_ci/images/icons/edit.png");
    

    This would give you something like:

    http://example.com/images/icons/edit.png
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!