loilo / contao-exec-bundle
直接从控制台执行与Contao相关的PHP代码
1.0.3
2020-04-02 00:02 UTC
Requires
- php: >= 7.1
- contao/core-bundle: ^4.3
- loilo/lowlight: ^2.0
- psy/psysh: 0.7.*|0.8.*|0.9.*|0.10.*
Requires (Dev)
Suggests
- loilo/contao-illuminate-database-bundle: Enables easy database querying and model manipulation
This package is auto-updated.
Last update: 2024-09-24 07:12:01 UTC
README
此包允许在Contao应用程序的上下文中直接从命令行执行PHP代码。
它提供了两种方法来实现这一点
-
基于PsySH的REPL,用于尝试或快速了解您的Contao项目的状态。
-
eval
命令,用于以特定数据交换格式获取程序的结果——这对于从Contao外部访问数据非常有用。
安装
composer require loilo/contao-exec-bundle
用法
REPL
您可以使用debug:repl
命令打开REPL
vendor/bin/contao-console debug:repl
这将带您进入一个加载了Contao框架、所有模型、DCAs等的REPL。
此外,如果您已安装,db()
帮助程序将自动加载到命名空间中,因此您可以立即进行一些快速操作
// Get the URL to the newest page db()->from('page')->asModel()->orderBy('id', 'desc')->first()->getAbsoluteUrl()
与往常一样,所有选项都通过以下方式可用
vendor/bin/contao-console help debug:repl
评估
contao:eval
命令接受一些PHP代码,将其传递到REPL并输出结果。
如果您正在手动使用终端,您可能想要使用debug:repl
。然而,contao:eval
可以是一个从其他进程(如Node.js)等访问Contao数据的强大工具。
因此,它不仅具有适用于人类可读数据的默认dump
输出格式,还包含其他一些格式,包括json
# Get ID and title of the newest page as a JSON object # This example requires the loilo/contao-illuminate-database-bundle to be installed vendor/bin/contao-console contao:eval 'db()\ ->from("page")\ ->select("id", "title")\ ->orderBy("id", "desc")\ ->first()'\ --format json\ --no-ansi # > {"id":1,"title":"Home"}
再次使用vendor/bin/contao-console help contao:eval
来查看使用不同选项可以实现什么。