Front controller in PHP

When you are reading about web application development, “front controller” and “single point of entry” are frequent words you are confronted with. To my understanding, the point is to provide a single location where you decide what to do in your program. That’s all nice and well in theory, but I am confused as to how this would look in reality.

In OOP, I suppose your “controller” would commonly be instantiated in the “index.php”, or whatever file is your “entry point”. But how to go from there and how does the controller class look like? Is it something along the lines of this:

//Dummy-code, it's just to make a point
class Controller {

    //first function to be called
    public function main() {
        switch($appAction) {
            case "login": userLogin(); break;
            case "view" : viewArticle(); break;
            //...
        }
    }

    protected function userLogin() {
        //Sanitisation of user input
        //making instances of necessary classes etc.
    }

    //...
}

or did I get the whole concept completely wrong? This is the only thing I can imagine to be the case, but I’m just not sure if there is a major mistake in approaching it this way or if I just misunderstood the purpose of a front controller.

PHP out of the box works like this (forgive the obviousness, but it’s an important foundation to understand):

  1. Client visits: example.com/about.php
  2. Server (usually Apache) receives this request, looks under example.com domain file structure, and in the main directory looks for “about.php”. If it is present the server executes PHP with a target of about.php.
  3. PHP reads the file, running the script as written.

Now, what’s wrong with this? Well, mostly nothing. But a few set of complaints began to be common.

One, how do you pass extra information to a script in the URL? Well, query strings of course…and some people found them ugly, and in wild and wooly ancient times lots of rumors abounded that URLs with query strings attached did not get properly credited – or even indexed – by search engines. Gasp!

Other people just found query strings ugly and hard to understand. But what about the internet isn’t, amiright?

Another line of problems was with “web applications”, and how most or even all pages have the same basic logic at the top. You might want to do some processing of query strings, or handle cookies, load in some template logic, etc. And that was thought to be messy to have files all around, conditional includes to keep file-sizes manageable, and so on.

In ancient times PHPophiles just had a common require() of a file that contained all this common logic. But this technique fell out of favor many years later (maybe as many as 5 years later if you can believe it), as some deemed it repetitious and old-fashioned (amongst other things).

It then came into common knowledge that you could instead use Apache to route ALL requests to a single file, and have Apache convert the extra path information in the URL and pass it as a parameter to a simple index.php file!

This basic idea, that all requests are sent to a single file instead of to individual PHP files directly, became known as a “front controller”.

My favorite framework for demonstration purposes of how these things might be handled is CodeIgniter, because it isn’t super magical or complex. Take a look at their index.php file to see how things might work.

Basically it’s just a regular PHP file, which itself defines constants that might come in handy elsewhere, (like BASEPATH and such), handles some severe unrecoverable system errors (like the whole application folder being in an unknown place!), etc. Then, once all that is done, the next file in the application is loaded, and on down the line.

With front controllers a key function is to disconnect the real system directory file structure on the server from the URL path. Where as in the old days if you had a folder in the main directory named “errata”, and a file in that named “misc-gumballs.php”, the URL to get to that file was simply “example.com/errata/misc-gumballs.php”.

With a front controller you might instead choose to have “example.com/super-delicious-free-samples/gumballs”, and using Apache and PHP you instead run a index.php file which looks in a “default” folder, loads the “classes.php” file inside of it, executes the function inside the Page class defined in that file gumballs($param = null), and passes “super-delicious-free-samples” as the value of the parameter.

Ack, History! TLDR;

A front controller is just a simple PHP file which, using something like server-side Apache routing rules, has requests funneled to it instead of to individual PHP files directly. This lets you put all your common logic in that file, and you can conditionally choose how to proceed with the request based upon the parameterized data from the requesting path.

It can be as simple as a conditional include() or require(), or it can instantiate a complex Object Oriented design. There’s really nothing magical or even especially complex about them.

Your example code is not entirely off the mark, though it is a little “public void main” for usual PHP-style coding. Most systems I’ve seen implement the functions themselves as page names, but this is not a strict requirement, and there is therefore nothing wrong – conceptually – with your example!

I think we got it.

2

Front controller is just a PHP file which dispathes requests, loads configuration, initializes autoloading mechanism, etc.

Frameworks may vary on what goes into front controller, but usually there is a set of Routes, which map request URL to a (Controller, Action) pair. For example, if URL is user/login, then UserController->loginAction() should be invoked.

As an example of such architecture, you can read Symfony docs.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật