Create Laravel original config file and call it with controller

Sep 8, 2020 PHP beginners Laravel for beginners Laravel7

Purpose

Implementation environment

Items Information
OS macOS Catalina (10.15.5)
Hardware MacBook Pro (13-inch, 2020, Four Thunderbolt 3 ports)
Processor 2GHz Quad Core Intel Core i5
Memory 32 GB 3733 MHz LPDDR4
Graphics Intel Iris Plus Graphics 1536 MB
Items Information Remarks
PHP version 7.4.3 Introduced with Homwbrew
Laravel version 7.0.8 Introduced by this method using commposer → Building environment for Mac Laravel
MySQL version 8.0.19 for osx10.13 on x86_64 Install using this method using Homwbrew → Install MySQL with Mac Homebrew

Prerequisites

Prerequisite information

Feeling after reading

Overview

  1. Creation and description of configuration file
  2. Addition of routing information
  3. Creating and describing a controller file
  4. Creating and describing a view file
  5. Confirmation

#Details

  1. Creation and description of configuration file

    1. Execute the following command in the application name directory to create a configuration file.

      $ vi config_test.php
      
    2. Add the following contents to the opened file, save and close.

      <? php
       
      return [
      #String settings
          'config_str' =>'This is the string set in the config file. ',
      ]
      
      ?>
      
  2. Addition of routing information

    1. Execute the following command in the application name directory to open the routing file.

      $ vi routes / web.php
      
    2. Add the following contents.

      Route :: get ('/ config_check','ConfigCheckController @ config_check')-> name ('config_check');
      
  3. Creating and describing a controller file

    1. Execute the following command in the application name directory to create a controller file.

      $ php artisan make: controller ConfigCheckController
      
    2. Execute the following command in the application name directory to open the controller file you just created.

      $ vi app / Http / Controllers / ConfigCheckController.php
      

    Describe as follows.

     ```App name directory /app/Http/Controllers/ConfigCheckController.php
     <? php
        
     namespace App \ Http \ Controllers;
        
     use Illuminate \ Http \ Request;
     // Add the following
     use Illuminate \ Support \ Facades \ Config;
        
     class ConfigCheckController extends Controller
     {
         // Add the following
         public function config_check ()
         {
             // Process to store the character string of config_str defined in the config_test.php file that exists directly under the application name directory / config in $ str
             $ str = Config :: get ('config_test.config_str');
             return view ('checks.config_check', [
                 'str' => $ str,
             ]);
         }
         // Add the above
     }
     `````
    
  4. Creating and describing a view file

    1. Execute the following command in the application name directory to create a directory to store the view file.

      $ mkdir resources / views / checks
      
    2. Execute the following command in the application name directory to create a view file.

      $ vi resources / views / checks / config_check.blade.php
      
    3. Write the following contents in the opened view file.

      {{$ str}}
      
  5. Confirmation

    1. Execute the following command in the application name directory to start the local server.

      $ php artisan serve
      
    2. Access the following with a browser. (You may need to log in to access the following with the Laravel app that has the Auth authentication function.) -http://127.0.0.1:8000/config_check

    3. As shown below, if the contents described in the setting file are displayed on the browser, the work is completed.

      *image

#References