In this episode, we're laying the groundwork for our application's configuration system — a crucial step for any app! We start by creating a config
directory at the root of our project, where we'll keep simple PHP files that return arrays with our config values. For now, we make an app.php
file that just returns the app name, but this will soon grow to house more settings.
Then, we build a Config
class completely from scratch inside a new Config
directory. To make sure the rest of our app can access configuration easily, we also create a ConfigServiceProvider
and register it with the application's container. This sets us up to inject or pull config settings everywhere we need them.
Next, we implement a convenient get
method in our Config
class. This lets us access config values using dot notation (e.g., app.name
), making it super easy to retrieve deeply nested config in a concise way. We pull in an external package to help with dot notation lookup, set things up so a default value is returned if the config key isn't found, and run a few tests to prove it's all working. For now, we're faking our config data in the class, but in the next episode, we'll hook it up to dynamically load values from actual config files.
By the end, we have a tidy, testable way to grab config values anywhere in our app, and we're ready to extend this in the next lesson.