php CodeIgniter - 在视图中链接到另一个页面的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5205155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:37:26  来源:igfitidea点击:

CodeIgniter - Correct way to link to another page in a view

phpcodeigniteranchor

提问by Cecil

I was wondering if someone could tell me the correct way to link to another page from within a view.

我想知道是否有人可以告诉我从视图中链接到另一个页面的正确方法。

Is there a function for this or is it just the usual about

是否有此功能或只是通常的

Cheers,

干杯,

回答by jondavidjohn

I assume you are meaning "internally" within your application.

我假设您在应用程序中的意思是“内部”。

you can create your own <a>tag and insert a url in the href like this

您可以创建自己的<a>标签并像这样在 href 中插入一个 url

<a href="<?php echo site_url('controller/function/uri') ?>">Link</a>

OR you can use the URL helper this way to generate an <a>tag

或者您可以通过这种方式使用 URL 助手来生成<a>标签

anchor(uri segments, text, attributes)

So... to use it...

所以……使用它……

<?php echo anchor('controller/function/uri', 'Link', 'class="link-class"') ?>

and that will generate

这将产生

<a href="http://domain.com/index.php/controller/function/uri" class="link-class">Link</a>

For the additional commented question

对于额外的评论问题

I would use my first example

我会用我的第一个例子

so...

所以...

<a href="<?php echo site_url('controller/function') ?>"><img src="<?php echo base_url() ?>img/path/file.jpg" /></a>

for images (and other assets) I wouldn't put the file path within the php, I would just echo the base_url() and then add the path normally.

对于图像(和其他资产),我不会将文件路径放在 php 中,我只会回显 base_url() 然后正常添加路径。

回答by Ashish Bafna

The best way is to use the following code:

最好的方法是使用以下代码:

<a href="<?php echo base_url() ?>directory_name/filename.php">Link</a>

回答by winnie damayo

you can also use PHP short tag to make it shorter. here's an example

你也可以使用 PHP 短标签来缩短它。这是一个例子

<a href="<?= site_url('controller/function'); ?>Contacts</a>

or use the built in anchor function of CI.

或使用 CI 的内置锚点功能。

回答by Ghulam Abbas

you can also use this code

您也可以使用此代码

//test" class="btn btn-primary pull-right">

//test" class="btn btn-primary pull-right">

回答by Amaan Iqbal

Best and easiest way is to use anchor tag in CodeIgniter like eg.

最好和最简单的方法是在 CodeIgniter 中使用锚标记,例如。

<?php 
    $this->load->helper('url'); 
    echo anchor('name_of_controller_file/function_name_if_any', 'Sign Out', array('class' => '', 'id' => '')); 
?>

Refer https://www.codeigniter.com/user_guide/helpers/url_helper.htmlfor details

请参阅https://www.codeigniter.com/user_guide/helpers/url_helper.html的细节

This will surely work.

这肯定会奏效。

回答by jareenamol joy

<a href="<?php echo site_url('controller/function'); ?>Compose</a>

<a href="<?php echo site_url('controller/function'); ?>Inbox</a>

<a href="<?php echo site_url('controller/function'); ?>Outbox</a>

<a href="<?php echo site_url('controller/function'); ?>logout</a>

<a href="<?php echo site_url('controller/function'); ?>logout</a>