Create Laravel original config file and call it with controller
Sep 8, 2020
PHP
beginners
Laravel
for beginners
Laravel7
Purpose
- -Summary of how to add to Laravel’s configuration file and how to read the configuration file
-
- The configuration file in this explanation is not an .env file, but a file directly under the
app name directory / config
.
- The configuration file in this explanation is not an .env file, but a file directly under the
Implementation environment
- -Hardware 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 |
- -Software environment
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
- -The environment described in the implementation environment or an environment equivalent to it is prepared and the Laravel application is created.
Prerequisite information
- -The author confirms the operation of this article using the application created in the following article. -Creating a posting app using Laravel CRUD processing Part 7 Creating an inquiry form
- -The link below is the remote repository of the source code used in the verification when writing this article. -https://github.com/miriwo0104/laravel_crud/tree/config
Feeling after reading
- -You can add your own settings to the configuration file directly under the
app name directory / config
. - -The value described in the configuration file directly under
app name directory / config
can be read and displayed.
Overview
- Creation and description of configuration file
- Addition of routing information
- Creating and describing a controller file
- Creating and describing a view file
- Confirmation
#Details
-
Creation and description of configuration file
-
Execute the following command in the application name directory to create a configuration file.
$ vi config_test.php
-
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. ', ] ?>
-
-
Addition of routing information
-
Execute the following command in the application name directory to open the routing file.
$ vi routes / web.php
-
Add the following contents.
Route :: get ('/ config_check','ConfigCheckController @ config_check')-> name ('config_check');
-
-
Creating and describing a controller file
-
Execute the following command in the application name directory to create a controller file.
$ php artisan make: controller ConfigCheckController
-
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 } `````
-
-
Creating and describing a view file
-
Execute the following command in the application name directory to create a directory to store the view file.
$ mkdir resources / views / checks
-
Execute the following command in the application name directory to create a view file.
$ vi resources / views / checks / config_check.blade.php
-
Write the following contents in the opened view file.
{{$ str}}
-
-
Confirmation
-
Execute the following command in the application name directory to start the local server.
$ php artisan serve
-
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
-
As shown below, if the contents described in the setting file are displayed on the browser, the work is completed.
*image
-
#References