Building extensions for Pagekit Part #3 - Model, View, Controller
This post is older than 365 days and may be outdated. Please use the site-search to search for updated information.
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
expirationdateand 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
- Controller: As the name suggests, the controller takes control. It simply does things. If you are
savingthings, 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
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:
As you can see, we are going to create the folders
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
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
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 ;).