0

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?

Carlos Vergara
  • 3,592
  • 4
  • 31
  • 56
  • try testing that using a framework. smarty is a templating engine. CakePhp is a framework – Ibu Jan 27 '13 at 07:57
  • Are you going to build your own MVC-like framework? As one who's done exactly that I can say, that's a lot of work. For what you need, the Front Controller should contain methods that pertain to the different scenarios, or 'workflows'. These methods take the place of having separate 'pages' such as `login.php` and `account.php`. – Ian Atkin Jan 27 '13 at 08:00
  • @Gordon: More like a framework, I'd say it's an engine. However, I'm interested on your answer, could you elaborate on how the controller and the pages work together? I was thinking I should create a template controller class and display the webpages from there, but now I'm intrigued! – Carlos Vergara Jan 27 '13 at 08:03
  • Hmmm, sounds like you plan to delegate the work to another class. What would the template controller do, other than choose which template to use? I also think you can get away with the 'user rights' part of the equation based on what data you have coming into a view, i.e. show some elements if the user has the privilege. – Ian Atkin Jan 27 '13 at 08:09
  • ...Actually that's my problem. The template controller loads a webpage and displays it, that's all. Maybe I can control certain conditions using the `$_SESSION` variable and embedding conditions in the corresponding places, but other than that I don't know what to do, especially when I need data from the database to load a form, for instance, without relying on AJAX. – Carlos Vergara Jan 27 '13 at 08:12

1 Answers1

0

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!

Carlos Vergara
  • 3,592
  • 4
  • 31
  • 56