How configure php symfony4 app inside site subfolder (routing problem)

冷暖自知 提交于 2019-12-11 09:58:12

问题


I need to configure a symfony app inside a subfolder of a hosted domain. The app registers some route, for example /hello.

The directory structure is:

/ (http root)
/myapp (symfony app)
/myapp/.htaccess (invisible redirect)
/myapp/public 
/myapp/src
/myapp/vendor
/myapp/...

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>

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

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.

Thanks.


回答1:


I got this error too few months ago and solved it.

Here is my thread about this same problem

Hope It can help you. If you have questions about my answers, fell free to ask.




回答2:


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

  • 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;
        }
    }

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...



来源:https://stackoverflow.com/questions/54402043/how-configure-php-symfony4-app-inside-site-subfolder-routing-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!