gpt4 book ai didi

php - Codeigniter 网址,双段,如 : news/news/

转载 作者:行者123 更新时间:2023-12-02 06:20:56 25 4
gpt4 key购买 nike

我已经完成了 CI 文档中包含的众所周知的“新闻”教程。有时我的链接字符串中会出现双“新闻/”段,如下所示:“/codeig/news/news/entry”,有时在重新加载页面后一切正常。我应该提一下,我已经按照另一个流行的教程摆脱了“index.php”部分。我的代码有什么问题?

这是我的路线:

$route['news/create'] = 'news/create'; 
$route['news/update/(:any)'] = 'news/update/$1';
$route['news/delete/(:any)'] = 'news/delete/$1';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['welcome'] = 'welcome';

$route['auth/(:any)'] = 'auth/$1';
$route['auth'] = 'auth';
$route['activate/:num/:any'] = "/auth/activate/$1/$2";
$route['reset_password/:num/:any'] = "/auth/reset_password/$1/$2";

$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'news';

views/news/index.php 文件:

<?php foreach ($news as $news_item): ?>

<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p>

<?php endforeach ?>

查看方法(新闻 Controller )

public function view($slug)
{
$data['news_item'] = $this->news_model->get_news($slug);

if (empty($data['news_item']))
{
show_404();
}

$data['title'] = $data['news_item']['title'];

$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}

最佳答案

在 HTML 中,如果当前 URL 是这样的:

http://example.com/news/

你有这样的链接:

<a href="news/article-slug">Link</a>

点击链接后,您的 URL 将变为:

http://example.com/news/news/article-slug

如果你再次点击它,你会:

http://example.com/news/news/article-slug/news/article-slug

注意:这并不完全正确,路径的相对性取决于当前 URL 和/或您的网址中是否有尾部斜杠 /链接。

href="news/something" 是一个相对 URL,相对于当前页面。您想要使用绝对 URL 或根 URL:

<a href="http://example.com/mysite/news/article-slug">Link</a>
<a href="/news/article-slug">Link</a>

使用以下任何 Codeigniter 函数使绝对 URL 更容易:

  • anchor()(为您制作整个链接)
  • site_url()(返回您的绝对基准 URL)
  • base_url()(同上)
<?php echo anchor('news/'.$news_item['slug'], 'Link Text'); ?>
<a href="<?php echo base_url('news/'.$news_item['slug']); ?>">Link Text</a>

所以澄清一下,这与您的路由无关 - 这纯粹是 HTML 中相关链接的问题。

关于php - Codeigniter 网址,双段,如 : news/news/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9956643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com