Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyramid: Equivalent of MVC in PHP Frameworks in Pyramid / Python?

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.

like image 422
Phil Avatar asked Feb 17 '12 19:02

Phil


2 Answers

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.

like image 115
Jonathan Vanasco Avatar answered Nov 16 '22 04:11

Jonathan Vanasco


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.

like image 39
Antonio Beamud Avatar answered Nov 16 '22 02:11

Antonio Beamud