I am in the process of creating a website from scratch, which is proving to be my first real encounter with PHP. For a while, I was looking at several PHP frameworks out there, but decided that I would rather get to know the language first than attempt to learn a framework.
One of the things I got hung up on was how I could redirect to a particular page depending on the logic of the currently executing page (by doing a redirect on the server). An example would be if you access a particular page for which you need to be logged in. At this point, you would want to redirect the user to a logon page. I searched all over and found people asking the same question, even someone asking how to redirect in PHP the way one does in Struts, which I realize now is not a very sensible question, as Struts is a framework, and PHP is a language.
The answer comes in the form of the Model-View-Controller (or MVC) design pattern. Though I had read much about it and thought I understood it, I had never applied it practically, so it wasn’t until really starting work on this website that I started to make sense of it, as is often the case.
Due to my inexperience with the PHP language, my implementation of MVC is very rudimentary, yet it is proving to work for me. I am using the include( ) function everywhere to pass control from one piece of code of another. How it works is as follows:
I have a folder called actionhandlers (the term maybe being borrowed somewhat from Struts concepts). In this folder, I have a number of PHP files, each representing a handler for a particular action. In another folder called views, I have a whole lot of files representing actual views, which are ultimately the outcome of calling an action.
When a user makes a request to the application, that request is routed through the index.php file at the root of the application, and part of the request is an ?action parameter. Based on the value of the action parameter, the control is passed to the php file in the actionhandlers folder with the same name (so that “?action=login” is handled by code in the “actionhandlers/login.php” file). This passing of control is done by a simple include( ).
Now the action handler in turn will implement some logic, the final outcome of which will be another include( ) statement, mostly including a file from the “/views” folder, but possibly passing control to a different action handler.
This very simple, to the point of being barbaric, implementation of the MVC design pattern has done away completely with the need of trying to do something like a “server-side redirect”.
Now the complexity lies in tying up the web application to the model that backs it. For me, this is rather a gray area. I would think that in most MVC scenarios, the model should have a completely independent API, so that you could easily replace whatever you are doing on the presentation side of things. In other words, your business logic should ideally reside entirely in the model.
I guess that what many of the frameworks out there do is not much different, but like I say, I think it is important for me to get a grip on the language before trying to learn a framework.