PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 20, 2022

[FIXED] Laravel 5: Remove development dependancies from app config file

 January 20, 2022     composer-php, deployment, git, laravel     No comments   

Issue

I'm deploying my Laravel's projects with git clone and updating with git pull

It works fine, but each time I deploy, I had to remove the development dependancies from config/app.php providers array and aliases array which is annoying and each time I do composer install --no-dev , it shows me errors because he didn't found the dev packages.


Solution

You can conditionally load service providers and facades based on the environment in laravel 5.

Service Providers

Service Providers need to be registered in /app/Providers/AppServiceProvider.php rather than config/app.php.

// AppServiceProvider.php

public function register()
{
    $this->app->bind(
        'Illuminate\Contracts\Auth\Registrar',
        'App\Services\Registrar'
    );

    if ($this->app->environment('production')) {
        $this->app->register('App\Providers\ProductionProvidersGoHere');
    } else {
        $this->app->register('App\Providers\DevelopmentProvidersGoHere');
    }
}

For a tutorial have a look here: https://mattstauffer.co/blog/conditionally-loading-service-providers-in-laravel-5

Facades

Facades can be conditionally loaded with the AliasLoader.

/**
 * List of only Local Enviroment Facade Aliases
 * @var array
 */
protected $facadeAliases = [
    'Debugbar' => 'Barryvdh\Debugbar\Facade',
];

/**
 * Load additional Aliases
 * Base file Alias load is /config/app.php => aliases
 */
public function registerFacadeAliases()
{
    $loader = AliasLoader::getInstance();
    foreach ($this->facadeAliases as $alias => $facade)
    {
        $loader->alias($alias, $facade);
    }
}

Here is a nice tutorial for this too: http://blog.piotrows.pl/en/laravel-5-load-serviceprovider-depend-on-env-file/

// Edit
added facades: Thanks to @Sn0opr for pointing me into this.



Answered By - Pᴇʜ
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing