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

问题描述 投票:27回答:6

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

有这个功能还是只是通常的

干杯,

php codeigniter anchor
6个回答
65
投票

我假设你的意思是“内部”在你的申请中。

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

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

或者您可以使用URL帮助程序生成<a>标记

anchor(uri segments, text, attributes)

所以...使用它......

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

这将产生

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

对于其他评论问题

我会用我的第一个例子

所以...

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

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


1
投票

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

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

1
投票

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

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

或使用CI的内置锚函数。


0
投票

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

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

有关详细信息,请参阅https://www.codeigniter.com/user_guide/helpers/url_helper.html

这肯定会奏效。


0
投票

你也可以使用这段代码

// test“class =”btn btn-primary pull-right“>


-1
投票
<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>
© www.soinside.com 2019 - 2024. All rights reserved.