Domanda

I've researched about the Front Controller pattern. I'm convinced I'm going to use that pattern for an upcoming project. I have no code written yet, but, how do I use presentation logic for my websites? What if I want to display certain information when the user is logged in and when the user has certain rights? What if a certain form needs data from the database in order to work? (For the latter I was thinking to call the corresponding controller with AJAX and loading the data this way but I don't know if there's a more efficient way) This leaves me a bit confusing, and I want to avoid if possible to use frameworks like Smarty or CakePHP. Thank you beforehand.

EDIT: I was thinking about using a template controller class, this is, a class whose responsability would be to load certain webpages and display them, called from the front controller. My main issue is with loading data from the database when the template page gets called. What if there's a form that needs data from the database? Do I have to rely on AJAX to load the data, or is there a way to tell a template to load data while keeping the layers separated?

È stato utile?

Soluzione

I think I got an answer. I haven't tested this yet but I think I can do it this way.

First, HTML5 has the capability to add custom data attributes to each tag. With that in mind, I can do the following, given we have a targetController class with a method named foo() and a front controller that appends the word "Controller" to every request:

<select data-target="target" data-action="foo">
</select>

This way I can call a certain controller and invoke a certain action before the document gets rendered! Now, instead of just including a template the regular way, I can load them through a DOM library that will delegate the controller loading to either the front controller as a regular request or through a class that might do the controller loading. This way I don't have to rely on javascript to do anything. I will definitely test this solution once I get to start working!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top