我刚刚使用基本的 Twig 模板设置了一个新的 Symfony 5 应用程序,使用注释进行路由。
我为“主页”和“条款和条件”页面设置了路线:
<?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"]);
}
}
当我将浏览器指向:
在我的 Twig 基本模板中,我想提供一个指向“条款”页面的链接:
<a href="{{ path('terms_and_conds') }}">Terms of Use</a>
但是生成的 URL 是
如何确保生成正确的链接,不包括 index.php?
我的 /routes/annotations.yaml 是:
controllers:
resource: ../../src/Controller/
type: annotation
kernel:
resource: ../../src/Kernel.php
type: annotation
我在 PHP 7.4.2 上运行 Symfony 5.0.4 作为 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;
}