【问题标题】:Not found 404 in Apache2 using the symfony framework使用 symfony 框架在 Apache2 中找不到 404
【发布时间】:2017-12-10 15:47:46
【问题描述】:

这里是我从官网取的配置:https://symfony.com/doc/current/setup/web_server_configuration.html(没有使用.htaccess的那个),我把它放在这里:/etc/apache2/sites-enabled/000-default.conf。我也改变了一些路径,现在我有了这个:

<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
DocumentRoot /home/shade/Documents/webchat/public
<Directory /home/shade/Documents/webchat/public>
    AllowOverride None
    Require all granted
    Allow from All
    <IfModule mod_rewrite.c>
        Options -MultiViews
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php [QSA,L]
    </IfModule>
</Directory>
<Directory /home/shade/Documents/webchat/public>
    <IfModule mod_rewrite.c>
        RewriteEngine Off
    </IfModule>
</Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined

当我输入 'localhost' 时,我收到消息说没有 '/' 的路由,这是真的(此消息来自 symfony)。但我确实有 /lucky/number 的路线,当我输入“localhost/lucky/number”时,我收到一条 404 Not Found 消息。我的 Apache Web 服务器有什么问题?如果我使用内置的 php web 服务器,一切正常。但我不知道 Apache 出了什么问题。

【问题讨论】:

  • 你在ServerName中使用domain.tld
  • 我已经删除了,谢谢,可惜还是不行。
  • 是的,我已经重新加载了我的 apache 网络服务器
  • 你是如何在 Symfony 中配置路由的?

标签: php apache symfony debian


【解决方案1】:

错了:

<Directory /home/shade/Documents/webchat/public>
    <IfModule mod_rewrite.c>
        RewriteEngine Off
    </IfModule>
</Directory>

它将禁用mod_rewrite,根据链接https://symfony.com/doc/current/setup/web_server_configuration.html,这是用来:

可选择禁用资产目录的 RewriteEngine 这将允许 apache 在文件被保存时简单地回复 404 not found 而不是将请求传递到完整的 symfony 堆栈中

这是配置资产而不是路由的可选示例,在此文件夹中将是资产:

 <Directory /var/www/project/public/bundles>

固定:

首先使用终端启用 mod_write:

$ sudo a2enmod rewrite

在 Debian 中:

$ su
$ a2enmod rewrite

然后尝试使用这个:

<VirtualHost *:80>
    ServerName localhost

    DocumentRoot /home/shade/Documents/webchat/public
    <Directory /home/shade/Documents/webchat/public>
        AllowOverride None
        Order Allow,Deny
        Allow from All

        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ index.php [QSA,L]
        </IfModule>
    </Directory>

    ErrorLog /var/log/apache2/project_error.log
    CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>

并在终端中重新启动 apache:

$ sudo /etc/init.d/apache2 restart

或者在 Debian 中:

$ su
$ /etc/init.d/apache2 restart

创建路线

可以使用config/routes.yaml,先在src/Controller/LuckyController.php创建一个Controller:

<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;

class LuckyController
{
    public function number()
    {
        $number = mt_rand(0, 100);

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

并在src/Controller/DefaultController.php 中为/ 创建一个Controller:

<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;

class DefaultController
{
    public function index()
    {
        return new Response(
            '<html><body>FooBar</body></html>'
        );
    }
}

config/routes.yaml文件之后,放这个:

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

# the "app_lucky_number" route name is not important yet
app_lucky_number:
    path: /lucky/number
    controller: App\Controller\LuckyController::number

或者你可以使用Annotions,先安装annotions:

composer require annotations

如果您的作曲家不是全球使用的:

php composer.phar require annotations

并将 lucky.php 更改为:

use Symfony\Component\Routing\Annotation\Route;

class LuckyController
{
    /**
     * @Route("/lucky/number")
     */
    public function number()
    {
        // this looks exactly the same
    }
}

【讨论】:

  • 不幸的是,它没有帮助:(我也将Order Allow,Deny更改为Require all granted
  • 我的代码与您在 creating routes 中列出的代码相同(使用 routes.yaml)。当我输入“localhost/lucky/number”时,似乎没有任何效果。
  • @ShadeWe 您使用的是symfony/skeleton 还是只使用“Symfony 核心”?
  • symfony/骨架
  • 我尝试将路由更改为app_lucky_number: path: / controller: App\Controller\LuckyController::number 并且成功了!其他不是“/”的 URI 有什么问题?
猜你喜欢
  • 1970-01-01
  • 2013-05-23
  • 1970-01-01
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 2014-09-20
相关资源
最近更新 更多