【问题标题】:link url in code and in address bar is different than expected代码和地址栏中的链接 url 与预期不同
【发布时间】:2016-09-01 19:05:57
【问题描述】:

我是 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,那么它工作正常。 这是怎么回事?

【问题讨论】:

  • 尝试 $config['base_url'] = 'http://localhost/yourprojectname/'; 在配置基 URL 中使用 http 并确保文件名和类名的首字母大写。
  • 您使用的是哪个版本的 Codeigniter?
  • 链接中你要的网址是什么?

标签: php codeigniter


【解决方案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() 函数将包含由您的index_page 配置设置定义的“index.php”。 base_url() 函数将包含“index.php”)

见:codeigniter.com/userguide3/helpers/url_helper.html#site_url

【讨论】:

  • 嘿,谢谢,我在其他地方也遇到了不同的问题,并且在两个地方都运行良好
【解决方案2】:

从 base_url 配置中删除 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>";

【讨论】:

  • 使用你的方法后;地址更改为 http://127.0.0.1/codeigniter/index.php/edit/23 再次无法正常工作
  • 如果 OP 使用的是 codeigniter >= 3.0.0 的版本,那么这是非常糟糕的建议。根据documentation“确保您的‘base_url’配置值不为空”。或者,在基本站点 URL 上的 config.php cmets 中:“警告:您必须设置此值!”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 2015-12-22
  • 2013-03-20
  • 2022-10-15
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
相关资源
最近更新 更多