In this episode, we start completely from scratch by setting up a new project folder and getting things ready for our own mini-framework. We create some basic directories, like app
for our core code and public
for the entry point to the application, with a simple index.php
file to make sure everything's wired up in the browser.
The main focus this time is to introduce Composer and how PSR-4 autoloading works. Composer is PHP's dependency manager, but it's also essential for automagically loading classes you define in your application. We walk through creating a composer.json
file, configuring it for PSR-4 so that everything inside the app
directory can be autoloaded just by requiring Composer's generated autoload.php
.
Then we create our first PHP class inside app/core/App.php
, and deal with namespacing so Composer knows how to find the class. We learn how to use namespaces with either an inline approach or by importing at the top of the file, and settle on using the more maintainable use
statement style for consistency going forward.
By the end, we've set up a very minimal but robust structure where we can add code to our app
directory, and it will always be easily available to us thanks to Composer and PSR-4 autoloading. This sets the stage for building the rest of our application in a clean and manageable way.