Codeigniter第4级路线无法正常工作

时间:2016-01-17 20:23:49

标签: php codeigniter

routes.php文件


    $route['admin/news'] = 'admin_news/index'; //working
    $route['admin/news/(:any)'] = 'admin_news/view/$1'; //working
    $route['admin/news/create'] = 'admin_news/create'; //working
    $route['admin/news/edit/(:any)'] = 'admin_news/edit/$1'; //not-working
    $route['admin/news/delete/(:any)'] = 'admin_news/delete/$1'; //not-working

控制器:admin_news.php


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

    class Admin_news extends CI_Controller {

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

                    if(!$this->session->userdata('is_logged_in')){
                redirect('admin/login');
            }
            }

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

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

    public function view($slug = NULL)
    {
            $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('admin/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('admin/includes/header', $data);
            $this->load->view('admin/news/create', $data);
            $this->load->view('admin/includes/footer');

        }
        else
        {
            $this->news_model->set_news();
            $this->load->helper('url');
            $this->index();
        }
    }



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

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

        $data['title'] = 'Edit: '.$data['news_item']['title'];

        $this->load->helper('form');
        $this->load->library('form_validation');

        $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('admin/includes/header', $data);  
            $this->load->view('admin/news/edit', $data);
            $this->load->view('admin/includes/footer');
        }
        else
        {
            $this->news_model->update_news( $this->input->post('id'),
                                            $this->input->post('title'),
                                            $this->input->post('text'));


            $data['news_item'] = $this->news_model->get_news($slug);
            $this->load->view('admin/includes/header', $data);                                    
            $this->load->view('admin/news/success');
            $this->load->view('admin/news/edit', $data);
            $this->load->view('admin/includes/footer');   
        }
    }


    public function delete($id = NULL) {
            $this->news_model->delete_news($id);
            $this->load->helper('url');
            $this->index();
        }

    }

模特:News_model.php


    class News_model extends CI_Model {

      public function __construct()
      {
        $this->load->database();
      }

      public function get_news($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();
      }

        public function set_news()
      {
        $this->load->helper('url');

        $slug = url_title($this->input->post('title'), 'dash', TRUE);

        $data = array(
        'title' => $this->input->post('title'),
        'slug' => $slug,
        'text' => $this->input->post('text')
        );

        return $this->db->insert('news', $data);
      }

     /*public function update_news($slug = FALSE) 
    {
        $this->load->helper('url');

        $slug = url_title($this->input->post('title'),'dash',TRUE);

        $data = array(
            'slug'  =>  $slug,
            'title' =>  $this->input->post('title'),
            'text'  =>  $this->input->post('text')
        );
        $this->db->where('slug', $slug);
        return $this->db->update('news', $data);
    }*/


    public function update_news($id, $title, $text) {
        $data = array(
            'title' => $title,
            'text' => $text
        );

        $this->db->where('id', $id);
        $this->db->update('news', $data);
    }

      public function delete_news($id = FALSE) 
        {
            $this->db->delete('news', array('id' => $id));
        }
    }

观看次数:admin / news / edit.php


    <h2>Edit a news item</h2>
    <?php echo validation_errors(); ?>

    <?php echo form_open('news/edit/'.$news_item['slug']) ?>

    

Title <?php echo form_input('title',$news_item['title']); ?>

Text <?php echo form_textarea('text',$news_item['text']); ?>

<?php echo form_hidden('slug',$news_item['slug']); ?> <?php echo form_hidden('id',$news_item['id']); ?>

<?php echo form_submit('submit', 'Save Changes'); ?>

<?php echo form_close(); ?>
当我访问
`http://localhost/ciadmin/admin/news/edit/news-slug`
时显示 404 Page Not Found 消息!!

3 个答案:

答案 0 :(得分:5)

这样说:

$route['admin/news/delete/(:any)'] = 'admin_news/delete/$1';
$route['admin/news/edit/(:any)'] = 'admin_news/edit/$1';
$route['admin/news/create'] = 'admin_news/create';
$route['admin/news/(:any)'] = 'admin_news/view/$1';
$route['admin/news'] = 'admin_news/index';

记住:

  

路线将按照定义的顺序运行。较高的路线总是优先于较低的路线。

Docs

答案 1 :(得分:1)

我在4.0.4上也遇到了一些问题,对此我感到困惑。
在刚刚定义的ci4中

myController extends BaseController

然后在routes.php中

$routes->get('/', 'myController::index');

查看更多https://codeigniter4.github.io/userguide/incoming/routing.html

答案 2 :(得分:0)

假设每个请求都重定向到$route['admin/news/(:any)']。 示例:https://example.com/admin/news/edit/1。您必须按以下方式更改路线顺序 $route['admin/news/delete/(:any)'] = 'admin_news/delete/$1'; $route['admin/news/edit/(:any)'] = 'admin_news/edit/$1'; $route['admin/news/create'] = 'admin_news/create'; $route['admin/news/(:any)'] = 'admin_news/view/$1'; $route['admin/news'] = 'admin_news/index';