简体   繁体   English

带有注释的 Symfony 5 路由不适用于 Apache 和 Docker

[英]Symfony 5 routing with annotations doesn't work with Apache and Docker

I'm trying to make a Symfony 5 app running in a Docker (version 19.03.8) with Apache.我正在尝试使用 Apache 制作在 Docker(版本 19.03.8)中运行的 Symfony 5 应用程序。

I have an issue with the routing of the applications using annotations, Symfony always returns the error Uncaught PHP Exception Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException: "No route found for "GET /test2"" at /app/vendor/symfony/http-kernel/EventListener/RouterListener.php line 136我在使用注释路由应用程序时遇到问题,Symfony 总是返回错误Uncaught PHP Exception Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException: "No route found for "GET /test2"" at /app/vendor/symfony/http-kernel/EventListener/RouterListener.php line 136

It seems to be environment related because it works properly when I use the symfony local web server and when I use a xampp installation in windows.它似乎与环境有关,因为当我使用 symfony 本地 Web 服务器以及在 Windows 中使用 xampp 安装时,它可以正常工作。

A really weird thing is that I only get the error if I use the annotations, if I configure my routes with the routes.yaml file it works fine.一个非常奇怪的事情是,如果我使用注释,我只会得到错误,如果我使用routes.yaml文件配置我的路由它工作正常。

I installed the annotations with composer require annotations and the symfony apache-pack with composer require symfony/apache-pack , which created the .htaccess file in my public folder我用composer require annotations安装了注释,用composer require symfony/apache-pack ,它在我的公共文件夹中创建了.htaccess文件

# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex index.php

# By default, Apache does not evaluate symbolic links if you did not enable this
# feature in your server configuration. Uncomment the following line if you
# install assets as symlinks or if you experience problems related to symlinks
# when compiling LESS/Sass/CoffeScript assets.
# Options FollowSymlinks

# Disabling MultiViews prevents unwanted negotiation, e.g. "/index" should not resolve
# to the front controller "/index.php" but be rewritten to "/index.php/index".
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Determine the RewriteBase automatically and set it as environment variable.
    # If you are using Apache aliases to do mass virtual hosting or installed the
    # project in a subdirectory, the base path will be prepended to allow proper
    # resolution of the index.php file and to redirect to the correct URI. It will
    # work in environments without path prefix as well, providing a safe, one-size
    # fits all solution. But as you do not need it in this case, you can comment
    # the following 2 lines to eliminate the overhead.
    RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
    RewriteRule .* - [E=BASE:%1]

    # Sets the HTTP_AUTHORIZATION header removed by Apache
    RewriteCond %{HTTP:Authorization} .+
    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]

    # Redirect to URI without front controller to prevent duplicate content
    # (with and without `/index.php`). Only do this redirect on the initial
    # rewrite by Apache and not on subsequent cycles. Otherwise we would get an
    # endless redirect loop (request -> rewrite to front controller ->
    # redirect -> request -> ...).
    # So in case you get a "too many redirects" error or you always get redirected
    # to the start page because your Apache does not expose the REDIRECT_STATUS
    # environment variable, you have 2 choices:
    # - disable this feature by commenting the following 2 lines or
    # - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
    #   following RewriteCond (best solution)
    RewriteCond %{ENV:REDIRECT_STATUS} =""
    RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    # Rewrite all other queries to the front controller.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the start page to the front controller explicitly so that the website
        # and the generated links can still be used.
        RedirectMatch 307 ^/$ /index.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>

This is the content of my controller:这是我的控制器的内容:

<?php declare(strict_types=1);

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class TestController {

    /**
     * @Route("/test2", name="test2")
     */
    public function test2() {

        return new Response('{"hehe": "working?"}', Response::HTTP_OK, ['content-type' => 'application/json']);
    }
}

This is the content of my annotations.yml这是我的 annotations.yml 的内容

controllers:
    resource: ../../src/Controller/
    type: annotation

kernel:
    resource: ../../src/Kernel.php
    type: annotation

This is my Dockerfile:这是我的 Dockerfile:

FROM php:7.4-apache

ENV COMPOSER_ALLOW_SUPERUSER=1

EXPOSE 80
WORKDIR /app/public

RUN apt-get update && \
    apt-get install -y libpq-dev zip unzip && \
    docker-php-ext-install pdo pdo_pgsql && \
    curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

ADD conf/php.ini /usr/local/etc/php/conf.d/app.ini

RUN a2enmod rewrite
ADD conf/vhost.conf /etc/apache2/sites-available/000-default.conf
ADD conf/apache.conf /etc/apache2/conf-available/z-app.conf
RUN a2enconf z-app
RUN chmod -R 755 /app

This is my apache.conf:这是我的 apache.conf:

DocumentRoot "/app/public"
<Directory "/app/public">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted
</Directory>

This is my vhost.conf:这是我的 vhost.conf:

<VirtualHost *:80>
    DocumentRoot "/app/public"
    <Directory "/app/public">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

I tried to clear the cache with php bin/console cache:clear我试图用php bin/console cache:clear

EDIT: Inside the container the route isn't listed, but if I run php bin/console debug:router outside of the container it works, I have no idea why...编辑:在容器内没有列出路由,但是如果我在容器外运行php bin/console debug:router它可以工作,我不知道为什么......

I finally found the issue which was a bug in virtualbox shared folders.我终于找到了问题,这是 virtualbox 共享文件夹中的错误。 My setup is a Windows 7 host with a linux VM that runs the container.我的设置是带有运行容器的 Linux VM 的 Windows 7 主机。

composer install wasn't working properly because of the shared folders situation (more information https://github.com/laravel/homestead/issues/1240 https://github.com/kylekatarnls/update-helper/issues/3 )由于共享文件夹的情况, composer install无法正常工作(更多信息https://github.com/laravel/homestead/issues/1240 https://github.com/kylekatarnls/update-helper/issues/3

I had version 5.XX of VirtualBox, I upgraded to 6.0.18 and now it's working fine我有 5.XX 版的 VirtualBox,我升级到 6.0.18,现在它工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM