8 \ November 2018 \ http://www.phparch.com
FEATURE
Maintaining Laravel Applications
Jason McCreary
As the creator of Laravel Shift^1 I have a unique insight into Laravel applications. I’m
super sensitive about sounding like a sale pitch. So, I don’t want to talk about Shift
itself. I want to talk about the data derived from Shift and how that can be used to
help you write more maintainable Laravel applications.
1 Laravel Shift: https://laravelshift.com
At the time of this writing, Shift has
upgraded over 9,000 Laravel applica-
tions. Every time a Shift runs a log file
is created. Initially, these log files were
for debugging; a way for me to not only
offer support but log events that let me
know how I might improve its automa-
tion.
For full transparency, here’s an exam-
ple of one of the log files. Other than
the Shift number, the data is completely
anonymous. No copied code. No API
tokens. Just simple log messages (see
Output 1).
Now, this doesn’t seem like much.
But a lot of information can be derived
from these simple messages. What files
were upgraded? What features were
used? What packages were used? What
versions were used?
I mined these log files for metrics
and combined them with my own
insights and those from the community.
I offer these as advice for you to craft
conventional, maintainable Laravel
applications and hopefully discover
new ways to use Laravel.
Maintainable Conventions
There is such a thing as “The Laravel
Way,” and it’s not just the way Taylor
Otwell or Jeffrey Way write their
applications. A majority of Laravel
applications adopt the conventions set
by the framework. There are always
outliers, but I assure you things get very
difficult when it comes time to upgrade
those applications. The conventions
outlined here are driven by the data
derived from upgrading thousands of
Laravel applications. They have been
put into practice not only by Shift
through automation, but personally in
my own applications and those I work
on.
Avoid Overwriting Config Values
Config files are the most changed files.
While this makes sense, it can be pain-
ful when time to upgrade. Every major
release, and even a few minor releases,
include changes to the config files. Of
course, these config files are where
you set different options and values to
customize your application. Shouldn’t
you change them?
The short answer is yes. But the longer
answer is you should do so in a way that
you don’t edit the config file directly.
It sounds odd, but Laravel provides a
way for you to do this with environ-
ment variables. Unfortunately, many
Output 1
- *** Shift version: 0281cff03fee62f250253f1e485dc7a
- *** Cloning application...
Shift 5.2: application did not contain references to SelfHandling
Shift 5.2: could not upgrade middleware Data:
- ["application\/Http\/Middleware\/Authenticate.php",
- "application\/Http\/Middleware\/EncryptCookies.php",
- "application\/Http\/Middleware\/RedirectIfAuthenticated.php"]
Shift 5.2: could not upgrade application/Providers/RouteServiceProvider.php
Shift 5.2: could not patch application/Providers/RouteServiceProvider.php
Shift 5.2: could not find User model path
Shift 5.2: found additional uses of Event names
Shift 5.2: matched core file: phpunit.xml with version: 5.1.
Shift 5.2: matched core file: tests/TestCase.php with version: 5.1.
Shift 5.2: matched core file:
- database/migrations/2014_10_12_100000_create_password_resets_table.php
- with version: 5.1.
Shift 5.2: matched core file: public/.htaccess with version: 5.1.
Shift 5.2: matched core file: config/broadcasting.php with version: 5.1.
Shift 5.2: matched core file: config/cache.php with version: 5.1.
Shift 5.2: matched core file: config/compile.php with version: 5.1.
Shift 5.2: matched core file: config/database.php with version: 5.1.
Shift 5.2: matched core file: config/filesystems.php with version: 5.1.
Shift 5.2: matched core file: config/queue.php with version: 5.1.
Shift 5.2: matched core file: config/view.php with version: 5.1.
Shift 5.2: could not upgrade config files Data: {"1":"config\/auth.php",
- "2":"config\/mail.php","3":"config\/services.php","4":"config\/session.php"}
Shift 5.2: could not upgrade package.json
Shift 5.2: application contained phpspec/phpspec requirement of ~2.
Shift 5.2: found customized namespace
- *** Shift ran in: 212.