Controller needs to be a class?

Asked

Viewed 113 times

3

The controllers within an MVC, must they be classes? Because from what I’m seeing here, their only function is to take the information that the view You’re asking, play for model "solve the problem" and when problem solved, it takes the solution ready and sends to view again... it’s really necessary for him to be a class?

  • This will depend on who made the Framework, nothing prevents it from being done otherwise, but the standard set by everyone I’ve ever touched is a class. If you program and make your own you can establish your own way, that is, it depends even on who does it, it is not a rule that is how all programs currently.

  • But, sincerity I ask because such questioning, you think it is a mistake because?

  • 1

    MVC is unrelated to object orientation. The problem is that HTTP MVC got "weird", I asked myself this question: https://answall.com/q/60830/3635, because Controllers seem more "coherent" in applications that are signal-triggered (regardless of OOP), as a desktop application, a button will make a list populated through a Model (abstracted and related data).

  • Lucas, just for the record, I know a php framework that uses normal functions to make the controllers, it’s old and kind of "poorly done", but it’s an example of how OOP has no connection with this. Now speaking of what might be interesting about using OOP with Controller, it would be organizing, simply organizing, based on verbs (post, get, put), or paths, or anything like that.

  • Virgilio, I don’t think it’s an error, I just wondered, because for example, a class, is technically a file bigger than an ordinary PHP file, depending, so it would be easier, for certain situations, to create without class. For example, I need a controller just to get user data from the database, so I’ll need to create the entire MVC process to get it. But I think it’s worth it in case the system gets bigger

  • 3

    @Lucascarvalho personal experience (ok, it’s only a little over 15 years of dealing with PHP, maybe it’s not much) - PHP without MVC and OOP is much simpler and easier to maintain. After all, it is not a programming language, but a mere collection of scripts short-lived throughout the implementation of the application. I see no point in wanting to imitate complex structures that make sense in monolithic things (when they do) as executable. Treating PHP as PHP makes life easier. A good example of this is traditional "routes": almost never needed in a well thought out application.

  • Bacco, because I’m a PHP lover, I’m offended to "hear" this from a 15-year-old programmer. PHP is a programming language SIM and if well used, can do fantastic things, as we can see for example Magento, Wordpress, and many others. I am now at this very moment creating an EAD platform, and applying MVC, as the answers I had here, already gave an idea, and I’m sure it will be very well functional, will be easy to maintain because of OOP and MVC, and will still be fast! PHP has long since ceased to be just a scripting language!

  • 4

    A collection of well-made scripts is fantastic yes. And I use PHP in my day-to-day life. I don’t need to "embellish" PHP and flip the gold for it. I wouldn’t be offended if you told me that my screwdriver is just a screwdriver. After all, that’s what it was created for. I will not use my SMD blower to take out a screw. I see no reason for offense in putting each thing in its place. But when people don’t understand what I say about scripting languages, I often ask "what is the main loop of your PHP program?" , to lighten the difference a little. Being script is not detrimental :)

  • That said, anyway, the intention was not to offend. I feel that it was seen this way. I hope you can reread and see that there’s nothing wrong with what was said.

  • All right, no fuss! But for me, as I said, PHP after version 5, it’s not just a scripting language!

  • The fact that I say offended, was because I am graduating a professional PHP, and I think all languages should be valued! And I think yes, that you can create fantastic things with PHP together with OOP, MVC, etc... For example, I wouldn’t know how to organize in PHP without OOP, it would get messy, and difficult to maintain.

  • 3

    @Lucascarvalho not related to the specific subject, but it is worth remembering that you already have points to participate in the network chat if you want. In particular, the "stack burst" room https://chat.stackexchange.com/rooms/11910/ which is where Sopt’s people usually stay (during the exodus there is usually movement there).

  • Thank you Bacco :D

  • 1

    @Lucascarvalho the word lover is key there. Lover is the one who has love. Love has the one who is amateur. Professional deepens, understand all the necessary concepts, makes relationships, learns from the most experienced and bases all your decisions. Of course you can have whatever opinion you want, there is nothing wrong with that. But opinions without foundation are yours alone. It’s your right, but it doesn’t change the fact.If you love a person is they’re a bad person, no one has the right to say they can’t love them, but people can say they’re bad, the fact is they’re bad. Sure, it would be nice to have criticism...

  • 2

    ...foundation. And what @Bacco is saying is grounded. There is a wide range of information, even here on the site that proves everything he said. PHP can do fantastic things, although I disagree with its examples as fantastic (all experienced professionals - qualitatively and not quantitatively - that I know do not consider them fantastic, are useful things, but the product in general very poorly built). Your opinions have been published, nice. But other than what I’ve already said, they don’t hold up. Your training will be complete when you understand everything I’m talking about...

  • 2

    When you have all the grounds to support what you say or admit it’s just an opinion, a taste. And taste is not discussed.No one is saying what you should or shouldn’t do, even less than what you should or shouldn’t like. We are helping you better understand something that may be useful to you. If you don’t feel comfortable ignoring it, most people do it. Most people who program in the area are not professionals, they never graduate completely, but it is always a right of each one. The market has plenty of space for people who prefer not to be professionals, until ...

  • 3

    because everyone starts without being professional. Just one last thing: I’ve never met a beginner programmer who knew how to do OOP right. Very few experienced people do OOP right. OOP is very difficult. What Bacco said is much easier. OOP has its advantages, but only if you do it right, in the right place. I talk about it on the site all the time, but few read. Of course, if you take an OOP cake recipe and make it the same, it might be fine, but the slightest change you make to that can already fall apart, and that’s what happens. And this is not programming. Working is not the same as being right.

Show 12 more comments

1 answer

3


As far as I know, technically it doesn’t have to be, but the way everyone implements it is through a class.

If you do it another way, it might even be better, you might be innovating in a way, going against the tide in a positive way. But you can do everything wrong by not having experience with it and by not having anything that has already been tested and validated by other people to "copy". It could be a long shot... and it could make up for :)

But if you use class for everything else and on controller No, then I think it’s weird.

Your understanding of the pattern is correct, implementing it correctly is more important.

If you’re going to innovate in controller maybe you don’t need MVC. In fact there is a current already questioning its use, and I’m not talking about one or the other outsider like me, in fact there are already more suitable ways for many scenarios.

  • 3

    Can someone justify the -1 in his answer please?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.