dse3168
2013-11-04 11:47
浏览 74
已采纳

Silex:路由变量赋值问题

I recently wrote a post about request forwarding in Silex, which used a blog example to explain sub requests in Silex.

I use a slightly modified version of this example for a domain controller. The path to the domain endpoint = /product/domain

A domain can also have a webhosting package attached to it. The url path for this endpoint would be /product/domain/(id)/webhosting/

You can fetch info about a webhosting package by using the url path. The url path for this endpoint would be /product/domain/(id)/webhosting/(id)

To handle these sub requests, I have a method called forwardRequest, which has no parameters in it's method signature, but uses func_get_args to keep it dynamic.

Unfortunately this doesn't work as Silex uses the named parameters in your route to call your method. So if you have /product/domain/domain_id/webhosting/webhosting_id, your method should have a signature of method($domain_id, $webhosting_id), which is a PITA if you want to forward multiple endpoints through one method. If you have additional /product/domain/domain_id/emailhosting and /product/domain/domain_id/dns endpoints, you have to create a method for each in order to forward the request.

Does anyone have a solution in which I can use only 1 method to forward all these sub requests?

Note: I'm using PHP 5.3.

图片转代码服务由CSDN问答提供 功能建议

我最近写了一篇关于在Silex中请求转发,它使用博客示例解释Silex中的子请求。

我对域控制器使用此示例的略微修改版本 。 域端点的路径= / product / domain

域还可以附加一个虚拟主机包。 此端点的URL路径为/ product / domain / (id)/ webhosting /

您可以使用网址路径获取有关网站托管包的信息。 此端点的网址路径为/ product / domain /(id)/ webhosting /(id)

为了处理这些子请求,我有一个名为forwardRequest的方法,它在方法签名中没有参数,但使用func_get_args使其保持动态。 \ n

不幸的是,这不起作用,因为Silex使用路线中的命名参数来调用您的方法。 因此,如果你有/ product / domain / domain_id / webhosting / webhosting_id,你的方法应该有一个方法签名($ domain_id,$ webhosting_id),如果你想通过一种方法转发多个端点,那么这是一个PITA。 如果您有其他/ product / domain / domain_id / emailhosting和/ product / domain / domain_id / dns端点,则必须为每个端点创建一个方法以转发请求。

任何人都有一个解决方案,我只能用一种方法转发所有这些子请求?

注意:我使用的是PHP 5.3。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dragonsun00000 2013-11-04 12:11
    已采纳

    The part of silex that decides which arguments to pass to the controller is called the "controller resolver". The default controller resolver uses reflection. You can override the controller_resolver service with a custom implementation though.

    Defining a custom controller resolver that wraps the existing one but replaces the arguments with a single one, the request:

    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
    
    class RequestArgumentControllerResolver implements ControllerResolverInterface
    {
        protected $resolver;
    
        public function __construct(ControllerResolverInterface $resolver)
        {
            $this->resolver = $resolver;
        }
    
        public function getController(Request $request)
        {
            return $this->resolver->getController($request, $controller);
        }
    
        public function getArguments(Request $request, $controller)
        {
            return [$request];
        }
    }
    

    Extend the existing controller resolver with the newly defined decorator:

    $app['controller_resolver'] = $app->share($app->extend('controller_resolver', function ($resolver, $app) {
        return new RequestArgumentControllerResolver($resolver);
    }));
    

    Note: This is just one way of doing it. You don't have to decorate, you can also replace the resolver completely if you like. And obviously this is just a very basic example of only passing a single arg to the controller, you can do something more sophisticated.

    点赞 评论

相关推荐 更多相似问题