我目前和一个客户一起生活在Bizzaro世界中。
因为我们正在编写的Symfony应用程序将响应注入到另一个应用程序中的页面中,我们被迫只能拥有一个应用程序的单个URL,但幸运的是我们可以完整地获取查询字符串。
因此,我需要做与Symfony相反的事情,一种老派的MVC方法。 我需要通过查询字符串参数进行路由,路由到正确的控制器并呈现正确的响应,而不是使用标准且合理的路径方法。
所以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);
}
}