wgriffioen / laravel-boilerplate
此包已被放弃,不再维护。未建议替代包。
我的个人样板,用于快速启动新的Laravel项目
v6.5.2
2019-11-25 20:42 UTC
Requires
- php: ^7.2
- fideloper/proxy: ^4.0
- laravel/framework: ^6.5
- laravel/tinker: ^1.0
Requires (Dev)
- barryvdh/laravel-ide-helper: ^2.6
- doctrine/dbal: ^2.9
- facade/ignition: ^1.4
- fzaninotto/faker: ^1.4
- laravel/ui: ^1.0
- mockery/mockery: ^1.0
- nunomaduro/collision: ^3.0
- phpunit/phpunit: ^8.0
- dev-master
- v6.5.2
- v6.1.0
- v6.0.3
- v5.8.28
- v5.8.7
- v5.7.26
- dev-dependabot/npm_and_yarn/eventsource-1.1.1
- dev-dependabot/composer/symfony/http-kernel-4.4.40
- dev-dependabot/npm_and_yarn/dns-packet-1.3.4
- dev-dependabot/npm_and_yarn/axios-0.21.2
- dev-dependabot/npm_and_yarn/color-string-1.9.0
- dev-dependabot/npm_and_yarn/minimist-1.2.6
This package is auto-updated.
Last update: 2022-08-31 03:19:41 UTC
README
Laravel样板是我的个人启动套件,用于加快启动新Laravel项目的速度。它基于最新的Laravel版本,包括我总是倾向于安装的一些包以及一些我发现有用的附加功能。
需求
由于默认Laravel项目中的编译后的资源已被移除,需要安装node和yarn。这是因为在项目创建后,资源会自动编译。
用法
要启动新项目,只需运行以下命令
$ composer create-project wgriffioen/laravel-boilerplate <new-project-name>
$ yarn
$ yarn run dev
包含的包
- Laravel IDE辅助工具 - barryvdh/larravel-ide-helper
- Mockery - mockery/mockery
对默认Laravel项目的修改
- 默认情况下,git会忽略public/js和public/css中的编译后的资源
- 将模型移动到命名空间
App/Models和相应的文件夹 - 默认启用Laravel Auth
- 切换到React预设。可以通过运行
php artisan preset vue轻松撤销此操作
附加功能
由于Laravel的Eloquent没有完全分离关注点,我不太喜欢它。因此,我喜欢使用仓库来查询、创建、修改和销毁数据源中的数据。我设置的方式是扩展App\Repositories\AbstractEloquentRepository,它实现了App\Repositories\RepositoryInterface。然后可以将仓库作为依赖项注入,就像在App\Http\Controllers\Auth\RegisterController中一样。
一个基本的UserRepository看起来像这样
<?php namespace App\Repositories; use App\Models\User; class UserRepository extends AbstractEloquentRepository { public function __construct() { $this->model = new User(); } public function findByEmail(string $email): ?User { return $this->model->where('email', $email)->first(); } }
要将仓库作为控制器依赖项注入,您需要在构造函数中提供仓库
<?php namespace App\Http\Controllers; use App\Repositories\UserRepository; use Illuminate\Http\Request; class UserController extends Controller { private $repository; public function __construct(UserRepository $repository) { $this->repository = $repository; } public function index(Request $request) { $user = $this->repository->findByEmail($request->input('email')); return view('users.index', ['email' => $user->email]); } }
测试
这种方法使得只用单元测试就可以更容易地覆盖你的代码
<?php namespace Tests\Unit; use App\Http\Controllers\UserController; use App\Models\User; use App\Repositories\UserRepository; use Illuminate\Http\Request; use Illuminate\View\View; use Tests\TestCase; class UserControllerTest extends TestCase { public function testIndex() { $user = factory(User::class)->make([ 'name' => 'Wim Griffioen', 'email' => 'wgriffioen@example.com' ]); $repository = \Mockery::mock(UserRepository::class); $repository ->shouldReceive('findByEmail') ->with('wgriffioen@example.com') ->andReturn($user); $request = new Request(); $request->replace(['email' => 'wgriffioen@gmail.com']); $controller = new UserController($repository); $this->assertInstanceOf(View::class, $controller->index($request)); $this->assertEquals(['email' => 'wgriffioen@example.com'], $controller->index($request)->getData()); } }
为了测试仓库的功能,你应该编写功能测试,因为它们与数据库交互
<?php namespace Tests\Feature; use App\Models\User; use App\Repositories\UserRepository; use Illuminate\Foundation\Testing\DatabaseMigrations; use Tests\TestCase; class UserRepositoryTest extends TestCase { use DatabaseMigrations; public function testFindByEmail() { factory(User::class)->create(['email' => 'wgriffioen@example.com']); $repository = new UserRepository(new User()); $this->assertInstanceOf(User::class, $repository->findByEmail('wgriffioen@gmail.com')); $this->assertEquals('wgriffioen@gmail.com', $repository->findByEmail('wgriffioen@gmail.com')->email); $this->assertNull($repository->findByEmail('johndoe@example.com')); } }
许可证
Laravel框架以及我的修改都是开源软件,受MIT许可证许可。