namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Comment extends Facade
{
protected static function getFacadeAccessor(): string
{
return 'commentaccessor';
}
}
namespace App;
class Comment
{
public function create()
{
echo "App\Comment\create()";
return $this;
}
public function delete()
{
echo "App\Comment\delete()";
return $this;
}
}
# ServiceProvider's register(), just treat this as executed before our code
public function register(): void
{
$this->app->singleton('commentaccessor', function ($app) {
return new \App\Comment();
});
}
# elsewhere in our code
App\Facades\Comment::create() // will output "App\Comment\create()"
The request is for App\Facades\Comment::create()
to link back to App\Comment::create()
, note the 2 classes may not always share the same name. The code wires up the 2 classes using the "commentaccessor" registered using the singleton
method. I can make the facade and class use different names if it helps.
It would be great if chained methods work as well, i.e. being able to link App\Facades\Comment::create()->delete()
back to App\Comment::delete()