Is the FrontController Pattern relevant for single page app

228 Views Asked by At

I am developing an app using KnockOutJS on the front end and PHP on the back-end.

The back-end has two entry points: index.php which contains all the html code and includes all the JS files from knockoutJS and my custom JS files, css etc. The other is main.php, this is where all my AJAX requests go to from my ViewModel, and it us pure PHP.

I don't see how I can make this a single entry app without mixing html and php code in the same file, (index.php). It would be counterproductive to do that because mixing multiple languages in the same file is generally confusing.

My question is, how can I apply a kind of FrontController pattern in this case? The FrontController pattern looks an awful lot like the command pattern. What are its advantages? (other than having a single entry point) How do I split my main.php file up so that I don't have 30 if else statements in a row, one for each kind of request?

1

There are 1 best solutions below

1
odan On

Usually, a front controller is used as the central entry point into an application.

In combination with a router, dispatcher and MVC, the front controller brings real advantages and the clear separation of responsibilities.

Using a front controller makes it possible to create Clean URL or "Pretty URL" very easily. Usualy you need a corresponding .htaccess file which forwards all requests to the front controller (index.php).

e.g.

# Redirect to front controller
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L] 

In the front controller, the application is getting bootstrapped and the request is forwarded to the appropriate controller action by matching the URL path. The controller takes the request, transfers the calculated data to the View (template engine) for rendering and returns the result of the view as a response.