Php 如果CodeIgniter方法没有';不存在。

Php 如果CodeIgniter方法没有';不存在。,php,codeigniter,class,redirect,methods,Php,Codeigniter,Class,Redirect,Methods,我正在使用CodeignIter,并且正在寻找一种方法,以便在调用的方法不存在时为单个控制器编写自定义处理例程 假设您拨打www.website.com/components/login 在组件控制器中,没有名为登录的方法,因此它不会发送404错误,而是默认为另一个名为默认的方法。是的,有一个解决方案。如果您有组件控制器和flilenameComponents.php。写下面的代码 <?php if (!defined('BASEPATH')) exit('No direct s

我正在使用CodeignIter,并且正在寻找一种方法,以便在调用的方法不存在时为单个控制器编写自定义处理例程

假设您拨打
www.website.com/components/login


组件
控制器中,没有名为
登录
的方法,因此它不会发送404错误,而是默认为另一个名为
默认
的方法。是的,有一个解决方案。如果您有
组件
控制器和flilename
Components.php
。写下面的代码

<?php

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

class Components extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    public function _remap($method, $params = array())
    {
        if (method_exists(__CLASS__, $method)) {
            $this->$method($params);
        } else {
            $this->test_default();
        }
    }

    // this method is exists
    public function test_method()
    {
        echo "Yes, I am exists.";
    }

    // this method is exists
    public function test_another($param1 = '', $param2 = '')
    {
        echo "Yes, I am with " . $param1 . " " . $param2;
    }

    // not exists - when you call /compontents/login
    public function test_default()
    {
        echo "Oh!!!, NO i am not exists.";
    }

}