wgriffioen/laravel-boilerplate

此包已被放弃,不再维护。未建议替代包。

我的个人样板,用于快速启动新的Laravel项目


README

Laravel样板是我的个人启动套件,用于加快启动新Laravel项目的速度。它基于最新的Laravel版本,包括我总是倾向于安装的一些包以及一些我发现有用的附加功能。

需求

由于默认Laravel项目中的编译后的资源已被移除,需要安装node和yarn。这是因为在项目创建后,资源会自动编译。

用法

要启动新项目,只需运行以下命令

$ composer create-project wgriffioen/laravel-boilerplate <new-project-name>
$ yarn
$ yarn run dev

包含的包

对默认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许可证许可。