Jump to content

Use ProcessWire in a MVC like fashion, or just use pages?


tehandyb
 Share

Recommended Posts

Hi I am looking into ProcessWire to build an application.

I am used to building applications that have db models separated out, which is nice and organized for building the application. I would also want to separate out a lot of the logic for the application(more in a Model View/Presenter fashion) and have seen some people are doing MVC with ProcessWire. But I wonder if I use pages, would this be similar to having models? I read somewhere that Users are pages in processwire, and user attributes can be fields of these pages. So maybe pages are one of the magic ingredients in PW?

Can/should I do the same for other db models in the application(use pages as the models), or would I better be served by integrating an MVCmodule into PW, or using Laravel with PW?

It's starting to seem like PW is a very different type of framework and can do all the MVC stuff, but in a slightly different paradigm, and that Pages are like the model, and Templates are the View/Presenter/Controller. And you could separate out the controller if you wanted to create some classes and implement some routing if you wanted. 

Link to comment
Share on other sites

...........

It's starting to seem like PW is a very different type of framework and can do all the MVC stuff, but in a slightly different paradigm, and that Pages are like the model, and Templates are the View/Presenter/Controller. And you could separate out the controller if you wanted to create some classes and implement some routing if you wanted.

Template

By adding the fields you desire to a template you basically model the content/data that you want to manage.

Template file

Associated to a Template, a template file is where most of the view and controller logic takes place. PW is very flexible so you can also make this more MVC-like, look at this for an example: https://github.com/fixate/pw-mvc-boilerplate/tree/develop

Page

A page is a data object that always follows the rules and data structure of the associated Template (you could say model). In PW pages are tree based (parent-child relationships). This gives powerfull options of organizing and relating content.

Routing

PW has a default way of routing following the page tree, but by utilizing urlsegments or hooking into the routing mechanism you can define your own if needed.

So PW has it's own way of doing things. It may take some getting used to, but in my opinion it is a very nice way to model and manage your data. I believe one should always choose the right tool for a certain job, so there might be projects that are better suited to build from the ground up using a general purpose framework like Laravel or Symfony. But in my experience PW is the right tool for a whole lot of stuff.

  • Like 7
Link to comment
Share on other sites

So if I wanted to, I could just use Templates to model data. Creating a template is equivalent to modeling a table from the sounds of it.  For example say I have a DB schema with the normal PW stuff like Users, Roles, Permissions, and then I want to model data for locations, events, and relate users to locations, relate events to users, relate events to locations etc. How would I model the relationships between tables with templates, is that even possible/recommended? I guess you could make a child template that represents the relationship table. 

The app that I'm creating would let users create their own personal 'page', where other users can go and schedule events with them dependent on locations and there will be some other relationships in there too. 

Link to comment
Share on other sites

Hi tehandyb :)

There is a rough translation between traditional terms and ProcessWire's, which is:

Templates = tables

Fields = columns

Pages = rows

Template files = controllers & views

In ProcessWire, you would create the necessary fields for your content, add the to one or more templates (to define your "schema" in a way), and you create pages based on those templates. The page output is handled by it's template's file. The fields you add to the template can vary from integers to text input to map markers (and more).

In your example, you would probably want to create templates for at least event and location. Users, roles and permissions already exist in PW, but you can extend these for your own needs if you have to.

The key to linking pages together (individual users, locations and events) is the Page fieldtype. This allows you to create a field that can reference one or more other pages of a type of your choosing.

Back to your example, I'm going to presume an event might have one location or none at all. First, create a location template with the basic fields you need. Create a section of your page tree to store them. If you don't want them to show up in lists or searches, set the parent to Hidden.

Then, create a new field called location and add it to your event template. Configure it to list pages with a template of location and the parent page where they're stored in your tree. You can also choose what type of inputfield it will use - a Dropdown or PageListSelect might be appropriate depending on how many you have. When you have done that, creating or editing an event will allow you to choose a location from the list.

In the event template file, you would access it like this:

<p>This is the <?php echo $page->title ?> event.</p>
<p>The venue is: <?php echo $page->location->title ?>.</p>

I'd recommend reading some more documentation and this brilliant thread on categorising your content. :)

  • Like 7
Link to comment
Share on other sites

Wow thanks for the great answers guys, it really helps!

Now I understand the structure better. Here's another scenario, can I have multiple users that belong to an event?(looks like changing the input type of the field to 'select multiple' will do this, but how can I get the users into a page?) Also what happens if there are two locations with the same name, how can I refer to them uniquely since there is no primary key to go by?

Right now I created a field called locationObject to serve as the list of locations. I made it a field type of Page. Now to get it to select all of the Location pages, do I go to input > selectable pages >  and select Location template? I also added a page under the Home page, and set the 'parent of selectable pages' to this page that i called locationsparent. And I made the input type select. 

Link to comment
Share on other sites

You would link multiple users in a similar way - new Page field called users, configured for "template=user", and use the "Multiple pages (PageArray)" option on the field Details tab, and add to the event template. The difference in code would be this:

<?php
// Loop through all users added to the page.

foreach ($page->users as $u) {
    // Use $u (or whatever you like) but not "$user" - it is a ProcessWire system variable (current logged in user).
    echo $u->name . " is a member of this event.<br>";
}
?>

Locations with the same name would not be permitted to have the same direct parent - so the name would have to be changed when adding it. Each page still has its own ID, name and path which can be used to identify and load them within code. For example:

ID: 1042
Name: concert-hall
Title: Concert Hall

Path: /locations/concert-hall/

The ID cannot be changed, but the name can. And the path is built up based on the name of the page and the names of it's parents.

By default, names are automatically generated from the title field value, but can be overridden if necessary, generated yourself, or automatically using datetime values.

 

  • Like 4
Link to comment
Share on other sites

Another question, would I be able to have the pages for users linked to the ProcessWire users? That way I could use the permissions and roles already in ProcessWire instead of having to create pages for Roles/Permissions. 

Link to comment
Share on other sites

Yes, you could - ProcessWire users are just pages with template of "user". Because they are pages, it means I can also do things like this, if I just want to allow users of a certain role:

template=user, roles=member
Link to comment
Share on other sites

You can access the user template by going to the Admin > Templates page, click the Filter section at the top, click Show system templates, and then Yes. A new group of templates will be shown (with the "system" tag) - user is one of them. Click that to view the user template :)

  • Like 1
Link to comment
Share on other sites

Maybe a little off-topic/semantics, but I found this enlightening:

PHP and Model View Controller: impossible!

The most elementary task of an MVC framework is to notify all open views when the model changes - and this is exactly what a pure server side technology like PHP is not capable of doing because of the very nature of the web. The PHP frameworks that claim to be MVC are simply cheating: they claim to offer a certain feature (MVC) that they do not have! They may be frameworks of high quality that do a very good job at seperating user interface code from the data layer and the business logic – but that has nothing to do with MVC. If you really want to build an MVC web application, you need much more than only PHP.

So yeah, it is a bit muddling to use MVC in the context of PHP, while at the same time we have JavaScript frameworks that employ the original Smalltalk MVC concept.

Here's a talk from European Smalltalk User Group Conference called "MVC Revival on the Web".

There is even a Smalltalk web application framework called AIDAweb.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...