从silex中的服务提供者获取调用类/方法

时间:2015-03-03 20:07:21

标签: php silex

如果我有以下代码,我将如何在提供程序中获取调用方法和类:

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');

这甚至可能吗?感谢

1 个答案:

答案 0 :(得分:0)

确实可以,但你需要做一些改变:

<?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');

请注意,代码中缺少use语句

您可以在official documentation

中获取更多信息