0

If I had the following code how would I get the calling method and the class in the provider:

class HelloServiceProvider implements ServiceProviderInterface {
    public function register(Application $app){
        $app['hello'] = $app->share(function () {
            // Get hello/indexAction
        });
    }

    public function boot(Application $app){}
}

class hello {
    public function addAction(){
        $app['hello']()
    }
}


$app->get('/hello', 'hello.controller:indexAction');

Is this even possible? Thanks

yllamiqyz
  • 1
  • 1
  • I think you can, check [```debug_backtrace``` php funcion documentation](http://php.net/manual/en/function.debug-backtrace.php) and also [this question](http://stackoverflow.com/questions/1214043/find-out-which-class-called-a-method-in-another-class). – mTorres Mar 04 '15 at 09:34
  • ahhh so there is nothing built into silex? That would allow me to get the method/class? – yllamiqyz Mar 04 '15 at 20:27
  • AFAIK no, there is not such a native Silex option. – mTorres Mar 05 '15 at 07:46

1 Answers1

0

Is it, indeed, possible but you need some changes:

<?php

// file HelloServiceProvider.php
class HelloServiceProvider implements ServiceProviderInterface {
    public function register(Application $app){
        $app['hello'] = $app->share(function () {
            // Get hello/indexAction
        });
    }

    public function boot(Application $app){}
}

// file Hello.php
class Hello {
    public function indexAction(Application $app){
        $app['hello']()
    }
}

// somewhere in your code:
$app->register(new Silex\Provider\ServiceControllerServiceProvider());
$app->register(new HelloServiceProvider());

$app['hello.controller'] = $app->share(function() {
    return new hello();
});

$app->get('/hello', 'hello.controller:indexAction');

Notice that the use statements are missing from code

You can get more information in the official documentation.

mTorres
  • 3,590
  • 2
  • 25
  • 36
  • that is my fault I did not include some of my code which was kinda stupid of me...my code works, everything fires as it should. I am just trying to figure out if there is a way to get the method and the class of the calling function. In your example you add the register function but dont show how to get the calling class in that $app['hello'] function. – yllamiqyz Mar 04 '15 at 08:11