An open API service indexing awesome lists of open source software.

https://github.com/grohiro/laravel-commons

Reusable Laravel components
https://github.com/grohiro/laravel-commons

laravel

Last synced: 28 days ago
JSON representation

Reusable Laravel components

Awesome Lists containing this project

README

          

# laravel-commons
再利用可能な Laravel コンポーネント

[toc]

## インストール

```bash
$ composer config repositories.grohiro/laravel-commons vcs https://github.com/grohiro/laravel-commons.git
$ composer require grohiro/laravel-commons:dev-master

```

設定ファイルを作成する。

```bash
$ php artisan vendor:publish --provider=LaravelCommons\\ServiceProvider
# edit .env and add:
# LARAVEL_COMMONS_LOG=true
```

## Logging

複数のロガーを追加します。

|ファイル|ログ|
|-------|---|
|request.log|アプリケーションが受け取る全てのリクエストとレスポンス|
|query.log|SQLログ|
|console.log|Command のログをファイルと`STDOUT`に出力する|
|http.log|外部APIを実行するときの HTTP リクエストとレスポンス|

### 使い方

#### console.log

Laravel の ログファイル `storage/logs/console.log` と `STDOUT` にログを出力します。

```php
$logger = resolve('logger.console');
$logger->debug('hoge');
// Log::debug() これは STDOUT には出力されない
```

### オプション

config/laravel_commons.php

|Key|Type|Description|
|---|----|-----------|
|logging.request|boolean|request.log を使用する|
|logging.query|boolean|query.log を使用する|
|logging.http|boolean|http.log を使用する|

## SSL

SSL を強制します。`http://` へのアクセスを `https://` にリダイレクトします。

### オプション

|Key|Type|Description|
|---|----|-----------|
|ssl.environments|array[string]|SSL を使用する `app.env` を指定する|

```php
'ssl' => [
'environments' => ['staging', 'production]
]
```

## Enum

`id` と `label` を持った `Enum` クラスを追加します。

```php
null
foreach (AgeRange::$Values as $value) {
if ($value->id === 3) {
echo $value->label;
}
}

// mutator
class User extends Model
{
function setAgeRangeAttribute($ageRange)
{
if ($ageRange !== null) {
$this->attributes['age_range_id'] = $ageRange->id;
} else {
$this->attributes['age_range_id'] = null;
}
}
function getAgeRangeAttribute()
{
return AgeRange::valueOf($this->attributes['age_range_id']);
}
}
// show AgeRange of user
echo $user->ageRange->label;
```

文字列のコード値でアクセスしたいときは `WithCode` トレイトを使用します。

```php
class Platform extends Enum {
use WithCode;

public static $Ios;
public static $Android;
public static $Values;

public function __construct($id, $label, $code)
{
parent::__construct($id, $label);
$this->code = $code;
}

public static function init() {

self::$Ios = new Platform(1, 'iOS', 'ios');
self::$Android = new Platform(2, 'Android', 'android');

self::$Values = [
self::$Ios,
self::$Android,
];
}
}

// initialize
Platform::init();

// accessor
$ios = Platform::valueOfCode('ios');
echo $ios->label;
```