如何在站点子文件夹中配置php symfony4应用(路由问题) [英] How configure php symfony4 app inside site subfolder (routing problem)

查看:90
本文介绍了如何在站点子文件夹中配置php symfony4应用(路由问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在托管域的子文件夹内配置一个symfony应用。
该应用程序注册了一些路由,例如 / hello

I need to configure a symfony app inside a subfolder of a hosted domain. The app registers some route, for example /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 /...

With Apache mod-rewrite I redirect internally all urls from www.mysite.test/myapp/... to www.mysite.test/myapp/public/....

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteRule ^(.*)$ myapp/public/$1 [L]
</IfModule>

然后,当我浏览url www.mysite.test / myapp / hello时该应用不会返回 myapp / hello 的路线。

Then when I browse the url www.mysite.test/myapp/hello the app return no route for myapp/hello.

有一种方法可以使用自定义前缀安装所有路由来解决此问题?通过这种方式,控制器可以处理所有带有 / myapp 前缀的url,并且route生成的url也具有该前缀。

There is a way to mount all routes with a custom prefix to solve this problem? In this way the controllers can handle all the url with the /myapp prefix and also the url generated by route has this prefix.

谢谢。

推荐答案

也许我找到了未找到路由错误的解决方案。

maybe I found a solution to the route not found error.


  • 我在 service.yaml app.web_root_prefix c>带有url前缀。

  • 我在 Kernel.php
  • $上更改Kernel :: loadRoutes的定义b $ b
  • I define a parameter app.web_root_prefix in service.yaml with the url prefix.
  • I change the definition of Kernel::loadRoutes on Kernel.php:
    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%

In this mode all routes loaded from a .yaml file are prefixed correctly. Also you must change all paths in security.yaml inserting the prefix %app.web_root_prefix%.

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

I must make more test...

这篇关于如何在站点子文件夹中配置php symfony4应用(路由问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆