Downloads
Stars
Version
To start project you should have locally installed:
php@^7.4|^8.0
,composer
. \ You can use google search with requests:linux php local install
,brew install php
.
After first setup project can work via Valet or docker containers.
Main Core Code for starter project modules and extensions
After installing CoreKit microservice you will receive next services out of the box:
!!!!Never commit the auth.json file to your repository. For docker containerization use Vault or other secure variables to store access token for a user.
composer config --global --auth http-basic.repo.packagist.com AttraactGroupUser <request token from maintainer>
{
"repositories": [
{"type": "composer", "url": "https://repo.packagist.com/attractgroup/"}
]
}
Step by step instructions, that will help you start a new project to build something amazing.
List of root directories for future project:
project-files
- contain all code files.docker
- contains all scripts for docker containerization process.docs
- contain all readme.md doc files.terraform
- contain all terraform templates for AWS resources setup.You can find full description about each directory setup below.
Replace directories and project names to the real project name.
Create root directory of the project. For example, attract-starter-kit
:
mkdir -m 777 attract-starter-kit &&
cd attract-starter-kit
After that you are ready for project-files
install:
laravel new project-files &&
cd project-files
TIMELESS - operations that will be changed after several steps.
Follow this TODO list to set up starter project:
^7.4|^8.0
DB_CONNECTION
to use sqlite driver into your .env
:
DB_CONNECTION=sqlite
DB_DATABASE=project_files # remove this row
cd ./database && touch database.sqlite && cd ..
[
'mysql' => [
//...,
'strict' => false,
//...,
]
];
composer require attract-cores/laravel-core-kit attract-cores/laravel-core-test-bench
vendor:publish
operations to create configuration environment for new project:
php artisan vendor:publish --tag=laravel-mail &&
php artisan vendor:publish --tag=attract-core-kit-core-modules --force &&
php artisan vendor:publish --tag=attract-core-kit-auth-migrations --force &&
php artisan vendor:publish --tag=attract-core-verification-broker-migrations --force &&
php artisan vendor:publish --tag=attract-core-kit-auth-seeders --force &&
php artisan vendor:publish --tag=attract-core-kit-auth-tests &&
php artisan vendor:publish --tag=attract-core-kit-tests &&
composer dump-autoload
cd ../terraform/tf-s3 && ln -s ../vars/variables.tf variables.tf && cd ../../project-files &&
cd ../terraform/s3 && ln -s ../vars/variables.tf variables.tf && cd ../../project-files
./config/auth.php
api driver from token to passport:
[
'api' => [
//'driver' => 'token', you can remove this row.
'driver' => 'passport',
'provider' => 'users',
'hash' => false,
],
];
'read_write_timeout' => 0
to your ./config/database.php
redis section:
[
'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', NULL),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
'read_write_timeout' => 0, // Add this into each settings block to prevent errors - "error while reading line from the server."
],
];
api.php
and web.php
route files.AttractCores\LaravelCoreKit\Http\Middleware\EnsureEmailIsVerified
in Kernel.php
:
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
//...,
'verified' => \AttractCores\LaravelCoreKit\Http\Middleware\EnsureEmailIsVerified::class,
//...,
];
APP_KEY
) from .env
into .env.example
and .env.dev
.env.example
-> .env
. If you will use docker skip this row.
If you are using Laravel Valet, set up variables for your local environment.$this->call([
DefaultAdminSeeder::class,
DefaultRolesAndPermissionsSeeder::class
]);
AttractCores\LaravelCoreAuth\Database\Seeders\DefaultRolesAndPermissionsSeeder
class and add new models like parent class do.
For example:
namespace Database\Seeders;
use App\Models\Permission;
use App\Models\Role;
use AttractCores\LaravelCoreAuth\Database\Seeders\DefaultRolesAndPermissionsSeeder as CoreDefaultRolesAndPermissionsSeeder;
use AttractCores\LaravelCoreAuth\Resolvers\CorePermission;
use AttractCores\LaravelCoreAuth\Resolvers\CoreRole;
/**
* Class DefaultRolesAndPermissionsSeeder
*
* @package AttractCores\LaravelCoreAuth\Database\Seeders\Publishes
* Date: 16.12.2020
* Version: 1.0
* Author: Yure Nery <[email protected]>
*/
class DefaultRolesAndPermissionsSeeder extends CoreDefaultRolesAndPermissionsSeeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
parent::run();
$permissions = CorePermission::all();
$permissionSlugFieldName = CorePermission::getSlugField();
if ( ! $permissions->contains($permissionSlugFieldName, Permission::CAN_ORGANIZATION_ACCESS) ) {
CorePermission::factory()
->createOne([ 'name_en' => 'Can have Organisation access', 'slug' => Permission::CAN_ORGANIZATION_ACCESS ]);
CoreRole::factory()
->createOne([ 'name_en' => 'Organisation Access', 'slug' => Role::CAN_ORGANIZATION ])
->permissions()
->sync([ 6 ]);
}
}
}
php artisan migrate --seed
php artisan passport:keys --force &&
php artisan passport:client --client --no-interaction &&
php artisan passport:client --password --no-interaction
APP_KIT_AUTH_PASSWORD_GRANT_CLIENT_ID
and APP_KIT_AUTH_PASSWORD_GRANT_CLIENT_SECRET
variables values in .env
.
Update them in ./docker/local/envs/{YOUR_ENV}
if you are using docker.OauthTest.php
class by adding this function:/**
* Return status for handler catchers.
*
* @return int
*/
protected function getCantLoginStatus()
{
return 401;
}
RegisterTest.php
adn add replace of testApiRegistrationValidation
function:/**
* Test api registration validation.
*
* @return void
* @throws \Throwable
*/
public function testApiRegistrationValidation()
{
$notUnique = $this->getTestRegisterData(false, 5);
$response = $this->withHeaders([ 'Authorization' => $this->getBearerClientToken() ])
->json('POST', $this->getRegisterRoute(), $notUnique);
$response->assertStatus(422);
$errors = collect($response->decodeResponseJson()->json('errors'));
$this->assertEquals(2, count($errors));
$this->assertTrue($errors->contains('field', 'password'));
$this->assertTrue($errors->contains('field', 'email'));
}
./tests/Feature/ExampleTest.php
file.project-files
root(if using Valet):
./vendor/bin/phpunit --stop-on-failur
Any core request, controller, resource or library can be easily extended via Laravel service providers bind features.
After
vendor:publish
process yourAppServiceProvider
already include all necessary functions for extension.
After CoreKit installation project will grow, so we will need to extend default user resource with new relation expands. Let's do this.
For example: we need to separate fist_name and last_name fields. Don't forget to update the migration.
namespace App\Http\Resources;
use AttractCores\LaravelCoreKit\Http\Resources\UserResource as CoreUserResource;
use Illuminate\Support\Arr;
/**
* Class UserResource
*
* @property \App\Models\User $resource
*
* @package App\Http\Resources\PublicResources
* Date: 17.12.2020
* Version: 1.0
* Author: Yure Nery <[email protected]>
*/
class UserResource extends CoreUserResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function toArray($request)
{
$parentData = Arr::except(parent::toArray($request), [ 'name' ]);
return array_merge(
$parentData, [
'first_name' => $this->resource->first_name,
'last_name' => $this->resource->last_name,
'relations' => array_merge($parentData[ 'relations' ], [
'avatar' => $this->whenLoaded('avatar', function () {
return new MediaResource($this->resource->avatar);
}),
]),
]);
}
}
Also, we should update App\Models\User
class for our case:
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'email',
'first_name',
'last_name',
'firebase_token',
];
After we create a new resource we should tell laravel to bind to our resource class rather, then to kit's one.
Add given code row into extendsCoreKitBinds
function in AppServiceProvider
:
// Replace Core User Resource
$this->app->bind(\AttractCores\LaravelCoreKit\Http\Resources\UserResource::class, \App\Http\Resources\UserResource::class);
After that manipulations, any UserResource
response in CoreKit actions will use your class rather, then core one.