douxiajia6720 2017-02-22 17:45
浏览 63

从加载器调用控制器的各种方法

I am trying to build a loader to load various controller with their methods (as needed) within a controller. I sketched out a simple code on my home controller to call the LeftController (now a dummy controller but I intend to use this controller as menu).

require 'controller/LeftController.php';
$LeftController = new LeftController();
$LeftController->index();

This works within the HomeController. It loads the LeftController controller and displays the method index().

Basing my Loader on the above code this is what I have done till now

class Loader
{

    public function controller($controller)
    {
        $file = 'controller/' . $controller . '.php';
        $class = $controller;

        if (file_exists($file)) {
            require($file); // require 'controller/LeftController.php';
            $controller = new $class(); //$LeftController = new LeftController();

            var_dump($controller);
        }
    }
}

This works too and the controller is instantiated. I see result using the var_dump().

Now, I need to call the method, as we see at the top most code $LeftController->index(); but on the Loader class this time.

One way of doing this is if I add $controller->index() right after the $controller = new $class(); but this will always call the index() method of the controller.

How do I code this method part as such that I can call any method associated with the controller and not just the index().

  • 写回答

1条回答 默认 最新

  • douqianzha6213 2017-02-25 05:10
    关注

    You can pass a method argument with your controller:

    public function controller($controller, $method)
    

    and then call it on your newly created object:

    $controller->$method()
    

    However,

    it seems you are trying to reinvent the wheel. The part where you verify if a files exists, include it and instantiate the class, is called autoloading.

    The code could look like this:

    public function controller($controller, $method)
    {
        $instance = new $controller();
        return $instance->$method();
    }
    

    While the autoloading part makes use of spl_autoload_register() to manage finding and including files.

    The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined.

    So you can use the code you already have, and abstract it from the action of instantiating the class:

    spl_autoload_register(function autoloader($controller) {
        $file = 'controller/' . $controller . '.php';
        if (file_exists($file)) { require($file); }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等