In view of that, i thought it’d be nice to describe how a minimal MVC structure could be implemented without running a composer require large/framework when all you just want is a clean way to seperate your logic from presentation and do not need all the features that’d be available within large/framework.
This is a router and we only are installing it since we get pretty urls which is a must nowadays.
Provides a web router implementation: given a URL path and a copy of $_SERVER, it will extract path-info and $_SERVER values for a specific route.
Create a config.json file in the root of any directory and update it as below :
after which you’d run composer install in an open terminal (with it’s current directory where the config.json file have been placed).
For AuraPHP (or any other router) to route and prettify our routes, we need to configure the web server to forward all requests to a single file, usually index.php.
Create a web sever config file (i’m using apache, so i would have .htaccess) and a index.php file.
Put the following in the web server config file
This configuration is meant for Apache web servers, if you use Nginx or another, a quick google search should help you forward all requests to a single file.
Fire up your favorite editor with the index.php file opened
As depicted above, the methods are quite descriptive.
This is a simple example as it represents a :
GET request to the / route. The action and method param are what we actually need in order to present a little attractive something to our users. In order to achieve this, we would employ Twig since we are all about seperating the pieces of our application .
POST request to the /submit route.
If no routes match what have been registered, call the ErrorController, which is simply a wrapper for 404 HTTP requests. All it does is present the user an error page.
Only HTTP Method(s) assigned to a route would match else a 404 HTTP error would be thrown.
addHead(), addDelete() , addPut , addPatch, addOptions are also available for use. But do have it in mind that not all servers are configured to accept some certain HTTP request methods, GET and POST are available on all servers though.
There’s a C (controller) in MVC, so we’d want something to mediate between web requests and our web app. Let’s write one .
In the above code block for IndexController.php, we have instructed Twig to load a template file named index.html.twig located within the src/view directory. The file does not exist yet so it should be created at this instance.
Ok, that works right but we can’t always know what parameters a route would present as web developers are always working with dynamic data every now and then. We need dynamic routes, i.e one with parameters!!
If you aren’t working with dynamic routes, it would be an overkill to use a routing engine (or even the MVC pattern) since all pages are predetermined. Plain old multiple points of entry (i.e index.php , contact.php , about.php) OR a simple switch statement in the index.php file would do the trick.
There are other methods AuraPHP router offers but this are the most implemented of them. Do check out it’s github’s page to find out those that were not mentioned .
There you go!!. A minimal MVC framework without the use of framework Y. With that said, frameworks on the market do offer far more features than what we’ve just built so far - heck, all our ‘framework’ has is just a router and a template engine but hey you sometimes do not want that monolithic Framework.