代码和地址栏中的链接URL与预期的不同

时间:2016-09-01 19:05:58

标签: php codeigniter

我是codeigniter的新手,我遇到了这个问题, config / config.php中的base_url是    $config['base_url'] = 'localhost/codeigniter/index.php/stud/';

我在view / Stud_view.php中使用它

echo "<td><a href = '".base_url()."/edit/".$r->roll_no."'>Edit</a></td>";

但在地址栏中显示为http://localhost/codeigniter/index.php/index.php/stud/edit/21

这里index.php出现了两次。 如果我从地址栏中删除index.php然后它正常工作。 这是怎么回事?

2 个答案:

答案 0 :(得分:1)

您的base_url应该绝对,包含协议,并且不包括index.php

$config['base_url'] = 'http://localhost/codeigniter/

您的网址可以构建为:

<?php 
    $url = site_url("stud/edit/" . $r->roll_no);
    echo "<td><a href='" . $url . "'>Edit</a></td>";
?>

您的最终网址看起来像......

http://localhost/codeigniter/index.php/stud/edit/{roll_no}

site_url()函数将包含&#34; index.php&#34;由您的index_page配置设置定义。 base_url()函数包括&#34; index.php&#34;)

请参阅:codeigniter.com/userguide3/helpers/url_helper.html#site_url

答案 1 :(得分:-1)

从base_url配置中删除网址。 改变:

$config['base_url'] = 'localhost/codeigniter/index.php/stud/';

为:

$config['base_url'] = '';

并将您的网址更改为:

echo "<td><a href = '".site_url()."/edit/".$r->roll_no."'>Edit</a></td>";