了解 Laravel Tinker Shell
转载自:https://juejin.im/entry/5a4ef7be518825734978b612/ 本文翻译整理自 Getting to Know the Laravel Tinker Shell Laravel 包含一个强大的 REPL,称为 tinker,由 tinker 曾经是 laravel/framework 软件包的一部分,但随着 Laravel 5.4 的发布被提取到独立的软件包中。 什么是REPL?REPL 是 Read Eval Print Loop 的缩写,它是一种交互式 shell,它接受单个用户输入,运行它们,并将结果返回给用户。我最先是通过 PHP 有一个可以运行的交互式 shell: 在Laravel 之外使用 PsySH我强烈建议您全局安装 psysh 软件包。您可以通过 运行以下命令全局安装 psysh: composer global require psy/psysh:@stable
确保你的 Composer 包在你的 export PATH="~/.composer/vendor/bin:$PATH"
要启动交互式会话,请运行 $ psysh
Psy Shell v0.8.11 (PHP 7.1.5 — cli) by Justin Hileman
>>> show array_key_exists
function array_key_exists($key,$search)
Source code unavailable.
>>> help
help
ls
dump
doc
show
...
当需要引用函数的工作方式时,甚至可以将 PHP 核心文档 作为 CLI 协同服务器下载: $ mkdir -p ~/.local/share/psysh/
$ wget -O
~/.local/share/psysh/php_manual.sqlite
http://psysh.org/manual/en/php_manual.sqlite
安装 PHP 手册后,您可以阅读文档,然后在 CLI 中尝试: $ psysh
Psy Shell v0.8.11 (PHP 7.1.5 — cli) by Justin Hileman
>>> doc array_key_exists
psysh
Psy Shell v0.8.11 (PHP 7.1.5 — cli) by Justin Hileman
>>> doc array_key_exists
function array_key_exists($key,$search)
Description:
Checks if the given key or index exists in the array
array_key_exists() returns TRUE if the given $key
is set in the array. $key can be any value
possible for an array index.
...
>>> array_key_exists('car',['bike' => 'BMX']);
=> false
我一直使用 PsySH 来查看内置的 PHP 函数是如何工作的,并以交互的方式使用 PHP 进行测试。我用来创建一个 Laravel Tinker: PsySH on Steroids就像我之前提到的那样, 文档命令该doc命令是查找有关函数或方法的文档的有效方法。例如,假设您想查看 $ php artisan tinker
>>> doc request
function request($key = null,$default = null)
Description:
Get an instance of the current request or an input item from the request.
Param:
array|string $key
mixed $default
Return:
IlluminateHttpRequest|string|array
或者,也许我想看到的是 >>> show request
> 633| function request($key = null,$default = null) 634| {
635| if (is_null($key)) {
636| return app('request');
637| }
638|
639| if (is_array($key)) {
640| return app('request')->only($key);
641| }
642|
643| return data_get(app('request')->all(),$key,$default);
644| }
在 Tinker 中使用 Artisan 命令运行 protected $commandWhitelist = [
'clear-compiled','down','env','inspire','migrate','optimize','up',];
从这个列表中,您可以看到可以运行 测试 Laravel 代码这可能 Tinker 最有用的部分之一,就是能够使用 Laravel 的代码,比如 $ php artisan tinker
>>> use AppUser;
>>> $user = new User([
'name' => 'John','email' => 'john@example.com'
]);
$user->password = bcrypt('example');
=> "$2y$10$2l1vIXYJy.Q5otmdaaNG5./l4jbxpYYlhrSipZAsJRwAuuzjsSXlq"
$user->save();
=> true
$user->toArray();
=> [
"name" => "John","email" => "john@example.com","updated_at" => "2017-09-12 06:37:13","created_at" => "2017-09-12 06:37:13","id" => 1,]
这是我在 $ php artisan tinker
>>> use AppUser;
>>> factory(User::class,5)->create();
...
以下是如何查询用户模型以从users表中返回十个结果: $php artisan tinker
>>> use AppUser;
>>> User::limit(10)->get();
=> IlluminateDatabaseEloquentCollection {#1071
all: [
AppUser {#1072
id: 1,publisher_id: null,name: "John",email: "john@example.com",created_at: "2017-09-12 06:37:13",updated_at: "2017-09-12 06:37:13",},],}
>>>
$ php artisan tinker
>>> $log = app('log');
=> IlluminateLogWriter {#1042}
>>> $log->info('test');
=> null
学到更多找出更多的最好方法是进入 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- angular – 如果从父级重新初始化FormGroup,则自定义组件Fo
- angularjs – 基于用户角色的UI-Router 1.0重定向
- bash – 一行上的Unix打印循环输出
- 《数据结构》实验二: 线性表实验 第一个
- typescript – Angular2:在渲染组件之前如何加载数据?
- Angular – 无法找到管道’keyvalue’
- shell – BitTorrent是否适用于工作场所服务器之间的复制文
- angular – Child ControlValueAccessor具有formgroup的组件
- 从 UNIX 到 GitHub:十个关于自由和开源软件历史的重要事件
- 用SoapUI进行Webservice的性能压力测试