Making This Blog
Programming 20th of July 2019
Just a quick post to explain how the code of this blog came about. I'll put it up on my github just for fun once I've done some more work on it in the future. I'll come back and edit this post in the future when I get around to it.
One of the real reasons I'm able to write this right now is because in order to interview for the new job that I have now, they asked me to make a simple blog. This seems pretty reasonable to me, don't make a big application, just build something small with three or so database tables which will require about a hundred lines of code, not including the front end stuff. So, I set to work and made this beauty.
The first thing I did was copy this template from getbootstrap.com because, hey, why kill myself designing something responsive when it's the PHP code they're interested in. Secondly, I stripped out what I didn't think I'd need, added some colour and a font and split the template up into different PHP files. I decided not to use a templating engine (PHP is certainly good enough as a templating engine for a simple blog anyway) as the point of the exercise is to demonstrate what you can code, not which fancy libraries you can use.
Next, I created my own project structure to make it a simple custom MVC framework. With my HTML in my new PHP files in a directory called 'views', I added a few more directories: 'models', 'controllers', 'images', 'assets'. I put the bootstrap css and JQuery JavaScript files inside of 'assets' and found some placeholder images for example blog posts to put inside of 'images'. It was all coming together so fast, which as you'll see, caused me to panic a bit once I was finished. Next, I created 'index.php' and put starting the database connection, starting the session and routing logic inside. For the routing, I simply take the reqeust URI, split the string up by forward slash characters and use the resulting arrays as URL segments to route to different controllers by including their PHP file. Any extra segment values are used as values instead of using ugly GET parameters.
Now that we have some global variables with the database connection and an array full of URL segment values we can write the controllers. They're simple files that include the PHP files of the models, which are PHP classes, and make an instance to access their methods to get database results. The controllers also check to see if any forms have been submitted so that they can perform any post back logic that might need to be done, declare some more global variables and then include the view files. I added a few database tables, 'articles', 'categories', 'users' and 'comments', and then created a model for each of them with methods to get, add, edit and delete database records.
A few hours later of tweaking and adding a few lines of JQuery to fetch the next set of articles using AJAX, I was done. It wasn't the most beautiful thing I'd ever written, it was simple and to be honest, it would have been better if the controllers and views were also PHP classes. It would have been better if I had've written a router that automatically found the controllers, made an instance and then used the next segment of the URL to access a public method. It would have been better with an auto-loader, one or two unit tests and possibly a deployment script. But I didn't do any of that. As often happens in the real world, I was tired so I zipped it up with the SQL and Apache rewrite rules to make it work and sent it off to my potential future employer. And then I started to panic.
In my mind, what I had just done was submit a perfectly working application that didn't showcase anything I was really capable of. How did it prove that I had skills of writing and carefully organising more than a hundred thousand lines of code and managing said code and back-end processes running across more than a dozen servers. And so, I emailed back and offered to remake the whole thing with the identical functionality using my own framework that I'd been working on for years. Yes, the whole point of the exercise was not to get everything done in 10 lines of Laravel, but technically, I had written 100% of the code of the framework I was going to use. The other selling point for suggesting this was that I had mentioned my framework, Abstraction Machine, as something I'd been building for a few years in my interview and my interviewer seems pretty interested in it.
After less than a working day I got my response. Sure, I could submit another example of the blog, they were still deciding on who to hire and the curiosity of a visual web framework still had their curiosity piqued. Great, I was thinking, this will surely impress them, the only problem being that I've only ever used my framework to export an actual working website application two times before and it wasn't all tha smooth. I got to work as soon as I could and a few bug fixes in my core code and three times the hours it took to make the first version, I had my result. A completely, functionally identical blog using my generated PHP code from my framework rather than my handwritten code from the first time. When I submitted the second version, I even sent a login so that they could look at the editing tools I used to make it and see how actions in the editor affect the generated code on the other end.
Thankfully, I ended up getting the job. I was later told that I seemed to have just a bit more real world experience. On the one hand, I was both elated to have a local, decently paying job again (more than the driving job I was doing for sure). On the other, I was deflated to realise that my framework, the one that was intended to speed up simple web application development, was far slower than an experienced developer making something by hand. I casually asked for feedback on my framework after a few days on the job and it was just what I thought I would hear. It's a fairly simple and intuitive system for structure the different pages of an application and performing database logic but putting values inside of HTML was very, very inefficient. The problem is that every time you want to put a database value on the page you have to end outputting HTML, add a new method, select the right variable, choose how it's escaped, and then start outputting HTML again. In other words, it lacked a template engine. The thing that made PHP so powerful and such a double edged sword since its inception was entirely lacking from my framework that was supposed to improved upon PHP.
The lack of decent templating is the only reason that I installed the handwritten version of my very, very simple blog here on this website yesterday. The good news is that it's given me the motivation to add something like Twig as a first class feature to Abstraction Machine so that templating is as powerful as any other solution. The bad news is that, as well as features like automatic FTP deployment and true first class custom framework objects, I've still got a while to go before it's finished in my eyes. Still, I'm working on it a bit every day and I've never felt so positive about the project. Hopefully, I'll be able to replace this blog with a version created by Abstraction Machine in the near future.
Until that happens, I don't mind using this basic thing I've strapped together. It actually has a sort of quaint charm. Just absolutely anything other than WordPress, please! However, one of the projects I need to interact with a fair amount at work is written in Symfony - a framework I never got around to using. So perhaps I'll rewrite it in Symfony instead. When I know what I'm doing with it I'll write about it here. Until next time.
Comments
There are 0 comment(s) on this blog post.
Be the first person to leave a comment!
You must by logged in to leave a comment. Please register or log in below before submitting your comment.
About Alex
Alex is a software developer who likes a lot of stuff. He enjoys writing about that stuff here.