Php Phalcon 3.4:在控制器操作中注入组件,而不使用dispatcher事件

Php Phalcon 3.4:在控制器操作中注入组件,而不使用dispatcher事件,php,phalcon,Php,Phalcon,我尝试使用laravel方法在控制器操作中注入组件,请参见laravel中的示例: class UserController extends Controller { /** * Store a new user. * * @param Request $request * @return Response */ public function store(Request $request) { $n

我尝试使用laravel方法在控制器操作中注入组件,请参见laravel中的示例:

class UserController extends Controller {

    /**
     * Store a new user.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        $name = $request->input('name');

        //
    }
}
他们使用一种叫做“服务容器”的东西来解析操作参数,这在Phalcon中适用吗

我确实试过用手工制作,但没有成功


我需要此功能,以避免在Phalcon DI中将每个组件定义为服务。

我提供了Phalcon文档中描述的解决方案,但进行了一些调整以满足我的需要

我提供的解决方案是将新的dispatch event listener添加到服务容器中,以处理操作依赖性,例如:

$di->setShared('dispatcher', function() {
    $evManager->attach("dispatch:beforeDispatch", function (Event $event, Dispatcher $dispatcher) {
        try {
            $methodReflection = new ReflectionMethod(
                $dispatcher->getControllerClass(),
                $dispatcher->getActiveMethod()
            );
            foreach ($methodReflection->getParameters() as $parameter) {
                $parameterClass = $parameter->getClass();
                if ($parameterClass instanceof ReflectionClass) {
                    $dispatcher->setParam($parameter->name, new $parameterClass->name);
                }
            }
        } catch (Exception $exception) {
            throw new \Exception('', Dispatcher::EXCEPTION_HANDLER_NOT_FOUND);
        }
    });
    $dispatcher = new Dispatcher();
    $dispatcher->setEventsManager($evManager);
    return $dispatcher;
}
每个框架都有自己的机制,所以如果需要制作一些特殊的东西,就必须手动制作