Controller call in controller?

How effective are the methods, is it recommended? I have a comment controller + model that needs to be called in the element and profile controller. The comment controller automatically loads the comment model.

Is it permissible to call the comment controller directly from the elements and the profile controller, or is this the "best way" to invoke the comment model?

I ask because in kohana a view is not a single class, so if I had to call a controller in another controller, I get two views. On the other hand, if I just called the model, there would be a duplicate code in the element and profile controllers.

All your MVC experts help! =)

+3
source share
4 answers

Generally, I would choose the Bold Model approach.

I'm not sure which code you really care about duplication.

There are several ways to do this:

First way: - Interview your comment model to return comments. - Pass the comment data into your presentation. - Change the comments in the view, possibly using some kind of view helper

The second way: - Understand that there is no reason why your opinion cannot speak directly about your model. - Write a view assistant that captures the data he needs directly from the model and displays it.

I prefer the second way. Some people have a problem allowing their presentation layer to talk to the model (in read-only mode!), But I'm not one of them.

+3

, , .

DTO/, , , - .

, , , . , Controller.

, , , ( ). , html . - .

To connect to the original html, it follows that if you change the presentation for comments, the display of comments in the profile module will inevitably change. it may be what you want, but I would go for flexibility.

Now, if you need data, you will need to extract data from the markup returned by your view, which is heavy and productive.

+1
source

If you are trying to call a static method, you can just call the function directly. For example:

class Controller_User extends Controller {
    public static function format_user($user){
        return array("data" => $user->getData());
    }
}

class Controller_Foo extends Controller {
    public function action_index(){
        $user = get_user_data_some_how();
        $user_data = Controller_User::format_user($user)
    }
}
0
source

Source: https://habr.com/ru/post/1718237/


All Articles