Blueprint PHP Framework Tutorial – Part 0

I have recently been working on a PHP framework, having spent many hours trawling through the MVC frameworks that already existed and getting frustrated at the learning curve required in order master the big picture.

The new Symfony framework resonated the most with me and I very much liked their approach towards dependency injection.

I wanted to create a framework that was very lightweight (as many frameworks take PHP’s memory usage to over 10MB before it’s even doing anything). I also wanted to concentrate on keeping the learning curve very short. It’s almost like learning a whole new language with some frameworks. I would like to think I am a proficient PHP coder and that is the language I want to program in.

I feel that young coders won’t get the exposure to the raw PHP I have worked through even though they will likely learn some good practices from diving straight into using frameworks.

And of course one final feature was that the framework should separate concerns to retain flexibility and allow designers and developers to work better together. An MVC approach is the way forward with this of course.

I have open sourced the project, so feel free to fork and modify it at will. I’d love to hear suggestions for improvement too.

N.B. I am very aware that there are some rough patches and some gaping holes in functionality. Some are deliberate (to keep the footprint down) and some are because either I haven’t figured out the best way to do something, or I simply just haven’t had chance to look at it properly yet.

Blueprint PHP Framework

Core concepts

  1. All requests are routed through /public/index.php
  2. This loads /app/bootstrap.php
  3. This initialises the autoloader and a blank container to hold the core dependencies
  4. Store all the core dependencies in App class
  5. Pass all this to Router
  6. Register route maps with the router
  7. Router determines what controller to invoke in /app/{namespace}/Controller
  8. Controller pulls out the dependencies it requires in setContainer()
  9. Model is created (if required) to do the business logic
  10. View is created in __construct() to pass output through
  11. View loads template that is requested in /out/
  12. /out/functions.php is loaded where you can create closures to manage the data needed in the template
  13. Template files are very basic and should only contain the most rudimentary PHP

Class name and class filenames must be camel case to avoid problems.

e.g localhost/css-test maps to /app/{namespace}/Controller/CssTest.php and calls CssTest();

Controllers, models, views and forms must all extend the abstract version of class from Blueprint.

e.g. CssTest should extend Controller.

Follow the MVC concept as much as possible, even if it seems like a chore at the time, it will be so much easier in the long run.

  • Controllers sole responsibility is to tie the request to a model and a view, so keep them light.
  • Models should do all the grunt work. Create logical methods and don’t let them get too big.
  • Views should pass all the data etc to the template to be output to the browser.
  • Templates should only contain the most rudimentary of PHP and a web designer with very little PHP experience should be able to work with them with only the knowledge of what variables they need for the data they want to display.
  • This separation of concerns makes it much easier for designers and developers to work together as well as making it easier to debug problems and keep the application flexible to modify.
  • For the love of god don’t do business code in the template files!

If you don’t think this has helped in your understanding in anyway, fear not. I will be going into detail about each of the steps above in the coming weeks.

So stay tuned!

One thought on “Blueprint PHP Framework Tutorial – Part 0

Leave a Reply