Symfony удалить "index. php" из сгенерированного пути ветки - PullRequest
1 голос
/ 06 февраля 2020

Я только что установил новое приложение Symfony 5 с базовым шаблоном Twig c, маршрутизацией с использованием аннотаций.

У меня есть маршруты, настроенные для страниц 'home' и 'условий и положений' :

<?php

namespace App\Controller;

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

class GeneralController extends AbstractController {

    /**
     * @Route("/", name="main_home")
     */
    public function homePage() {
        return $this->render('general/home.html.twig', ['title' => "Welcome"]);
    }

    /**
     * @Route("/terms", name="terms_and_conds")
     */
    public function termsAndConditions() {
        return $this->render('general/terms.html.twig', ['title' => "Terms and Conditions"]);
    }
}

Они оба прекрасно работают, когда я указываю своему браузеру:

https://example.com/

https://example.com/terms

В моем базовом шаблоне Twig я хочу предоставить ссылку на страницу «терминов»:

<a href="{{ path('terms_and_conds') }}">Terms of Use</a>

однако URL генерируется

https://example.com/index.php/terms

Как обеспечить создание правильной ссылки, исключая индекс. php?

Мой /routes/annotations.yaml:

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

kernel:
    resource: ../../src/Kernel.php
    type: annotation

Я использую Symfony 5.0.4 на PHP 7.4.2 в качестве приложения FPM, обслуживаемого Nginx на CentOS 7 с Plesk Obsidian.

Редактировать:

Вот мой nginx .conf

Обратите внимание, что это сгенерировано Plesk, и я не могу изменить его напрямую, однако я могу делать что хочу во включенном vhost_ nginx .conf файл.

#ATTENTION!
#
#DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,
#SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED.
server {
    listen 111.222.333.444:443 ssl http2;

    server_name example.com;
    server_name www.example.com;
    server_name ipv4.example.com;

    ssl_certificate             /usr/local/psa/var/certificates/scfecEZhs;
    ssl_certificate_key         /usr/local/psa/var/certificates/scfecEZhs;
    ssl_client_certificate      /usr/local/psa/var/certificates/scfsFsYXH;

    client_max_body_size 128m;

    proxy_read_timeout 120;

    root "/var/www/vhosts/example.com/public";
    access_log "/var/www/vhosts/system/example.com/logs/proxy_access_ssl_log";
    error_log "/var/www/vhosts/system/example.com/logs/proxy_error_log";

    if ($host ~* ^www\.example\.com$) {
        rewrite ^(.*)$ https://example.com$1 permanent;
    }

    #extension letsencrypt begin
    location ^~ /.well-known/acme-challenge/ {
        root /var/www/vhosts/default/htdocs;

        types { }
        default_type text/plain;

        satisfy any;
        auth_basic off;
        allow all;

        location ~ ^/\.well-known/acme-challenge.*/\. {
            deny all;
        }
    }
    #extension letsencrypt end

    #extension sslit begin

    add_header Strict-Transport-Security "max-age=15768000; includeSubDomains" always;

    #OCSP Stapling
    ssl_stapling on;
    ssl_stapling_verify on;

    #extension sslit end

    location / {
        proxy_pass https://111.222.333.444:7081;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        access_log off;

    }

    location ~ "^/" {
        proxy_pass https://111.222.333.444:7081;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        access_log off;

    }

    location @fallback {
        proxy_pass https://111.222.333.444:7081;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        access_log off;

    }

    location ~ ^/(.*\.(ac3|avi|bmp|bz2|css|cue|dat|doc|docx|dts|eot|exe|flv|gif|gz|htm|html|ico|img|iso|jpeg|jpg|js|mkv|mp3|mp4|mpeg|mpg|ogg|pdf|png|ppt|pptx|qt|rar|rm|svg|swf|tar|tgz|ttf|txt|wav|woff|woff2|xls|xlsx|zip))$ {
        try_files $uri @fallback;
    }

    location ~ ^/~(.+?)(/.*?\.php)(/.*)?$ {
        alias /var/www/vhosts/example.com/web_users/$1/$2;
        fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass "unix:///var/www/vhosts/system/example.com/php-fpm.sock";
        include /etc/nginx/fastcgi.conf;

    }

    location ~ ^/~(.+?)(/.*)?$ {
        proxy_pass https://111.222.333.444:7081;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        access_log off;

    }

    location ~ \.php(/.*)?$ {
        fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass "unix:///var/www/vhosts/system/example.com/php-fpm.sock";
        include /etc/nginx/fastcgi.conf;

    }

    add_header X-Powered-By PleskLin;

    include "/var/www/vhosts/system/example.com/conf/vhost_nginx.conf";
}
server {
    listen 111.222.333.444:80;

    server_name example.com;
    server_name www.example.com;
    server_name ipv4.example.com;

    client_max_body_size 128m;

    proxy_read_timeout 120;

    if ($host ~* ^www\.example\.com$) {
        rewrite ^(.*)$ https://example.com$1 permanent;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

и включенный vhost_ nginx .conf

    if (!-f $request_filename) {
        rewrite ^/(.*)$ /index.php last;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass unix:/run/plesk-php74-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;

        internal;
    }

    location ~ \.php$ {
        return 404;
    }

1 Ответ

0 голосов
/ 07 февраля 2020

Заменить:

if (!-f $request_filename) {
    rewrite ^/(.*)$ /index.php last;
}

На:

location / {
    try_files $uri /index.php$is_args$args;
}

Ссылка: https://symfony.com/doc/current/setup/web_server_configuration.html#nginx

...