Symfony2使用querystring参数路由到控制器


Symfony2 routing to controllers using querystring parameters

我现在和一个客户住在Bizzaro World。

因为我们正在编写的symfony应用程序将响应注入到另一个应用程序的页面中,所以我们被迫只为该应用程序提供一个URL,但幸运的是,我们确实完好无损地获得了传递给我们的查询字符串。

因此,我需要做与symfony完全相反的事情,一种老式的MVC方法。我需要通过querystring参数进行路由,路由到正确的控制器并呈现正确的响应,而不是使用标准的、合理的路径方法。

因此url将是http://www.bizzaro.com/appname?route=%2fblog而不是http://www.bizzaro.com/appname/blog,依此类推。

路由部分中有一些很好的例子,说明如何确保将查询字符串作为参数传递给控制器操作,但不适用于这种相当回归的方法。

我从哪里开始

我实现了Corentin Dandoy在下面提出的解决方案2,但进行了轻微修改,以防止在查找根时出现循环。

namespace AppBundle'Controller;
use Symfony'Component'HttpFoundation'Request;
use Symfony'Component'Routing'Exception'ResourceNotFoundException;
use Symfony'Bundle'FrameworkBundle'Controller'Controller;
class FrontControllerControllerController extends Controller
{
    /**
     * Forward the request to the appropriate controller
     * @param Request $request
     * @return 'Symfony'Component'HttpFoundation'Response
     */
    public function indexAction(Request $request)
    {
        // get the parameter that specifies the route to the 'real' homepage controller
        $homeroute = $this->container->getParameter('homeroute');
        $route = $request->query->get('route');
        // Convert the query-string route into a valid path route
        $path = '/'.$route;
        // if it is the route, then use the 'real' homepage controller, otherwise you end up in a routing loop!
        if ($path === '/')
        {
            $match = $this->get('router')->match('/' . $homeroute);
        } else {
            try {
                $match = $this->get('router')->match($path);
            } catch (ResourceNotFoundException $e) {
                throw $this->createNotFoundException('The route does not exist');
            }
        }
        $params = array(
            'request' => $request,
        );
        return $this->forward($match['_controller'], $params);
    }
}

解决方案1

一个简单的解决方案(不可扩展(是:

namespace AppBundle'Controller;
use Symfony'Component'HttpFoundation'Request;
use Symfony'Component'HttpFoundation'Response;
use Sensio'Bundle'FrameworkExtraBundle'Configuration'Route;
class MainController
{
    /**
     * @Route("/app", name="bizarro_app")
     */
    public function mainAction(Request $request)
    {
        $route = $request->query->get('route');
        switch ($route) {
            case 'blog':
                return $this->blogAction($request);
            default:
                throw $this->createNotFoundException('The route does not exist');
        }
    }
    protected function blogAction(Request $request)
    {
        // Your blog page here
        return new Response('...');
    }
}

解决方案2

如果你不介意有两种可用的路线,你可以试试这个:

namespace AppBundle'Controller;
use Symfony'Component'HttpFoundation'Request;
use Symfony'Component'HttpFoundation'Response;
use Symfony'Component'Routing'Exception'ResourceNotFoundException;
use Sensio'Bundle'FrameworkExtraBundle'Configuration'Route;
class MainController
{
    /**
     * @Route("/app", name="bizarro_app")
     */
    public function mainAction(Request $request)
    {
        $route = $request->query->get('route');
        // Convert the query-string route into a valid path
        $path = '/'.$route;
        try {
            $match = $this->get('router')->match($path);
        } catch (ResourceNotFoundException $e) {
            throw $this->createNotFoundException('The route does not exist');
        }
        $params = array(
            'request' => $request,
        );
        return $this->forward($match['_controller'], $params);
    }
    /**
    * @Route("/blog", name="bizarro_blog")
    */
    public function blogAction(Request $request)
    {
        // Your blog page here
        return new Response('...');
    }
}

这样,您就可以从Sf2路由组件中获益。请注意,我自己没有测试过。