I would like to override the method get() of the class Illuminate\Cache\Repository as :
<?php
namespace App\Illuminate\Cache;
use Illuminate\Cache\Repository as BaseRepository;
class Repository extends BaseRepository{
public function get($key)
{
// changes
}
}
But I don't know how to tell Laravel to load my class instead of the original one.
is there any way to do this ?
edit 1
I have created a macro() but it only works if the method does not exist in the BaseRepository, for example:
this dosnt work
use Illuminate\Cache;
Cache\Repository::macro('get',function (){
return 'hi';
});
However, this worked:
use Illuminate\Cache;
Cache\Repository::macro('newName',function (){
return 'hi';
});
so macro can't do it because Laravel::macro() is creating a new function but doesn't override
When you are making new cache objects it's easy to make an instance from your class, not the BaseRepository class.
But when Laravel's service container is building the object (Or with dependency injection) you have to bind your extended class as the main class in the appServiceProvider.
But you have to pass an implementation of \Illuminate\Contracts\Cache\Store to the Repository's constructor.