Codeigniter & PHP - forcing a 404?

后端 未结 8 1358
予麋鹿
予麋鹿 2020-12-08 10:19

In codeigniter, as you know, a page of the form: /class/function/ID, where class is the controller name, function is the method within the controller, and ID is

8条回答
  •  一整个雨季
    2020-12-08 10:58

    Create controller in your application/controllers folder.

    class Error extends Controller
    {
    function error_404()
    {
        $this->load->view('error');
    }
    }
    

    Then in your application/library extend the Router class by creating application/libraries/MY_Router.php

    class MY_Router extends CI_Router
    {
    private $error_controller = 'error';
    private $error_method_404 = 'error_404';
    
    function MY_Router()
    {
        parent::CI_Router();
    }
    
    // this is just the same method as in Router.php, with show_404() replaced by $this->error_404();
    function _validate_request($segments)
    {
        // Does the requested controller exist in the root folder?
        if(file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }
    
        // Is the controller in a sub-folder?
        if(is_dir(APPPATH.'controllers/'.$segments[0]))
        {
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);
            if(count($segments) > 0)
            {
                // Does the requested controller exist in the sub-folder?
                if(!file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    return $this->error_404();
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');
                // Does the default controller exist in the sub-folder?
                if(!file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }
            }
            return $segments;
        }
        // Can't find the requested controller...
        return $this->error_404();
    }
    
    function error_404()
    {
        $segments = array();
        $segments[] = $this->error_controller;
        $segments[] = $this->error_method_404;
        return $segments;
    }
    
    function fetch_class()
    {
        // if method doesn't exist in class, change
        // class to error and method to error_404
        $this->check_method();
        return $this->class;
    }
    
    function check_method()
    {
        $class = $this->class;
        if (class_exists($class))
        {
            if ($class == 'doc')
            {
                return;
            }
            if (! in_array('_remap', array_map('strtolower', get_class_methods($class)))
                && ! in_array(strtolower($this->method), array_map('strtolower', get_class_methods($class))))
            {
                $this->class = $this->error_controller;
                $this->method = $this->error_method_404;
                include(APPPATH.'controllers/'.$this->fetch_directory().$this->error_controller.EXT);
            }
        }
    }
    

    }

    If the page does not exist, it will be routed to error controller

提交回复
热议问题