我需要在托管域的子文件夹内配置一个symfony应用。
该应用程序注册了一些路线,例如/hello
。
目录结构为:
/ (http root)
/myapp (symfony app)
/myapp/.htaccess (invisible redirect)
/myapp/public
/myapp/src
/myapp/vendor
/myapp/...
使用Apache mod-rewrite,我将所有URL从www.mysite.test/myapp/...
重定向到www.mysite.test/myapp/public/...
。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ myapp/public/$1 [L]
</IfModule>
然后,当我浏览网址www.mysite.test/myapp/hello
时,应用程序不会返回myapp/hello
的路由。
有一种方法可以用自定义前缀挂载所有路由来解决此问题?这样,控制器就可以处理所有带有/myapp
前缀的URL,并且route生成的URL也具有该前缀。
谢谢。
答案 0 :(得分:0)
答案 1 :(得分:0)
也许我找到了“找不到路由”错误的解决方案。
app.web_root_prefix
中定义了带有网址前缀的参数service.yaml
。Kernel.php
上更改Kernel :: loadRoutes的定义: class Kernel extends BaseKernel
{
use MicroKernelTrait {
loadRoutes as _loadRoutes;
}
// [omitted code]
public function loadRoutes(LoaderInterface $loader)
{
$r = $this->_loadRoutes($loader);
$prefix = $this->getContainer()->getParameter('app.web_root_prefix');
if (!empty($prefix)) {
/** @var Route $route */
foreach ($r->getIterator() as $route) {
$path = $route->getPath();
if (strpos($path, $prefix) !== 0 && empty(array_diff($route->getSchemes(), array('http', 'https', 'ftp')))) {
// Add the prefix only if it is not already there.
$route->setPath('/'.$prefix.$path);
}
}
// $r->addPrefix($prefix);
}
return $r;
}
}
在此模式下,将从.yaml文件加载的所有路由正确加上前缀。
另外,您必须更改security.yaml中的所有路径,并插入前缀%app.web_root_prefix%
。
我必须进行更多测试...