簡體   English   中英

使用symfony框架在Apache2中找不到404

[英]Not found 404 in Apache2 using the symfony framework

我從以下官方網站上獲取了配置: 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到底出了什么問題。

這是錯誤的:

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

根據鏈接https://symfony.com/doc/current/setup/web_server_configuration.html ,它將禁用mod_rewrite,它用於:

(可選)禁用資產目錄的RewriteEngine,這將允許apache在找不到文件時簡單地以404進行回復,而不是將請求傳遞到完整的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

或者,您可以使用注釋,首先安裝注釋:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM