如何在站点子文件夹中配置php symfony4应用(路由问题)

时间:2019-01-28 12:27:38

标签: php routing symfony4 subdirectory

我需要在托管域的子文件夹内配置一个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也具有该前缀。

谢谢。

2 个答案:

答案 0 :(得分:0)

几个月前我就收到了此错误,并已解决。

Here is my thread about this same problem

希望它可以为您提供帮助。 如果您对我的答案有疑问,请随意提问。

答案 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%

我必须进行更多测试...

相关问题