gpt4 book ai didi

codeigniter - CodeIgniter路由404错误

转载 作者:行者123 更新时间:2023-12-02 04:03:54 26 4
gpt4 key购买 nike

跟着CI News Tutorial

我只做新闻部分,所以我将默认 Controller 更改为“新闻”

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

现在,“查看文章” anchor 生成了404错误。将默认值更改为:
 $route['default_controller'] = 'welcome';

创建正确的路径。我应该如何更改路由器以使用新闻?

没有自定义配置,或使用.htaccess。

从config.php中:
$config['base_url'] = 'http://frameworks:8888/ci_site_tut/';

$config['index_page'] = 'index.php';

新闻负责人:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
*
*/
class News extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}

public function index()
{
$data['news'] = $this->news_model->get_news();

$data['title'] = 'News Archive';

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


}


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');
}

public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');

$data['title'] = 'Create A News Item';

$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');

if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
} else {


$this->news_model->set_news();
$this->load->view('news/success');

}



}

}

解决方案:

自动加载网址帮助器:
$autoload['helper'] = array('url');

更新路线:
$route['default_controller'] = 'news';
$route['404_override'] = 'errors/page_missing';
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';

在views / news / index.php中更新了URL:
<p><a href="<?php echo site_url('news/' . $news_item['slug']); ?>">View Article</a></p>  

最佳答案

看来您没有在view方法中的任何地方设置$ new_item ['slug']。在加载 View 之前,请在view方法中执行'print_r($data['news_item']),以查看是否实际设置了子弹。

如果您确定自己是,请确保url帮助程序已自动加载,然后尝试在 View 中将其用作您的url。

<a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View article</a>

关于codeigniter - CodeIgniter路由404错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8696513/

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