本文介绍了路由在 Symfony 3.4 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a new Symfony 3.4 project using:

composer create-project symfony/skeleton my-project

之后我添加了以下组件:

After that I added the following components:

composer require twig
composer require annotations
composer require maker

并创建了一个控制器:

php bin/console make:controller

我添加了一个带有合法"路由的操作.这是默认控制器:

I added an action with a route "legal". Here is the 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', []);
    }
}

文件 config/routes.yaml:

File config/routes.yaml:

#index:
#    path: /
#    defaults: { _controller: 'App\Controller\DefaultController::index' }

和 config/routes/annotations.yaml:

And config/routes/annotations.yaml:

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

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

When I access the homepage, no problem, the page is showing. But when I try the /legal page, I have a 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}
 ------------------ -------- -------- ------ --------------------------

我使用控制台命令并通过删除 var/cache 目录的内容清除了缓存.但仍然是 404.

I cleared the cache, with the console command and by removing the content of the var/cache directory. But still the 404.

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

I'm new to 3.4. Any ideas ? Thanks...

推荐答案

好吧,正如@Basel Issmail 所指出的,Symfony/Flex 不会创建 .htaccess就像以前的 Symfony 安装程序一样,我已经忘记了.

Well, as @Basel Issmail pointed out, Symfony/Flex doesn't create a .htaccess like the previous Symfony installer did, and I had forgotten it.

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

I just had a minimal Apache configuration file:

<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>

所以我在 /public/(index.php 文件所在的位置)中创建了 .htaccess 文件,以及所需的最低配置是这样的:

So I created the .htaccess file in /public/ (where the index.php file lies), and the minimal required configuration is something like:

<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>

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

The missing part was to rewrite all queries (except existing files like assets) to index.php, for Symfony to handle the route.

--- 编辑---

除了手动创建 .htaccess,您还可以使用 symfony flex 配方:

Instead of manually creating the .htaccess, you can also just use a symfony flex recipe:

composer require apache-pack

这将安装 this .htacess 文件.

这篇关于路由在 Symfony 3.4 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:11