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

格兰尼

我目前与一位客户住在Bizzaro World。

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

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

因此,URL将为http://www.bizzaro.com/appname?route=%2fblogand not 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路由组件中受益。请注意,我自己尚未对其进行测试。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章