dongyulan6251 2019-02-27 15:19
浏览 149

Codeigniter 3路由和处理404错误

I am trying to configure pretty URLs in codeigniter and it's not making much sense to me. I'd like my URLs to follow the following structure;

example.com/admin/view/form/123

I can successfully view data when I visit the URL above. I see the same data when I visit;

example.com/admin/view/123

Notice the 3rd segment /form/ is missing, but still returns the data. It's almost like it's ignoring this - I thought CI should throw a 404 error, or do I need to check for this manually? If so, how?

When I visit this URL;

example.com/admin/view

I see the following error;

An uncaught Exception was encountered
Type: ArgumentCountError
Message: Too few arguments to function Admin::view(), 0 passed 

My code can be seen below;

Controller

defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends CI_Controller

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

    public function index()
        {
        $this->load->view('template/header');
        $data = array(
            'title' => 'Admin Page'
        );
        $this->load->view('admin/index', $data);
        $this->load->view('template/footer');
        }

    public function view($form_submission_id)
        {
        $this->load->view('template/header');
        $data = array(
            'title' => 'Form View Page',
            'record' => $this->Admin_model->getSubmissionById($form_submission_id)
        );
        $this->load->view('admin/view/index', $data);
        $this->load->view('template/footer');
        }
    }

Model

class Admin_model extends CI_Model {

    public function getSubmissionById($form_submission_id) {
        $this->db->select('*');
        $this->db->from('submissions');
        $this->db->where('id', $form_submission_id); 
        $query = $this->db->get();
        return $query->row(); 
    }
}

Routes // redirect any urls that contain a number after /form $route['admin/view/form/(:num)'] = "admin/view/$1";

My views folder structure looks like this;

 - views
 -- admin
     - index.php
     - view
        -- index.php

Ideally I want my URLs to look like this, and throw 404 errors where they don't.

example.com/admin/view/form/123

example.com/admin/update/form/123

example.com/admin/delete/form/123

  • 写回答

1条回答 默认 最新

  • dongshicuo4844 2019-02-27 16:02
    关注

    You can do this by several ways.

    The fastest way is by creating routes like this:

    $route['admin/view/form/(:num)']   = "Admin/view/$1"; 
    

    When typing exemple.com/admin/view/form/9 , it will redirect to the controller admin, function view and the "9" will be the parameter $form_submission_id

    The same thing for routes below.

    $route['admin/update/form/(:num)'] = "Admin/update/$1";
    $route['admin/delete/form/(:num)'] = "Admin/delete/$1";
    

    of course you could create a function for each one, view, update, delete ... etc

    It's normal that you see error when you enter example.com/admin/view because the view function take 1 parameter $form_submission_id

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?