Laravel Configuration

Basic Configuration :-

  • After installing Laravel, the first thing we need to do is to set the write permission for the directory storage and bootstrap/cache.
  • Generate Application key to secure session and other encrypted data. If the root directory doesn’t contain the .env file then rename the .env.example to .env file and execute the following command where you have installed Laravel. The newly generated key can be seen in the .env file.
  • You can also configure the locale, time zone, etc. of the application in the config/app.php file.
  • Environment Configuration :-

    Environment variables are those which provide a list of web services to your web application. All the environment variables are declared in the .env file which includes the parameters required for initializing the configuration. By default, the .env file includes following parameters −

    APP_ENV = local
    APP_DEBUG = true
    APP_KEY = base64:ZPt2wmKE/X4eEhrzJU6XX4R93rCwYG8E2f8QUA7kGK8 =
    APP_URL = http://localhost
    DB_CONNECTION = mysql
    DB_HOST = 127.0.0.1
    DB_PORT = 3306
    DB_DATABASE = homestead
    DB_USERNAME = homestead
    DB_PASSWORD = secret
    CACHE_DRIVER = file
    SESSION_DRIVER = file
    QUEUE_DRIVER = sync
    REDIS_HOST = 127.0.0.1
    REDIS_PASSWORD = null
    REDIS_PORT = 6379
    MAIL_DRIVER = smtp
    MAIL_HOST = mailtrap.ioMAIL_PORT = 2525
    MAIL_USERNAME = null
    MAIL_PASSWORD = null
    MAIL_ENCRYPTION = null



    Important Points :-

    While working with basic configuration files of Laravel, the following points are to be noted −

  • The .env file should not be committed to the application source control, since each developer or user has some predefined environment configuration for the web application.
  • For backup options, the development team should include the .env.example file, which should contain the default configuration.


  • Retrieval of Environment Variables:-

    All the environment variables declared in the .env file can be accessed by env-helper functions which will call the respective parameter. These variables are also listed into $_ENV global variable whenever application receives a request from the user end. You can access the environment variable as shown below −

    'env' => env('APP_ENV', 'production'),

    env-helper functions are called in the app.php file included in the config folder. The above given example is calling for the basic local parameter.


    Accessing Configuration Values :-

    You can easily access the configuration values anywhere in the application using the global config helper function. In case if the configuration values are not initialized, default values are returned. For example, to set the default time zone, the following code is used −

    config(['app.timezone' => 'Asia/Kolkata']);


    Caching of Configuration:-

    To increase the performance and to boost the web application, it is important to cache all the configuration values. The command for caching the configuration values is −

    config:cache



    c:/xampp/htdocs/itechxpert>php artisan config:cache


    Maintenance Mode :-

    Sometimes you may need to update some configuration values or perform maintenance on your website. In such cases, keeping it in maintenance mode, makes it easier for you. Such web applications which are kept in maintenance mode, throw an exception namely MaintenanceModeException with a status code of 503. You can enable the maintenance mode on your Laravel web application using the following command −

    c:/xampp/htdocs/itechxpert>php artisan down


    Once you finish working on updates and other maintenance, you can disable the maintenance mode on your web application using following command −

    c:/xampp/htdocs/itechxpert>php artisan up



    Database Configuration :-

    The database of your application can be configured from config/database.php file. You can set configuration parameters that can be used by different databases and you can also set the default one to use.

    'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', 'localhost'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
    'strict' => false,
    'engine' => null,
    ],


    Naming the Application :-

    The App Directory, by default, is namespaced under App. To rename it, you can execute the following command and rename the namespace.

    php artisan app:name <name-of-your-application>
    Replace the <name-of-your-application> with the new name of your application that you want to give.


    Maintenance Mode

    We need to modify our website on a regular basis. The website needs to be put on maintenance mode for this. Laravel has made this job easier. There are two artisan commands which are used to start and stop the maintenance mode which are described below.

    Start Maintenance Mode
    To start the maintenance mode, simply execute the following command.

    php artisan down
    After successful execution, you will receive the following output −

    Output :-


    Stop Maintenance Mode

    After making changes to your website and to start it again, execute the following command.
    php artisan up
    After successful execution, you will receive the following output −

    Environment Variable Type

    In laravel database configuration .env files allow every variable to pass through it as a string. Therefore some reserved values have been generated to facilitate you to return a broader range of types from the env() function:

    If you require to define an environment variable including a value which incorporates spaces, you may do so by embedding the value in double quotes. A below-mentioned example will clear your doubts.

    APP_NAME="My Application"

    Retrieving Environment Configuration

    The entire variables that are placed or listed in this file will be loaded inside the $_ENV PHP super-global when your application acquires a request. Though, you may utilize the env helper to recover or retrieve values from these variables in your configuration files.

    ‘debug’ => env(‘APP_DEBUG’, false),



    The second value as you can see is a value passed to the “env” function is the “default value.” which will be utilized if no environment variable endures for the addressed key.


    .htaccess file

    File name : .htaccess

    copy the .htaccess content from public directory htaccess file. and paste the root .htaccess file

    <IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
    Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    </IfModule>


    Rename the server.php file to index.php

    File name : index.php

    Rename the server.php file to index.php

    assets file

    Directory name : public

    All assets file store into public directory. such as All css, Js, images etc.


    Set The .env file

    File name : index.php

    DB_DATABASE=mylara
    DB_USERNAME=root
    DB_PASSWORD=

    collective not found

    File name : index.php

    [Symfony\Component\Debug\Exception\FatalErrorException] Class 'Collective\Html\HtmlServiceProvider' not found

    then, go on composer

    c:/xampp/htdocs/mylara> composer require laravelcollective/html

    composer dump-autoload
    Then run:
    composer update






    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here


    Ittutorial