如何在codeigniter中创建链接? [英] How to create links in codeigniter?

查看:191
本文介绍了如何在codeigniter中创建链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起我不会说好英语,使用翻译。
如何在codeigniter中创建链接?



如果我想使用固定链接,该怎么办?



每次更改固定链接时,我都必须更改整个系统代码。



有没有库从这些参数制造链接,并且动态配置?



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

  http://testing/webapp/index.php 

到:

  http://production/index.php 

我需要更改所有代码吗?
这是最好的做法?

解决方案

您可以使用codeingiter的方法base_url()和site_url()。



首先,您需要在控制器中加载网址助手:

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

请参阅下面的定义(根据CI文档, http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html






site_url()




返回您的配置文件中指定的网站网址。 index.php
文件(或者您在config
文件中设置为您的网站index_page的任何内容)都将添加到URL中,将任何URI段传递给
函数,以及您的配置文件中设置的url_suffix。



您可以随时使用此函数生成
a本地网址,在您的
网址更改的情况下可移植。



段可以选择作为字符串或
数组传递到函数。这里是一个字符串示例:



echo site_url(news / local / 123);上面的例子将返回
,例如:

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

这是一个作为数组传递的段的示例:

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

echo site_url($ segments);







base_url()




返回您的配置文件中指定的网站基址。示例:

  echo base_url(); 

此函数返回与site_url相同的内容,不附加
index_page或url_suffix。



也像site_url一样,您可以以字符串或数组形式提供细分。
这里是一个字符串示例:

  echo base_url(blog / post / 123);上面的例子将返回如下:


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

这很有用,因为与site_url()不同,您可以向
文件(例如图像或样式表)提供字符串。例如:

  echo base_url(/ _ user_guide_src_ci / images / icons / edit.png); 

这会给你一些东西:

  http://example.com/images/icons/edit.png 



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?

解决方案

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

这篇关于如何在codeigniter中创建链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆