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?

图片转代码服务由CSDN问答提供 功能建议

抱歉,我说英语不好,请使用翻译。 如何在codeigniter中创建链接?

如果我想在之后使用固定链接怎么办?

每次更改永久链接时我都必须更改整个系统代码?< / p>

是否有一个库从这些参数生成链接并按动态配置?

例如,如果我想更改系统目录:

 <代码> HTTP://testing/webapp/index.php 
   
 
 

为: <前> http://production/index.php

我是否需要更改所有代码? 什么是最佳做法 这个?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doutu6616 2013-04-07 14:46

    This is precisely what the Base URL is for in the config file. The only thing you need to worry about after that are your relative URL's, you can even set up different environments by making new folders inside of the config folder

    for example if I set in my config.php directly inside of the config folder

    $config['base_url'] = 'http://localhost/hello';
    

    Then the base url is always http:// localhost/hello now let's say I have my home development environment and a production environment. If I copy the config.php to a folder inside config called production so application/config/production/config.php set the base URL to:

    $config['base_url'] = 'http://www.example.com/hello';
    

    Then in my index.php I set:

    define('ENVIRONMENT', 'production');
    

    Everywhere I need the environment to change Codeigniter will change it for me. So on the live site I use the production environment, at home I change that environment to development and CI does the work for me.

    Then in your code use the base_url() to make your links.

    For example:

     <a href="<?=base_url();?>hello_world">Hello World</a>
    

    Will produce:

    //at home:
    http://localhost/hello_world
    //in production:
    http://www.example.com/hello_world
    

    Make sense?

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题