路由不在Symfony 3.4中工作

sca*_*del 2 symfony symfony-3.4

我使用以下方法创建了一个新的Symfony 3.4项目:

composer create-project symfony/skeleton my-project
Run Code Online (Sandbox Code Playgroud)

之后我添加了以下组件:

composer require twig
composer require annotations
composer require maker
Run Code Online (Sandbox Code Playgroud)

并创建了一个控制器:

php bin/console make:controller
Run Code Online (Sandbox Code Playgroud)

我添加了一个"合法"路线的动作.这是DefaultController:

<?php

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function index()
    {
        return $this->render('index.html.twig', [
            'controller_name' => 'DefaultController',
        ]);
    }

    /**
     * @Route("/legal", name="legal")
     */
    public function legal()
    {
        return $this->render('legal.html.twig', []);
    }
}
Run Code Online (Sandbox Code Playgroud)

文件config/routes.yaml:

#index:
#    path: /
#    defaults: { _controller: 'App\Controller\DefaultController::index' }
Run Code Online (Sandbox Code Playgroud)

和config/routes/annotations.yaml:

controllers:
    resource: ../../src/Controller/
    type: annotation
Run Code Online (Sandbox Code Playgroud)

当我访问主页时没问题,页面正在显示.但是当我尝试/ legal页面时,我有一个404:

找不到 - 在此服务器上找不到请求的URL/legal.

php bin/console debug:router 显示预期:

 ------------------ -------- -------- ------ -------------------------- 
  Name               Method   Scheme   Host   Path                      
 ------------------ -------- -------- ------ -------------------------- 
  homepage           ANY      ANY      ANY    /                         
  legal              ANY      ANY      ANY    /legal                    
  _twig_error_test   ANY      ANY      ANY    /_error/{code}.{_format}  
 ------------------ -------- -------- ------ -------------------------- 
Run Code Online (Sandbox Code Playgroud)

我使用console命令清除了缓存,并删除了var/cache目录的内容.但仍然是404.

我是3.4的新手.有任何想法吗 ?谢谢...

sca*_*del 7

好吧,正如@Basel Issmail所指出的那样,Symfony/Flex并没有.htaccess像以前的Symfony安装程序那样创建,我忘记了它.

我只有一个最小的Apache配置文件:

<VirtualHost *:80>
    DocumentRoot /path/to/my-project/public
    ServerName myproject.localhost

    <Directory /path/to/my-project/public>
        Options -Indexes +FollowSymLinks -MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

所以我创建了.htaccess文件/public/(index.php文件所在的位置),所需的最小配置如下:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Determine the RewriteBase automatically and set it as environment variable.
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule .? %{ENV:BASE}/index.php [L]
</IfModule>
Run Code Online (Sandbox Code Playgroud)

缺少的部分是将所有查询(除资产等现有文件除外)重写index.php为Symfony以处理路由.

---编辑---

您也可以使用symfony flex配方,而不是手动创建.htaccess:

composer require apache-pack
Run Code Online (Sandbox Code Playgroud)

这将安装此.htacess文件.


Bas*_*ail 5

听起来你需要用apache设置你的RewriteBase,因为问题不在 /

也许.htaccess文件甚至丢失了,因为您使用的是 Symfony/flex,并且它不会像以前的 Symfony 版本那样默认添加它。

检查此页面以配置 Web 服务器

.htaccess应该在 public 文件夹中,并且让您的应用程序在 Apache 下运行的最低配置是:

<VirtualHost *:80>
    ServerName domain.tld
    ServerAlias www.domain.tld

    DocumentRoot /var/www/project/public
    <Directory /var/www/project/public>
        AllowOverride All
        Order Allow,Deny
        Allow from All
    </Directory>

    # uncomment the following lines if you install assets as symlinks
    # or run into problems when compiling LESS/Sass/CoffeeScript assets
    # <Directory /var/www/project>
    #     Options FollowSymlinks
    # </Directory>

    ErrorLog /var/log/apache2/project_error.log
    CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回答,它为我指明了正确的方向;我忘记了`.htaccess`。但重要的部分是将所有请求重定向到`index.php`的重写规则,所以我写了一个完整的答案。 (2认同)