It’s been over five years since I transitioned from PHP to C#, so I wanted to see how one goes about making a PHP app in 2020.
I chose Laravel since its one of the more popular frameworks, much more so than my old friend CakePHP. Everything I’ve seen so far has convinced me PHP is just as useful as any other language. When I started in PHP, there was no composer, no PSR-4. Class loading was always a black box to me because each framework handled it differently and rarely required the developer to know how it worked - that’s the point.
It’s so simple now. Dependency management is essentially just npm
for a Node.js project. My dev environment is pretty similar: VirtualBox + Linux (just swapped CentOS for Debian). And now with PHPStorm and PSR-4, it’s just like writing (pseudo) strongly-typed.
My app is a developer-oriented static site generator, so I can quickly update any static site at minimal cost. All sites are pushed and delivered by S3.
Right now I am providing options for how the static page posts will be stored. Yes, you can use dependency injection in PHP!
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
// Repositories
if ($this->app['config']['database.default'] == 'dynamodb') {
$this->app->bind(PostRepositoryInterface::class, function ($app) {
$client = new DynamoDbClient([
'credentials' => [
'key' => $app['config']['database.connections.dynamodb.key'],
'secret' => $app['config']['database.connections.dynamodb.secret'],
'token' => $app['config']['database.connections.dynamodb.token']
],
'region' => $app['config']['database.connections.dynamodb.region'],
'version' => 'latest'
]);
return new AwsDynamoDbPostRepository($client, $app['config']['database.connections.dynamodb.table'], new DateTimeFactory(), new UniqueIdFactory());
});
$this->app->bind(SiteRepositoryInterface::class, function ($app) {
$client = new DynamoDbClient([
'credentials' => [
'key' => $app['config']['database.connections.dynamodb.key'],
'secret' => $app['config']['database.connections.dynamodb.secret'],
'token' => $app['config']['database.connections.dynamodb.token']
],
'region' => $app['config']['database.connections.dynamodb.region'],
'version' => 'latest'
]);
return new AwsDynamoDbSiteRepository($client, $app['config']['database.connections.dynamodb.table'], new DateTimeFactory());
});
} else {
$this->app->bind(PostRepositoryInterface::class, EloquentPostRepository::class);
$this->app->bind(SiteRepositoryInterface::class, EloquentSiteRepository::class);
}
...