在 apache 上带有错误路由的 Symfony

Symfony with buggy routing on apache

我在新的 Debian 9 服务器上安装了 Symfony。

我创建了一个项目:

composer create-project symfony/website-skeleton symfony

(masch3是我开发机器的名字)

打开时http:///masch3/symfony/public/

我得到了众所周知的 Symfony 欢迎页面。

当我创建一个名为“home”的最简单的控制器时

class HomeController extends AbstractController
{
    #[Route('/home', name: 'home')]
    public function index(): Response
    {
        return $this->render('home/index.html.twig', [
            'controller_name' => 'HomeController',
        ]);
    }
}

http:///masch3/symfony/public/home -> 404 未找到

http:///masch3/symfony/public/index.php/home -> 确定

按照其他答案的建议,我安装了 Apache 包

composer require symfony/apache-pack

但是这样还是不行

php bin/console debug:router

 -------------------------- -------- -------- ------ -----------------------------------
  Name                       Method   Scheme   Host   Path
 -------------------------- -------- -------- ------ -----------------------------------
  _wdt                       ANY      ANY      ANY    /_wdt/{token}
  _profiler_home             ANY      ANY      ANY    /_profiler/
  _profiler_search           ANY      ANY      ANY    /_profiler/search
  _profiler_search_bar       ANY      ANY      ANY    /_profiler/search_bar
  _profiler_phpinfo          ANY      ANY      ANY    /_profiler/phpinfo
  _profiler_search_results   ANY      ANY      ANY    /_profiler/{token}/search/results
  _profiler_open_file        ANY      ANY      ANY    /_profiler/open
  _profiler                  ANY      ANY      ANY    /_profiler/{token}
  _profiler_router           ANY      ANY      ANY    /_profiler/{token}/router
  _profiler_exception        ANY      ANY      ANY    /_profiler/{token}/exception
  _profiler_exception_css    ANY      ANY      ANY    /_profiler/{token}/exception.css
  _preview_error             ANY      ANY      ANY    /_error/{code}.{_format}
  home                       ANY      ANY      ANY    /home
 -------------------------- -------- -------- ------ -----------------------------------

php bin/console about

-------------------- -------------------------------------------
  Symfony
 -------------------- -------------------------------------------
  Version              6.0.2
  Long-Term Support    No
  End of maintenance   07/2022 (in +194 days)
  End of life          07/2022 (in +194 days)
 -------------------- -------------------------------------------
  Kernel
 -------------------- -------------------------------------------
  Type                 App\Kernel
  Environment          dev
  Debug                true
  Charset              UTF-8
  Cache directory      ./var/cache/dev (6.8 MiB)
  Build directory      ./var/cache/dev (6.8 MiB)
  Log directory        ./var/log (19 KiB)
 -------------------- -------------------------------------------
  PHP
 -------------------- -------------------------------------------
  Version              8.0.14
  Architecture         64 bits
  Intl locale          de_DE
  Timezone             Europe/Berlin (2022-01-18T13:45:22+01:00)
  OPcache              true
  APCu                 false
  Xdebug               false
 -------------------- -------------------------------------------

编辑主 Apache 配置文件:

sudo nano /etc/apache2/apache2.conf

查找此部分:

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

AllowOverride None 替换为 AllowOverride All

激活apache的重写模块。

sudo a2enmod rewrite
sudo systemctl restart apache2

转到您的 Symfony 项目的文件夹并安装 Apache 包

cd /var/www/html/symfony
composer require symfony/apache-pack

现在:

http:///masch3/symfony/public/home -> 确定