سؤال

ما هي مكافئات الهرم / Python للنموذج - العرض - المتحكم في أطر عمل PHP مثل Kohana؟

Genacodicetagpre

أحاول فهم منطق الهرم.كإضافة إلى الإجابة ، سيكون موضع تقدير أي مساعدة أو وثائق وما إلى ذلك.

شكرًا

هل كانت مفيدة؟

المحلول

كانت Pylons ، أحد الإطارين اللذين تم ضمهما معًا ليكونا هرم (الآخر كان repoze.bfg) "قريبًا" من نظام MVC.

أقترب من الاقتباسات ، لأنه على مدار السنوات القليلة الماضية ، كان الكثير من الأشخاص يتشاجرون حول ما تعنيه MVC ... وبدأت العديد من المشاريع التي كانت تروج نفسها ذات مرة باسم "MVC" تسميها "MTC" (نموذج نموذج تحكم) "MT" (نموذج نموذجي) أو "MV" (عرض نموذج). يتفق الجميع على ماهية "النموذج" ، ولكن بالضبط ما تعينه "طريقة العرض" و "وحدة التحكم" - في إطار عمل معين - يمكن أن يكون نقطة خلاف.

يتمتع كل من الهرم والأبراج بوظيفة "المرسل" لإعداد التعيين للطلب. تحت أبراج في config / route.py ؛ يختلف الأمر قليلاً تحت Pyramid - السقالات الافتراضية لها التوجيه في app / init .py ، لكن يمكنك تقسيمها إلى app / route.py أو استخدام config.include () لدفعها إلى "معالجات" أو config.scan () لسحبها من "طرق العرض".

"المعالجات" في الهرم يتم توفيرها بواسطة pyramid_handlers ، وهي في الحقيقة مجرد "عروض" مع مجموعة من عناصر التوليد التلقائي هناك. إذا كنت ترغب في ذلك ، يمكن أن تستخدم تطبيقاتك كلاً من المعالجات وطريقة العرض (أنا أفعل).

على أي حال ، بناءً على كيفية تفسيرك لـ MVC / MTC / وما إلى ذلك ، هذا جدول فضفاض لما قد ترغب فيه: Genacodicetagpre

ملاحظة سريعة - أنا أحدد ما ورد أعلاه ليس بناءً على تفسيري أو ما هو تعريف MVC "الرسمي" ... إنه يستند إلى كيفية تسويق الأطر الشائعة الأخرى لأنفسها.

نصائح أخرى

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.

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.

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)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top