当前位置: 首页 > 知识库问答 >
问题:

Codeigniter 4级路由不工作

许华清
2023-03-14

路线。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));
        }
    }

视图:管理员/新闻/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']); ?>

文字

`http://localhost/ciadmin/admin/news/edit/news-slug`

共有3个答案

戚澄邈
2023-03-14

因为每个请求都重定向到$route['admin/news/(:any)]。例子:https://example.com/admin/news/edit/1.您必须更改路由顺序,如下所示$route['admin/news/delete/(:any)]='admin\u news/delete/$1' $route['admin/news/edit/(:any)]='admin\u news/edit/$1' $route['admin/news/create']='admin\u news/create' $route['admin/news/(:any)]='admin\u news/view/$1' $route['admin/news']='admin\u news/index'

狄新立
2023-03-14

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

myController extends BaseController

然后在路线上。php

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

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

孙熠彤
2023-03-14

这样说:

$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';

记得:

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

医生。

 类似资料:
  • 我的目标是让http://mydomain/route1呈现React组件Component1,让http://mydomain/route2呈现component2。因此,我认为编写如下路线是很自然的: http://mydomain/route1按预期工作,但http://mydomain/route2反而呈现Component1。 然后我读了这个问题,并将路线改为:

  • 在其核心,linkerd 的主要工作是路由:接受请求(HTTP,Thrift,Mux或其他协议),并将该请求发送到正确的目标。本指南将详细解释 linkerd 如何确定请求应该发送到哪里。这个过程由4个步骤组成:identification/识别,binding/绑定,resolution/解析和load balancing/负载均衡。 Linkerd 路由 识别 Identification/识

  • 我已经稍微调整了React路由器的私有路由示例,以便与Redux玩得很好,但是当链接或重定向到其他页面时,不会呈现任何组件。这个例子可以在这里找到: https://reacttraining.com/react-router/web/example/auth-workflow 他们的PrivateRoute组件如下所示: 但是,因为我已经把它合并到一个Redux应用程序中,我不得不稍微调整一下私

  • 我一直在致力于将我们的应用程序从React.js 0.13升级到15.4.2以及与之相关的依赖项。我可能很接近,但经过4天50个小时后,我终于到了终点,我最后的希望。 在升级过程中,react路由器被升级到4.0.0版,每个文档、教程和堆栈溢出问题都显示了如何编写路由器(所有这些都必须是正确的),但是,当我这样做时,它总是失败。要么说历史是必需的属性,要么当我通过示例添加它时,它仍然显示为未定义和

  • 我是拉威尔的新手,刚刚开始构建我的第一个应用程序。我使用的是Laravel5.2。5.我正在跟踪laracast视频以实现身份验证。当我第一次启动应用程序时,我可以访问/auth/register、/auth/login和/auth/logout。注册和登录工作正常。Register在我的用户表中创建一个条目,/auth/login允许我使用该用户登录。成功登录后,我尝试注销。当我尝试访问/aut