Both Laravel and Drupal have an interactive debugger & REPL environment for tinkering in PHP. You can test various php statements, resolve services or even query the database! Both environment's are customized versions of the excellent PsySh.
-
Laravel:
php artisan tinker
-
Drupal via a Drush:
drush php
This new command, ddev tinker
, checks which project you are currently in, and runs the correct command for the project. If you bounce between projects types, this command is really useful.
This command works best if installed globally (set-and-forget). It can then be accessed in any Laravel or Drupal project.
- Copy the web/tinker file into
~/.ddev/commands/web
.
You could also use this command on a per-project basis.
- Copy the web/tinker file into your project
./.ddev/commands/web
.
To start your framework's REPL environment, simply type the follow:
ddev tinker
- Tinker can accept simple arguments.
$ ddev tinker 6+8
14
- More complex arguments should be wrapped with '.
$ ddev tinker 'User::first()'
[!] Aliasing 'User' to 'App\Models\User' for this Tinker session.
App\Models\User^ {#4400
...
$ ddev tinker 'node_access_rebuild()'
[notice] Message: Content permissions have been rebuilt.
$ ddev tinker '$node = \Drupal\node\Entity\Node::load(1); print $node->getTitle();'
Who Doesn’t Like a Good Waterfall?
While this might be helpful for a quick one-off command, it's recommend to run ddev tinker
for tinkering to avoid any Docker connection delays between multiple commands.
- Wrapping may also work with ", depending on the command used. For more consistent results between frameworks and host OS, it is recommended to use '.
- See ddev/ddev#2547
Contributed by @tyler36