0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

いい加減ちゃんとlaravelの初期設定を覚える

Posted at

目的

  • laravelのアプリ作成直後に実施する初期設定でやらないといけないことをまとめる

環境

  • ハードウェア環境
項目 情報
OS macOS Catalina(10.15.5)
ハードウェア MacBook Pro (13-inch, 2020, Four Thunderbolt 3 ports)
プロセッサ 2 GHz クアッドコアIntel Core i5
メモリ 32 GB 3733 MHz LPDDR4
グラフィックス Intel Iris Plus Graphics 1536 MB
  • ソフトウェア環境
項目 情報 備考
PHP バージョン 7.4.8 Homebrewを用いてこちらの方法で導入→Mac HomebrewでPHPをインストールする
Laravel バージョン 6.X commposerを用いてこちらの方法で導入→Mac Laravelの環境構築を行う
MySQLバージョン 8.0.19 for osx10.13 on x86_64 Homwbrewを用いてこちらの方法で導入→Mac HomebrewでMySQLをインストールする

情報

  • 筆者はMacに直接環境を構築して今回の記事の検証を行った。
  • 本説明で実行するコマンドは特筆しない限り、前のコマンドと同じディレクトリで実行するものとする。
  • 実行するコマンドは特筆しない限り一つ前に実行したコマンドと同じディレクトリで実行する。

条件

  • アプリの作成、DBとの紐付けは完了しているものとする。

概要

  1. タイムゾーンとローカルの設定
  2. 定数ファイルの作成
  3. Logの出力ファイルの設定
  4. シンボリックリンクの作成

詳細

  1. タイムゾーンとローカルの設定

    1. アプリ名ディレクトリで下記コマンドを実行して設定ファイルを開く。

      $ vi config/app.php
      
    2. 下記のように修正する。

      • 修正前

        アプリ名ディレクトリ/config/app.php
        /*
        |--------------------------------------------------------------------------
        | Application Timezone
        |--------------------------------------------------------------------------
        |
        | Here you may specify the default timezone for your application, which
        | will be used by the PHP date and date-time functions. We have gone
        | ahead and set this to a sensible default for you out of the box.
        |
        */
        
        'timezone' => 'UTC',
        
        /*
        |--------------------------------------------------------------------------
        | Application Locale Configuration
        |--------------------------------------------------------------------------
        |
        | The application locale determines the default locale that will be used
        | by the translation service provider. You are free to set this value
        | to any of the locales which will be supported by the application.
        |
        */
        
        'locale' => 'en',
        
      • 修正後

        アプリ名ディレクトリ/config/app.php
        /*
        |--------------------------------------------------------------------------
        | Application Timezone
        |--------------------------------------------------------------------------
        |
        | Here you may specify the default timezone for your application, which
        | will be used by the PHP date and date-time functions. We have gone
        | ahead and set this to a sensible default for you out of the box.
        |
        */
        
        'timezone' => 'Asia/Tokyo',
        
        /*
        |--------------------------------------------------------------------------
        | Application Locale Configuration
        |--------------------------------------------------------------------------
        |
        | The application locale determines the default locale that will be used
        | by the translation service provider. You are free to set this value
        | to any of the locales which will be supported by the application.
        |
        */
        
        'locale' => 'ja',
        
  2. 定数ファイルの作成

    1. 下記コマンドを実行して定数ファイルを予め作成して開く。

      $ vi config/const.php
      
    2. 下記の内容を記載してPHPのソースをすぐ記載できる状態にしておく。

      アプリ名ディレクトリ/config/const.php
      <?php
      
  3. Logの出力ファイルの設定(こちらの記事を参考にして作業した→Laravel Log dailyログとerrorログを別々に出力する

    1. 下記コマンドを実行してlogの設定ファイルを開く。

      $ vi config/logging.php
      
    2. 下記のように修正する。

      • 修正前

        アプリ名ディレクトリ/config/logging.php
        <?php
        
        use Monolog\Handler\NullHandler;
        use Monolog\Handler\StreamHandler;
        use Monolog\Handler\SyslogUdpHandler;
        
        return [
        
            /*
            |--------------------------------------------------------------------------
            | Default Log Channel
            |--------------------------------------------------------------------------
            |
            | This option defines the default log channel that gets used when writing
            | messages to the logs. The name specified in this option should match
            | one of the channels defined in the "channels" configuration array.
            |
            */
        
            'default' => env('LOG_CHANNEL', 'stack'),
        
            /*
            |--------------------------------------------------------------------------
            | Log Channels
            |--------------------------------------------------------------------------
            |
            | Here you may configure the log channels for your application. Out of
            | the box, Laravel uses the Monolog PHP logging library. This gives
            | you a variety of powerful log handlers / formatters to utilize.
            |
            | Available Drivers: "single", "daily", "slack", "syslog",
            |                    "errorlog", "monolog",
            |                    "custom", "stack"
            |
            */
        
            'channels' => [
                'stack' => [
                    'driver' => 'stack',
                    'channels' => ['single'],
                    'ignore_exceptions' => false,
                ],
            
                'single' => [
                    'driver' => 'single',
                    'path' => storage_path('logs/laravel.log'),
                    'level' => env('LOG_LEVEL', 'debug'),
                ],
            
                'daily' => [
                    'driver' => 'daily',
                    'path' => storage_path('logs/laravel.log'),
                    'level' => env('LOG_LEVEL', 'debug'),
                    'days' => 14,
                ],
            
                'slack' => [
                    'driver' => 'slack',
                    'url' => env('LOG_SLACK_WEBHOOK_URL'),
                    'username' => 'Laravel Log',
                    'emoji' => ':boom:',
                    'level' => env('LOG_LEVEL', 'critical'),
                ],
            
                'papertrail' => [
                    'driver' => 'monolog',
                    'level' => env('LOG_LEVEL', 'debug'),
                    'handler' => SyslogUdpHandler::class,
                    'handler_with' => [
                        'host' => env('PAPERTRAIL_URL'),
                        'port' => env('PAPERTRAIL_PORT'),
                    ],
                ],
            
                'stderr' => [
                    'driver' => 'monolog',
                    'handler' => StreamHandler::class,
                    'formatter' => env('LOG_STDERR_FORMATTER'),
                    'with' => [
                        'stream' => 'php://stderr',
                    ],
                ],
            
                'syslog' => [
                    'driver' => 'syslog',
                    'level' => env('LOG_LEVEL', 'debug'),
                ],
            
                'errorlog' => [
                    'driver' => 'errorlog',
                    'level' => env('LOG_LEVEL', 'debug'),
                ],
            
                'null' => [
                    'driver' => 'monolog',
                    'handler' => NullHandler::class,
                ],
            
                'emergency' => [
                    'path' => storage_path('logs/laravel.log'),
                ],
            ],
        ];
        
      • 修正後

        アプリ名ディレクトリ/config/logging.php
        <?php
        
        use Monolog\Handler\NullHandler;
        use Monolog\Handler\StreamHandler;
        use Monolog\Handler\SyslogUdpHandler;
        
        return [
        
            /*
            |--------------------------------------------------------------------------
            | Default Log Channel
            |--------------------------------------------------------------------------
            |
            | This option defines the default log channel that gets used when writing
            | messages to the logs. The name specified in this option should match
            | one of the channels defined in the "channels" configuration array.
            |
            */
        
            'default' => env('LOG_CHANNEL', 'stack'),
        
            /*
            |--------------------------------------------------------------------------
            | Log Channels
            |--------------------------------------------------------------------------
            |
            | Here you may configure the log channels for your application. Out of
            | the box, Laravel uses the Monolog PHP logging library. This gives
            | you a variety of powerful log handlers / formatters to utilize.
            |
            | Available Drivers: "single", "daily", "slack", "syslog",
            |                    "errorlog", "monolog",
            |                    "custom", "stack"
            |
            */
        
            'channels' => [
                'stack' => [
                    'driver' => 'stack',
                    // 下記を追記修正する
                    'channels' => [
                        'daily',
                        'errors',
                    ],
                    'ignore_exceptions' => false,
                ],
            
                'single' => [
                    'driver' => 'single',
                    'path' => storage_path('logs/laravel.log'),
                    'level' => env('LOG_LEVEL', 'debug'),
                ],
            
                'daily' => [
                    'driver' => 'daily',
                    // 下記を追記修正する
                    'path' => storage_path('logs/daily.log'),
                    'level' => env('LOG_LEVEL', 'debug'),
                    'days' => 14,
                ],
            
                'slack' => [
                    'driver' => 'slack',
                    'url' => env('LOG_SLACK_WEBHOOK_URL'),
                    'username' => 'Laravel Log',
                    'emoji' => ':boom:',
                    'level' => env('LOG_LEVEL', 'critical'),
                ],
            
                'papertrail' => [
                    'driver' => 'monolog',
                    'level' => env('LOG_LEVEL', 'debug'),
                    'handler' => SyslogUdpHandler::class,
                    'handler_with' => [
                        'host' => env('PAPERTRAIL_URL'),
                        'port' => env('PAPERTRAIL_PORT'),
                    ],
                ],
            
                'stderr' => [
                    'driver' => 'monolog',
                    'handler' => StreamHandler::class,
                    'formatter' => env('LOG_STDERR_FORMATTER'),
                    'with' => [
                        'stream' => 'php://stderr',
                    ],
                ],
            
                'syslog' => [
                    'driver' => 'syslog',
                    'level' => env('LOG_LEVEL', 'debug'),
                ],
            
                'errorlog' => [
                    'driver' => 'errorlog',
                    'level' => env('LOG_LEVEL', 'debug'),
                ],
            
                'null' => [
                    'driver' => 'monolog',
                    'handler' => NullHandler::class,
                ],
            
                'emergency' => [
                    'path' => storage_path('logs/laravel.log'),
                ],
                
                //下記を追記
                'errors' => [
                    'driver' => 'daily',
                    'path' => storage_path('logs/error.log'),
                    'level' => 'error',
                    'days' => 360,
                ],
                //上記までを追記
            ],
        ];
        
  4. シンボリックリンクの作成(アプリケーションのローカルに画像をアップロードする)

    1. 下記コマンドを実行して.envファイルを開く。

      $ vi .env
      
    2. 下記の内容を追記する。

      アプリ名ディレクトリ/.env
      FILESYSTEM_DRIVER=public
      
    3. 下記コマンドを実行する。

      $ php artisan storage:link
      
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?