drtoaamk20278 2014-11-27 06:50
浏览 52
已采纳

Symfony2如何将URI的参数传递给控制器​​Action方法?

I have started learning Symfony2. I came across a doubt: if I have this route:

# app/config/routing.yml
hello:
    path:      /hello/{name}
    defaults:  { _controller: AcmeHelloBundle:Hello:index }

And this controller:

// src/Acme/HelloBundle/Controller/HelloController.php
namespace Acme\HelloBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloController
{
    public function indexAction($name)
    {
        return new Response('<html><body>Ciao '.$name.'!</body></html>');
    }
}

Internally Symfony2 (inside app/bootstrap.php.cache) calls the call user_func_array() PHP built-in function:

$arguments = $this->resolver->getArguments($request, $controller);
$response = call_user_func_array($controller, $arguments);

And the call to the getArguments() method returns an array of arguments to pass to the action method. But if the controller were:

// src/Acme/HelloBundle/Controller/HelloController.php
namespace Acme\HelloBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloController
{
    public function indexAction($n)
    {
        return new Response('<html><body>Ciao '.$n.'!</body></html>');
    }
}

Symfony would have complained with a RuntimeException because no param $n is set.

My question is: how Symfony controls this behaviour, I mean, if a route has a {name} param, why the controller must have an action method with a $name parameter and the parameter must be called $name?

Cause in plain PHP, this would work:

$name = 'Alex';
function indexAction($n) {
   echo $n;
}
$f = 'indexAction';
$arguments = array($name);
call_user_func_array($f, $arguments);

Even if the functions signature accepts a param named as $n and not as $name.

I hope that this question is understandable, if not, please tell me and I will make an edit.

Thanks for the attention!

  • 写回答

3条回答 默认 最新

  • doumengbai2031 2014-11-27 08:15
    关注

    It's all done in the Controller Resolver HttpKernel/Controller/ControllerResolver.php via getArguments() & doArguments().

    For a better understanding, you will find what you need in Getting The Controller Arguments


    Edit: Answer comment.

    Does Symfony use the ReflectionParameter class internally in order to keep track of the params of the method's signature and match them with the route params?

    Yes, the ControllerResolver uses:

    Here is how:

     public function getArguments(Request $request, $controller)
        {
            if (is_array($controller)) {
                $r = new \ReflectionMethod($controller[0], $controller[1]);
            } elseif (is_object($controller) && !$controller instanceof \Closure) {
                $r = new \ReflectionObject($controller);
                $r = $r->getMethod('__invoke');
            } else {
                $r = new \ReflectionFunction($controller);
            }
            return $this->doGetArguments($request, $controller, $r->getParameters());
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?