一个简单的路由规则在 Symfony4 中不起作用

A simple routing rule is not working in Symfony4

我试图在 Symfony4 中使用 Apache 作为网络服务器创建一个简单的路由规则,但它无法正常工作。

我能看到主要/规则,但看不到任何人。

我已经按照这些命令安装了我的项目:

composer create-project symfony/skeleton s4-02-routing 
composer require symfony/apache-pack

这是我的routes.yaml文件...

app_lucky_number:
    path: /lucky/number
    controller: App\Controller\LuckyController::number

这是控制器...

<?php

// src/Controller/LuckyController.php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;

class LuckyController {

    public function number() {
        $number = random_int(0, 100);

        return new Response(
                '<html><body>Lucky number: ' . $number . '</body></html>'
        );
    }

}

.htaccess 文件存在,这是虚拟主机配置:

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        <Directory /var/www/html>
            AllowOverride All
#           Order Allow,Deny
            Require all granted
            Allow from All
        </Directory>

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

主要的 URL http://192.168.2.12/s4-02-routing/public/ 工作正常,因为我可以看到:

Welcome to Symfony 4.3.2 Your application is now ready. You can start working on it at: /var/www/html/s4-02-routing/

但是,URL http://192.168.2.12/s4-02-routing/public/lucky/number 不起作用,因为我看到一个 symfony 错误:

Sorry, the page you are looking for could not be found. (2/2) NotFoundHttpException No route found for "GET /lucky/number"

但是,规则创建得很好:

vagrant@06f2a172f274:/var/www/html/s4-02-routing$ php bin/console debug:router
 ------------------ -------- -------- ------ ---------------
  Name               Method   Scheme   Host   Path
 ------------------ -------- -------- ------ ---------------
  index              ANY      ANY      ANY    /
  app_lucky_number   ANY      ANY      ANY    /lucky/number

怎么了?谢谢你的帮助。

我找到了解决问题的方法:清理缓存...

php bin/console cache:clear