12

What are the Pyramid / Python equivalents of Model - View - Controller of PHP Frameworks such as Kohana?

In Pyramid "Model" is .... and it is used for .....
In Pyramid "View" is .... and it is used for .....
In Pyramid "Controller" is .... and it is used for .....

I am trying to understand Pyramid's logic. As an addition to the answer, any help, documentation etc would be appreciated.

Thanks.

Phil
  • 13,875
  • 21
  • 81
  • 126

4 Answers4

21

Pylons, one of the two frameworks that joined together to be Pyramid ( the other was repoze.bfg ) was "close" to an MVC system.

I put close in quotations, because over the past few years a lot of people have been fighting about what MVC means... and many projects that once touted themselves as "MVC" started to call them "MTC" (model template controller) "MT" (model template) or "MV" (model view). Everyone agrees on what the "model" is, but exactly what the "view" and "controller" map to - on a given framework - can be a point of contention.

Pyramid and pylons both have a "dispatcher" functionality to set up the mapping for a request. Under pylons its in config/routes.py ; under Pyramid it's a little different -- the default scaffolds have the routing in app/init.py , but you're free to break it out into app/routes.py or use config.include() to push it into you 'handlers' or config.scan() to pull it from your 'views'.

'handlers' in pyramid are provided by pyramid_handlers, and are really just 'views' with a bunch of auto-generation stuff in there. If you wanted to, your apps could use both handlers AND views ( mine do ).

In any event, depending on how you interpret MVC / MTC / etc , this is a loose table of what you might want:

           || mvt            | mvc            | mvc
==========================================================================
model      || sqlalchemy     | sqlalchemy     | sqlalchemy
view       || views/handlers | templates      | views/handlers + templates
controller ||                | views/handlers | dispatch/routing
template   || templates      |                |

Quick note- I'm defining the above not based on my interpretation or what the 'official' MVC definition is... It's based in relation to how other popular frameworks market themselves.

Jonathan Vanasco
  • 15,111
  • 10
  • 48
  • 72
11

If you want, with pyramid you can simulate the MVC pattern:

  • Model: For example using sqlalchemy (http://docs.sqlalchemy.org)
  • View: Using templates and view methods.
  • Controller: You can use the package pyramid_handlers, to create controllers and map actions defined in a route to actions in the controller, for example:
   Class HomeController(object):
     def __init__(self, request):
          self.request = request

      def form_proc(self):
          name = self.request.params['name']
          ... bla, bla, bla ...

In the config you can add something like:

    config.add_handler('home', '/home/{action}',
                       handler='mypackage.HomeController')

If you put this url in your form action -> http://SERVER_NAME/home/form_proc, you can process the form.

Pyramid give you all the flexibility if you need it.

Antonio Beamud
  • 2,281
  • 1
  • 15
  • 26
5

From the Pyramid Introduction:

You Say Pyramid is MVC, But Where’s The Controller?

The Pyramid authors believe that the MVC pattern just doesn’t really fit the web very well. In a Pyramid application, there is a resource tree, which represents the site structure, and views, which tend to present the data stored in the resource tree and a user-defined “domain model”. However, no facility provided by the framework actually necessarily maps to the concept of a “controller” or “model”. So if you had to give it some acronym, I guess you’d say Pyramid is actually an “RV” framework rather than an “MVC” framework. “MVC”, however, is close enough as a general classification moniker for purposes of comparison with other web frameworks.

Wilduck
  • 13,822
  • 10
  • 58
  • 90
  • Hello, I have read all this on their web page and even looked at examples but I still don't get it therefore seek explanation from another PHP developer who can put it in terms stated in my question, so that I can understand it. Thank you. – Phil Feb 17 '12 at 19:48
  • But the developers of Pyramid are telling you that there isn't a direct analogy. Why don't you trust them? – Wilduck Feb 17 '12 at 19:54
  • 1
    No no, I do trust them. Python people are really nice. I just would love to understand the logic behind it. In Kohana for example, it's simple. Define URIs, trigger a controller from the URI, tell controller to do stuff, for example, use a model-class to do DB calls, then include a template or two, mix the two create-render the response and send it back. I just want to know how this process is done with Pyramid. I really really like Python's syntax and want to get into it. Thank you. – Phil Feb 17 '12 at 20:00
  • Phil: I have never used Kohana, and yet what you've defined sounds exactly how Pyramid does it. Your question reads as asking someone to map the various parts of Pyramid into the "Model", "View" and "Controller" sections. Wilduck has shown documentation where the creators of Pyramid believe that there is no such mapping that can be accurately made. If you still are confused, perhaps you should rephrase your question to be more specific, as it seems the current question is really unanswerable. – Mark Hildreth Feb 17 '12 at 20:18
2

I have experience with CakePHP and now I'm starting with Pyramid and Python. There is not a direct mapping but it is not because pyramid does things in weird manner but because framework authors abused the MVC term.

In Cake, for example, there are some classes that they like to call 'Models' but most of the time are just ORM classes. The controllers are mostly used as namespaces for related methods called 'actions' that pass the data to the views, which are just the templates.

In pyramid terms, 'Resources' are the 'models', and you are free to use wherever you want here, if you want an ORM you can use SQLAlchemy for example or mongodb or wherever.

The framework itself functions as the 'controllers', and the actions are called 'views' this can be normal functions or classes, you are free to organize them wherever you like. This views may use a template and a renderer to construct the response that is send to the browser.

Hope it helps (please excuse my bad english)

Chopsticks
  • 126
  • 1
  • 3
  • Thank you very much for this kind answer. If you don't mind, I would love to ask you a few more questions. In PHP (in general), you unzip the files, put them in a folder and apache+php runs it. In Python, I am having difficulty understanding how this works. Is each folder considered an "app"? How can a web-server host multiple python web sites? And what is the use if WCGI? I want to get in to Python but it seems complicated regarding WCGI and all these command line tools. – Phil Feb 22 '12 at 00:02