【问题标题】:Changing baseURL in symfony of all controllers在所有控制器的 symfony 中更改 baseURL
【发布时间】:2018-10-20 17:16:19
【问题描述】:

所以我将我的应用程序托管在 apache 服务器上 /~test/ 下的子目录中 我尝试在路由中添加前缀而没有发生任何事情

我希望所有控制器的所有路由都以/~test/ 开头,而无需我手动将其添加到每个路由。现在,当一条路线到达 / 时,它会超出我的 /~test/ 目录

例如:

  /**
     * @Route("/login", name="app_login")
     */
    public function login(AuthenticationUtils $authenticationUtils): Response
    {
    }

这条路线我想去/~test/login

我的 .htaccess:

DirectoryIndex index.php

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    RewriteCond %{HTTP:Authorization} .
    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]

    RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 307 ^/$ /index.php/
    </IfModule>
</IfModule>

【问题讨论】:

  • 将 SetEnv BASE "/test" 添加到 .htaccess
  • @mblaettermann 试过了,把它放在 htaccess 的顶部没有结果,这可能是因为 apache 设置我不知道它是否允许覆盖我无权访问根配置
  • 你的 Symfony 版本?同时复制粘贴您的主要网址
  • @Trix symfony 4.1 我的网址在 Intranet 上基本上是 uni.co.uk/~myusername/
  • 你想要你的 symfony 应用程序主页在http://uni.co.uk/~myusername/test/ 吗?还是别的什么?

标签: apache symfony routing


【解决方案1】:

看那个https://symfony.com/blog/new-in-symfony-4-1-prefix-imported-route-names

你可以在你的 routes.yaml 文件中写这样的东西:

controllers:
    resource: ../src/Controller/
    type: annotation
    prefix: /cool-prefix

【讨论】:

    【解决方案2】:
    1. 我的应用程序位于 /~test/ 下 apache 服务器上的子目录中

    2. 测试是一个例子,我实际上是指〜用户名

    3. 主页将是 uni.co.uk/~myusername/index.php

    把它放在你的应用程序根目录的.htaccess:

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteRule ^$ /public/index.php [L]
    </IfModule>
    

    【讨论】:

      猜你喜欢
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      • 2018-09-09
      • 2016-12-12
      • 2012-11-02
      • 2013-05-08
      相关资源
      最近更新 更多