Laravel Facades: When To Use Facades
When To Use Facades
Facades Vs. Dependency Injection
Use Illuminate\Support\Facades\Cache;
Route::get('/cache', function () {
return Cache::get('key');
}); Use Illuminate\Support\Facades\Cache;
/**
* A basic functional test example.
*
* @return void
*/
public function testBasicExample()
{
Cache::shouldReceive('get')
->with('key')
->andReturn('value');
$this->visit('/cache')
->see('value');
} Since facades use dynamic methods to proxy method calls to objects resolved from the service container, we actually can test facades just as we would test an injected class instance.
Facades Vs. Helper Functions
Use Illuminate\Support\Facades\Cache;
/**
* A basic functional test example.
*
* @return void
*/
public function testBasicExample()
{
Cache::shouldReceive('get')
->with('key')
->andReturn('value');
$this->visit('/cache')
->see('value');
} There is absolutely no practical difference between facades and helper functions.
Semantic portal