laravel-bridge / scratch
适用于从头开始的项目
Requires
- php: ^7.1 | ^8.0
- illuminate/config: ^5.6 | ^6 | ^7 | ^8
- illuminate/container: ^5.6 | ^6 | ^7 | ^8
- illuminate/support: ^5.6 | ^6 | ^7 | ^8
Requires (Dev)
- ext-pdo: *
- illuminate/database: ^5.6 | ^6 | ^7 | ^8
- illuminate/events: ^5.6 | ^6 | ^7 | ^8
- illuminate/filesystem: ^5.6 | ^6 | ^7 | ^8
- illuminate/http: ^5.6 | ^6 | ^7 | ^8
- illuminate/log: ^5.6 | ^6 | ^7 | ^8
- illuminate/pagination: ^5.6 | ^6 | ^7 | ^8
- illuminate/translation: ^5.6 | ^6 | ^7 | ^8
- illuminate/view: ^5.6 | ^6 | ^7 | ^8
- mikey179/vfsstream: ^1.6.7
- mockery/mockery: ^1.3
- phpunit/phpunit: ^7.2 | ^8 | ^9
- psy/psysh: ^0.10
- squizlabs/php_codesniffer: ^3.5.2
README
从头开始创建Laravel项目。
安装
运行以下命令以要求包
composer require laravel-bridge/scratch
使用
设置您想使用此包时
数据库
需要
illuminate/database
和illuminate/events
方法 setupDatabaseConfig()
有 3 个参数,以下是其签名
public function setupDatabaseConfig(string $name, array $connection, bool $default = false);
$name
是数据库名。$connection
仅是数据库配置。$default
如果为真,将设置默认数据库。
方法 setupDatabaseConfigs()
有 2 个参数,以下是其签名
public function setupDatabaseConfig(array $connections, string $default = 'default');
$connections
是所有连接配置。$default
指定连接为默认。
示例
index.php 数据库示例
use LaravelBridge\Scratch\Application; $connections = [ 'driver' => 'sqlite', 'database' => __DIR__ . '/sqlite.db', ]; $app = Application::getInstance() ->setupDatabaseConfig('default', $connections, true) ->bootstrap();
Eloquent 也很简单。
use Illuminate\Database\Eloquent\Model; class User extends Model { } // --- $user = new User(); $user->username = 'root'; $user->password = 'password'; $user->save(); User::all()->toArray();
视图
需要
illuminate/view
,当需要翻译时需要illuminate/translation
index.php 视图示例
use LaravelBridge\Scratch\Application; Application::getInstance() ->setupTranslator(__DIR__ . '/lang') ->setupView(__DIR__, __DIR__ . '/compiled') ->withFacades() ->bootstrap(); echo View::make('view', ['rows' => [1, 2, 3]]);
模板示例 view.blade.php
@foreach ($rows as $row) {{ $row }} @endforeach
日志记录
需要
illuminate/log
和illuminate/events
方法 setupLogger()
有 3 个参数,以下是其签名
public function setupLogger(string $name, LoggerInterface $logger, bool $default = false);
$name
是日志名称,并使用 FacadeLog::driver($name)
指定。$logger
是实现了Psr\Log\LoggerInterface
的实例。$default
如果为真,将设置默认日志驱动程序。
以下是一个测试示例
$spy = new TestHandler(); $logger = new Monolog\Logger('test'); $logger->pushHandler($spy); $this->target->setupLogger('test', $logger, true) ->bootstrap(); Log::info('log_test'); $this->assertTrue($spy->hasInfoRecords());
配置
配置将使用 illuminate/config
包。以下是其优先级。
- 设置方法配置或设置步骤
- 配置加载器或引导步骤
外观
使用 withFacades()
来激活外观并注册简短类
$app->withFacades(); View::make(); // It's works
引导
引导是Laravel Kernel 中的一个生命周期。以下是其引导顺序。
\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class
\Illuminate\Foundation\Bootstrap\LoadConfiguration::class
\Illuminate\Foundation\Bootstrap\HandleExceptions::class
\Illuminate\Foundation\Bootstrap\RegisterFacades::class
\Illuminate\Foundation\Bootstrap\RegisterProviders::class
\Illuminate\Foundation\Bootstrap\BootProviders::class
在Scratch应用程序中,我们可以功能性地加载配置,并使用 withFacades()
首先注册外观。最后,在调用 bootstrap()
时,在每次调用 ServiceProvider::register()
时,对每个提供者调用 ServiceProvider::register()
,就像Laravel Kernel一样。
bootstrap()
有一个参数 $withAllLaravelProviders
,如果为真,将注册所有Laravel提供者。默认情况下,它为真。但是,如果您不想使用某些Laravel提供者,请使用 withoutLaravelProvider()
。
示例项目或库
项目
库
感谢
- 由 @recca0120 提出
- 由 @ycs77 设计logo