wgriffioen / laravel-boilerplate
My personal boiler plate to quickly bootstrap a new Laravel project
Installs: 12
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 6
Type:project
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 boiler plate is my personal starter kit to speed up the process of bootstrapping a new Laravel project. It is based on the most recent version of Laravel and includes a few packages I always tend to install as well as a few additions which I find useful.
Requirements
Because the compiled assets from the default Laravel project are removed, it's required to have node and yarn installed. This is because after the project is created, the assets are compiled automatically.
Usage
To bootstrap a new project, simply run the following commands
$ composer create-project wgriffioen/laravel-boilerplate <new-project-name>
$ yarn
$ yarn run dev
Included packages
- Laravel IDE helper - barryvdh/larravel-ide-helper
- Mockery - mockery/mockery
Modifications to the default Laravel project
- The compiled assets in
public/js
andpublic/css
are by default ignored by git - Move the models to the namespace
App/Models
and the corresponding folder - Enable Laravel Auth by default
- Switch to the React preset. It's easy to revert this by running
php artisan preset vue
Additions
I'm not a fan of Laravel's Eloquent because it's not completely separation of concerns. Because of this, I like to make
use of repositories to query, create, modify and destroy data in the data source. The way I set this up, is to extend
the App\Repositories\AbstractEloquentRepository
which implements the App\Repositories\RepositoryInterface
. The
repository can than be injected as a dependency as can be in App\Http\Controllers\Auth\RegisterController
.
A basic UserRepository look like this
<?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(); } }
To inject the repository as a dependency of the controller, you need to supply the repository to the constructor:
<?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]); } }
Testing
This approach makes it easier to cover your code with only unit tests
<?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()); } }
In order to test the functionality of the repositories, you should write feature tests as they interact with the database:
<?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')); } }
License
The Laravel framework as well as my modifications are open-source software licensed under the MIT license.