• Building extensions for Pagekit Part #3 - Model, View, Controller

Building extensions for Pagekit Part #3 - Model, View, Controller

Potentially outdated

This post is older than 365 days and may be outdated. Please use the site-search to search for updated information.

Hello everybody,

this is it: The next part of my blog series Building extensions for Pagekit.

My last post was about the initial setup of our IDE - we also added a composer.json and the module definition (index.php) to our project.

Before we continue coding we have to clarify some basic stuff - so grab a cup of coffee and some chocolate as this post is going to be theoretical.

The MVC pattern

Nowadays writing modern web applications means to create a clean, well structured and readable code, which can be easily maintained.

To archive this, we can use the MVC pattern. MVC is shorthand for Model, View, Controller.

Huh? Never heard of this? That doesn't matter, I'll explain it in short words:

  • Model: The model provives all the data you want to work with. Typically the model binds a database and prepares them. In our extension we are going to have at least one model that is called "Survey". This model is going to provide information about the created surveys like title, creationdate, expirationdate and some more information.
  • View: The view is basically what you see, when you are using the extension. The view displays all the information and provides elements we need for our user interface - such as buttons or forms
  • Controller: As the name suggests, the controller takes control. It simply does things. If you are saving things, the controller takes care that the save-action is performed. If you want to open a view, the controller takes care to load data from the model to the view- we will see two types of controllers in our extension: Our basic Controller and the API Controller, which allows us to do API calls.

How does all this work together?

We are using the MVC pattern to create our extension. We are going to have one or more views, one or more models and one or more controllers.

This is how it could look like in our extension:

  • views
    • admin
      • settings.php
      • survey-index.php
      • survey-edit.php
  • src
    • Controller
      • SurveyController.php
      • SurveyApiController.php
    • Model
      • Survey.php
      • SurveyModelTrait.php

As you can see, we are going to create the folders views and src and create subfolders for our files. All the items are being separated so we exactly know, which classes (in this case they are going to have the same name as the filename) are existing and what they are doing. So the class SurveyController is, as it is obviously a controller, located in src/Controller/SurveyController.php.

The view where the user can see all surveys in the admin interface is located in views/admin/survey-index.php - if the user is going to edit a single survey, the matching view would be located in views/admin/survey-edit.php.

Of course you are free to choose different filenames. Just make sure your names are consistent and self-explaining.

Enough for today

As I promised - today we did not write code at all - and we won't in the next "lesson", as we are going to have a quick look at namespaces. Don't worry - this will be the last theoretial "lesson" for a while ;).

{{ message }}

{{ 'Comments are closed.' | trans }}

Wait a second!

Did you know that there's are great support channels for Pagekit? Visit us today and sign up for free using email or your GitHub-, Twitter-, Google- or Facebook-Account.

Latest blog posts

Latest comments

  • How to fork a theme

  • spqr/toc updated

  • spqr/toc updated

Like my work?

If you would like to support my work, you are invited to do so.