0

I am learning Symfony2, and part of the documentation on controllers states that the methods on the controller object are actually the controllers, whereas the object is more of a controller container.

Which part specifically is referred to as the controller(s)? I'm new to MVC and OOP, so I'm just trying to make sure I have it right.

Sam Selikoff
  • 12,366
  • 13
  • 58
  • 104
  • If you are learn to OOP, then why the hell are you messing with frameworks ? – tereško Jan 28 '13 at 06:43
  • I felt I grasped enough of the high-level concepts that I thought going through Symfony's tutorial would be a good way to make the concepts more concrete. I am open to suggestions, though - what do you think I should do? – Sam Selikoff Jan 28 '13 at 17:56
  • You should start instead by research OOP. Learn what dependency injection, SOLID principles, SoC and LoD are. You might be able to learn how to write a class from frameworks, but classes do not make OOP. You can write object oriented code in assembler or write purely procedural code in ruby. Just because you know the constructs, does not meant that you understand the paradigm. You might find some useful lectures/books listed [here](http://stackoverflow.com/a/9855170/727208), but without knowing you actual level of skill it would be impossible to suggest anything more precise. – tereško Jan 28 '13 at 19:38
  • Thanks for the references, I will definitely check them out. – Sam Selikoff Jan 28 '13 at 20:24

2 Answers2

1

In the example page you shared, the "class HelloController" is the Controller and its' functioned are Controller "Methods".

Okay, they are referring to the default method, indexAction() as the Controller.

In this MVC architecture (and most others, if not all) the "index" method is the default method (function) called when that controller is requested.

Alberto Ponte
  • 479
  • 3
  • 7
1

the page describes actually a convention endorsed by Symfony2 creators.

in some MVC frameworks (esp. in Java) controllers are implemented by one-class-per-controller convention, e.g.:

class ListContactsController {
    public function start() {
      // query db...
      return ...;
    }
}

class AddContactController {
    public function start($name, $details) {
      // insert into db...
      return ...;
    }
}

note that every controller-class has one method start() that defines what the controller actually does

in other MVC frameworks (like Symfony2 or cake-php) controllers are implemented by one-method-per-controller convention, grouped together for convenience, e.g.:

class ContactsController {
    public function list() {
      // query db...
      return ...;
    }

    public function add($name, $details) {
      // insert into db...
      return ...;
    }
}

here the convention assumes every controller is implemented as a method rather than a separate class with a particular method like start()

EDIT: another way of thinking about this difference is this:

  • one-class-per-controller assumes there is one controller instance (might hold internal state) and when user interacts with the view, view is communicating with that controller instance via callbacks i.e. methods in controller's class.
  • one-method-per-controller assumes any state is contained within parameters that are passed to methods, and when user interacts with the view, view is communicating with separate controllers/actions. those controllers are seen as independent concepts.
mantrid
  • 2,830
  • 21
  • 18
  • Very helpful, thanks! Two questions. First, in one-class-per-controller, why not just use functions? Second, are these the only two options? In rails, for instance, it seems like the controller actually refers to the class, which has several actions. – Sam Selikoff Jan 28 '13 at 02:02
  • @Sam in case a controller is more complex, manages a state, implements a multi-step workflow or a wizard it's better to separate it from other code by enclosing it within its own class. I haven't run into other conventions; rails follows the second convention (actions == controllers) – mantrid Jan 28 '13 at 02:31