douci1677 2016-01-12 15:15
浏览 22

在帮助器中使用模型

I've a quick question related to the software architecure. In my application I have a model which contains a method to check the environment the application works in. Let's say the model is called "AppModel".

So, the AppModel::isDevEnv() indicates whether the app is runnig in production or development. It's easy to call this method inside others models, components etc.

The problem is when I want to check the environement inside a view. I created a helper with a propriety method inside just to cover the method from the model and return the result coming from exactly model's method.

class AppModel {
    public function isDevEnv() {
        return boolean;
    }
}

class AppHelper {
    public static function isDevEnv() {
        $app = new AppModel();
        return $app->isDevEnv();
    }
}

Is it correct approach? Maybe it's a little bit overcomplicated? Maybe I should just make a static method inside a model and call it whenever I would like to call it?

  • 写回答

1条回答 默认 最新

  • dongliantong3229 2016-01-12 15:36
    关注

    If this is a legacy system I would recommend to refactor it to the desirable solution. If you want to have this helper or it is a required step for further refactoring then do it.

    In general I would inject services which behave differently based on the environment instead of checking the environment inside your models. But it might not be easy with legacy system.

    评论

报告相同问题?