laravel项目中推荐便捷调试代码方式之:Laravel Tinker调试工具
Laravel Tinker 是一款让我们可以更方便调试 laravel 的工具,
它可以让你输入一段代码去执行,并把执行结果直接打印到命令行界面里
当你想要测试一小段代码的时候,你无需再插入各种 var_dump 和 die,
然后还要在调试完后删掉它们,No! 完全无需如此,
你只需要 php artisan tinker 就够了。
进入tinker交互式命令行界面
我这是在docker环境中(你在宿主机或真实机上也一样)
[[email protected] ~]# docker exec -it php /bin/sh
/www # ls
demo larabg localhost
/www # cd larabg/
/www/larabg # ls
LICENSE app composer.json database public server.php tests
README.en.md artisan composer.lock package.json resources storage vendor
README.md bootstrap config phpunit.xml routes tailwind.config.js webpack.mix.js
/www/larabg # php artisan tinker
Psy Shell v0.10.4 (PHP 7.4.27 — cli) by Justin Hileman
>>> show dd
35: function dd(...$vars)
36: {
37: foreach ($vars as $v) {
38: VarDumper::dump($v);
39: }
40:
41: exit(1);
42: }
ps:
show是查看源码命令,show dd是打印dd的源码
通过User模型 查询id=1的数据
>>> User::where(['id' => 1])->first()->toArray();
[!] Aliasing 'User' to 'App\Models\User' for this Tinker session.
=> [
"id" => 1,
"name" => "admin",
"email" => "[email protected]",
"email_verified_at" => null,
"current_team_id" => null,
"profile_photo_path" => "profile-photos/726sxzkRKwn1lszPOApcL4gYrRTFlhC39MEc1Ku6.gif",
"created_at" => "2020-09-25T03:37:38.000000Z",
"updated_at" => "2020-09-30T02:15:43.000000Z",
"subscribe" => 1,
"profile_photo_url" => "http://larabg.com/storage/profile-photos/726sxzkRKwn1lszPOApcL4gYrRTFlhC39MEc1Ku6.gif",
]
>>>
ps:
各种增删改命令都能执行,具体自行官方手册搜索学习
测试控制器代码:
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
class InfoController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
echo 111;
}
tinker调用控制器
>>> $c = app()->make('App\Http\Controllers\API\InfoController');
=> App\Http\Controllers\API\InfoController {#4606}
>>> app()->call([$c, 'index'], []);
111⏎
=> null
>>>
Tinker是Laravel自带的工具,直接可用,彻底解放dd()调试,是不是很方便
相关文章