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

编程入门 行业动态 更新时间:2024-10-18 14:16:37
本文介绍了如何在站点子文件夹中配置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 带有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应用(路由问题)

本文发布于:2023-10-31 13:19:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546199.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   站点   文件   夹中   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!