php – 如果CodeIgniter方法不存在,则重定向到默认方法.

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如果CodeIgniter方法不存在,则重定向到默认方法.脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用CodeignITer,我正在寻找一种方法,当一个调用方法不存在时,为单个控制器编写自定义处理例程.

让我们说你打电话给www.website.COM/components/login

在组件控制器中,没有一个名为LOGin的方法,因此它不会发送404错误,而是认使用另一个名为default的方法.

解决方法

是的,有一个解决方案.如果您有Components控制器和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.";
    }

}

由于认为PHP保留,因此您无法使用它,因此您可以编写自己的方法,例如test_default.这将自动检查您的类中是否存在方法并相应地重定向.它还支持参数.这项工作非常适合我.你可以测试自己.谢谢!!

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如果CodeIgniter方法不存在,则重定向到默认方法.全部内容,希望文章能够帮你解决php – 如果CodeIgniter方法不存在,则重定向到默认方法.所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。