CodeIgniter:在控制器中查找方法,如果没有,则查找默认控制器


CodeIgniter: Look for a method in a controller, and if not, look on the default controller

我有一个相同的应用程序(在CodeIgniter中制作(在同一台服务器上为三个不同的客户端运行。

该应用程序基本相同,但每个客户端都有自己的修改(和自己的子域(,但现在任何时候我必须进行一般修改时,我都必须浏览所有三个客户端的应用程序并执行修改。

我想要的是:

该应用程序只部署过一次,如果你去,可以进行某种控制

client1.example.com/method1

该应用程序在控制器client1.php中查找方法1,如果找到方法1,则加载该方法,否则,在控制器default.php 中查找方法

这样,我就可以为从同一源加载的所有客户端提供通用方法,然后为每个客户端提供具有特定方法的特定控制器。

附言:方法基本相同,但客户的不同需求要求能够对每种方法进行大的修改。

如有任何帮助,我们将不胜感激。


编辑:

好的,让我试着说清楚一点。

在wiredesignz Modular Extensions-HMVC中,当您调用index.php/welcome时,它会在默认控制器中查找它。当你加载一个模块并调用index.php/welcome时,它会先在模块目录中查找它,如果找不到,就会默认为默认的控制器。

如果没有模块功能,我如何实现这一点?

好吧。。。如果我解释一下你的问题。

假设我有这样的URL(主页是我的默认控制器(

http://stackoverflow.com/home/method_1 # Exist in my controller 
http://stackoverflow.com/home/method_2 # Exist in my controller 
http://stackoverflow.com/home/method_3 # NOT in my controller  <--------- 
http://stackoverflow.com/home/method_4 # Exist in my controller 

在以上链接中,第三个URL不存在。

关于你的问题,我有一个问题如果没有URL,用户如何访问链接??。除非你定义了url,否则没有人知道它。用户不会在浏览器中键入url。好的,我们继续吧。


所以,我得到的更好的解决方案是,如果页面不存在,我们称其为404覆盖。因此,您可以在routes.php 中使用Codeigniter 404_override

http://stackoverflow.com/home/method_1 # Valid URL
http://stackoverflow.com/home/method_2 # Valid URL
http://stackoverflow.com/home/method_3 # 404 ERROR <---------
http://stackoverflow.com/home/method_4 # Valid URL

application/config/routes.php

$route['404_override'] = ''; # set this to your home controller
class defaultcontroller
{
    protected function method1()
    {
        //default.php
    }
}

class client1 extends defaultcontroller
{
    public function method1()
    {
        //client1.php
        //if this method existed,it will be executed
        //or the same method in defaultcontroller will be excuted
    }
}

您可以在client2.php和client3.php中执行相同的操作