One of the common problems of a web application is the need to return HTML and JSON to a request. Why? Imagine we have a SPA who needs the responses in JSON, and a backend that needs HTML. In this article, I will show you some ways on how to accomplish this in Laravel.
In this tutorial, I will show you a way of architecture a Service Layer in Laravel, to interact with the Unsplash API. The Service Layer using DTOs, won’t have any concerns about validating data. Let’s see how.
Unsplash is probably the most popular stock photography website, and it has a very big reason to be so popular.
All the photos are very good and entirely free to use, even for commercial uses.
Unsplash also has an API, unlimited, and free to use.
Websites like this one, Medium, and Trello, among others, use it to provide free quality photos for their…
In this article, I will talk about how to have camelCase properties in Eloquent models. Let’s see these two ways.
One of the issues in using models with Eloquent ORM is having the default behavior of all the attributes in snake_case.
Like this for example:
// We import the model class.
use App\Model\Image;// Create a new instance.
$image = new Image();// Use a property of the model.
Of course, this could be an issue, if we want to use a different convention.
And is this way, because, the attributes have the same names of the columns…
This layer can be between the domain and the database and provides access to data.
Repositories are a Design Pattern, that provides access to data. This data could be stored in a database, file system, or external service.
They were introduced as part of the Domain-Driven Design in 2004, and want to provide an abstraction of data the application can use, like an interface to a collection.
Adding, removing, updating, and selecting items from the collection, through methods, without to deal with database or other concerns.
We want to achieve loose coupling, and keep the domain objects persistence ignorant.
Or do you have a web app? You can also turn it into a desktop application!
What we are going to build?
A desktop application that allows managing of contacts, using a Laravel web app.
For this, we are going to use Laravel Electron project.
This can also be interesting for anyone who wants to convert his web application into a desktop application.
Why we should avoid FAT Controllers, and how to structure the Model layer in Laravel.
Laravel is one of the MVC frameworks from the PHP ecosystem.
And, as such, when we use it, the M and C, models and controllers, have a lot of code.
This makes sense because we need these layers to do the vastest and different operations.
The problem happens when we have a lot of code and responsibilities in one of the layers. This can harm our codebase.
Bloating anything is always an anti-pattern. Separation of concerns helps maintenance and testing.
Let’s get the unsafe client IP using invokes and complying with the SRP.
SRP means the Single Responsibility Principle and is a recommendation who says that each software module or class should have one and only one reason to change.
Gather together the things that change for the same reasons. Separate those things that change for different reasons.
— Bob Martin
With this principle in mind, I wanted to have a class with only one responsibility, that could give the client IP.
Of course, there are several approaches, and one of them is to use the magic method __invoke.
First step. Install PHP OSX.
What is this?
This is a binary package that installs several PHP versions in macOS.
This package installs the (usually) latest PHP 7.3/7.2/7.1/7.0/5.6/5.5/5.4/5.3 on OS X 10.6 (aka Snow Leopard), OS X 10.7 (aka Lion) and OS X 10.8 (aka Mountain Lion) and OS X 10.9 (aka Mavericks) and OS X 10.10 (aka Yosemite) and OS X 10.11 (aka El Capitan) and macOS 10.12 (aka Sierra) and macOS 10.13 (aka High Sierra) in /usr/local/php5. It installs many useful extensions (see below) and ini-settings and is what we at Liip use for our development. It’s especially…
Table of Contents1. Logging: what are the best practices?
1.1 Be aware of the levels
1.2 What is your default log level?
1.3 For each level address channels
1.4 Log retention policy
1.5 Logging personal information or secrets
1.6 Use the appropriate tools
1.7 Same log format in every environment2. Heroku Logging Overview
2.1 Logplex, sources, and drains
2.2 Heroku and file-based logging
2.3 Heroku and Syslog or Database drains
2.4 Troubleshooting with Heroku logs
I never put too much thought into it until recently, but the reality is when we are in a development environment with a…