本文介绍了带子弹的Codeigniter路由给我404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个客户网站上工作,该网站的一部分用于检索新闻,这是我的第一个codeigniter应用程序,并且我遵循了CI教程,代码和路由。

I'm working on a client website which part of it to retrieve news, this is my first codeigniter application and I have followed CI tutorials, slugs and routing.

我遇到的问题是,一切正常,但是当获得基于段塞的记录时,我得到了404。我所做的是从删除了index.php我的网址并对其进行了测试,效果很好。

The problem I'm having is that everything works fine but when it comes to get a record based on slug I get 404. What I did was removed index.php from my URL and tested it, which works fine.

这是我的route.php

$route['default_controller'] = "welcome";
$route['404_override'] = '';

$route['news/(:any)'] = 'news/singe_news/$1';
$route['news'] = 'news';

这是我的模型news_model.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News_model extends CI_Model {


    function get_all(){

    $sql="SELECT * FROM news AS news";
    $query=$this->db->query($sql);
    return $query->result();

    }

    //// get a single post

    function get_single($slug = FALSE) {

    if ($slug === FALSE)
        {
        $query = $this->db->get('news');
            return $query->result_array();
        }

    $query = $this->db->get_where('news', array('slug' => $slug));
    return $query->row_array();


    }


    function news_categories(){

     $sql="SELECT * FROM news_categories";
     $query=$this->db->query($sql);
     return $query->result();

    }

}

这是我的控制器news.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends CI_Controller {



    public function index()
    {
        $this->load->model('news_model');
        $data['results']=$this->news_model->get_all();
        $this->load->view('public/header', $data);
        $this->load->view('public/menu');
        $this->load->view('public/news-listing', $data);
        $this->load->view('public/footer', $data);
    }



    public function single_news($slug) {


        $this->load->model('news_model');
        $data['single_news'] = $this->news_model->get_single();

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

        $this->load->view('public/header');
        $this->load->view('public/menu');
        $this->load->view('public/single-news', $data);
        $this->load->view('public/footer');


    }

}

所以我有问题:

1- $ slug哪里来?我有一个视图,有

2-是否缺少任何内容?

1- Where does $slug come from? I have a view has

2- Is there anything missing?

URL必须为

domain.com/news/this-is-a-slug

domain.com/news/this-is-a-slug

如果在其他帖子上以不同的格式提出了不同的要求,请多谢。

Many thanks and apologies if this may have been asked in different format with different intention on other posts.

推荐答案

$ slug来自何处?

您的URI看起来像这样:www.example.com/controller/method/arg1/arg2/arg3(不含查询字符串)

Your URI looks like this: www.example.com/controller/method/arg1/arg2/arg3 (w/o query string)

嗯,您应该做的事情很少:

Well, there is few things you should do:


  1. 使用自动加载(config / autoload.php)来加载您最常用的模型,或者如果该模型未被广泛使用,则至少在类构造函数中加载它。

  1. Use autoload (config/autoload.php) to load your most used models, or if this model is not used widely at least load it at class constructor.

您没有将$ slug参数传递给模型方法,即使您要解决路由也无法使用。

You are not passing $slug argument to model method, this will not work even if you will fix your routing.

$ data ['single_news'] = $ this-> news_model-> get_single();

更好显示404(如果找不到),请不要在失败时重新获取所有数据。

Better to show 404 if you can't find a slug, don't refetch all data on fail.

404错误请按照以下步骤操作:

To fix 404 error follow those steps:


  1. 检查您的.htaccess文件是否存在错误

  1. Check your .htaccess file for errors

在配置中检查uri_protocol设置

Check uri_protocol setting in config

尝试像这样的路由$ route [’news /((.+)$)?'] = news / singe_news / $ 1; (这应替换两条新闻的路线)

Try routing like this $route['news/((.+)$)?'] = "news/singe_news/$1"; (this should replace both routes for news)

这篇关于带子弹的Codeigniter路由给我404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:46