gpt4 book ai didi

symfony - 路由在 Symfony 3.4 中不起作用

转载 作者:行者123 更新时间:2023-12-03 09:52:42 24 4
gpt4 key购买 nike

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

composer create-project symfony/skeleton my-project

之后,我添加了以下组件:
composer require twig
composer require annotations
composer require maker

并创建了一个 Controller :
php bin/console make:controller

我添加了一个带有“合法”路线的 Action 。这是默认 Controller :
<?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:
#index:
# path: /
# defaults: { _controller: 'App\Controller\DefaultController::index' }

和 config/routes/annotations.yaml:
controllers:
resource: ../../src/Controller/
type: annotation

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

Not Found - The requested URL /legal was not found on this server.


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。

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

最佳答案

好吧,正如@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>

所以我创建了 .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>

缺失的部分是将所有查询( Assets 等现有文件除外)重写为 index.php ,让 Symfony 处理路由。

--- 编辑 ---

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

这将安装 this .htacess file .

关于symfony - 路由在 Symfony 3.4 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49199481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com