Question

I want to pass $this from my Controller to my Service Provider so I can use it with my custom class. That way, I can reference back to my controller and output the success.

Controller

function Foo(){

    //$this is defined
    MyFacade::Bar($input);

}

function success(){

     return var_dump('Succes!');

}

Service Provider

<?php namespace App\Libraries\Extensions\Filesystem;

use Illuminate\Support\ServiceProvider as  IlluminateServiceProvider;
use App\Libraries\Extensions\Filesystem\Filesystem;

class FilesystemServiceProvider extends IlluminateServiceProvider {

   public function register()
        { 
            $this->app->bind('files', function(){

               return new Filesystem($this);

            });    
    }
}

Custom Class

<?php namespace App\Libraries\Extensions\Filesystem;

use \Illuminate\Filesystem\Filesystem as IlluminateFilesystem;

class Filesystem extends IlluminateFilesystem {

protected $listener;

    public function __construct($listener)
    {

        $this->listener = $listener;

    }

   function Bar($input){
     //Some code
     return $this->listener->Success();
   }

With this code I'm getting Call to undefined method App\Libraries\Extensions\Filesystem\FilesystemServiceProvider::Success().

I believe that my Service Provider $this is passed instead of my Controller $this.

How can I pass my Controller $this to my Service Provider so I can just call MyFacade::Bar($input)?

Was it helpful?

Solution

I want to pass $this from my Controller to my Service Provider so I can use it with my custom class. That way, I can reference back to my controller and output the success.

That is a terrible idea, and you'll have trouble making it work anyway.

Your Filesystem class should have no knowledge of your controller class. Passing in $this and trying to get File to do your controller's work is not the way to do it. It should just do its 'file' stuff, and then return an appropriate response.

Controller

function Foo(){

    if (MyFacade::Bar($input))
    { 
        // Success
    }
    else
    {
        // Fail
    }

}

Custom Class

function Bar($input){
     //Some code
     return true; // or false
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top