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

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.

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题