简体   繁体   English

Laminas 框架路由问题

[英]Laminas Framework Routing Issues

I am developing a website using the Laminas Framework.我正在使用 Laminas 框架开发一个网站。 Some URIs (listed below) are not behaving as expected.某些 URI(下面列出)未按预期运行。 I am unsure if the problem is Laminas or php:fpm or nginx. Questions:我不确定问题是 Laminas 还是 php:fpm 或 nginx。问题:

  1. How can I have (G) and (H) be treated exactly the same by Laminas so that my custom '404 Page Not Found' is rendered?我怎样才能让 (G) 和 (H) 被 Laminas 完全相同地对待,以便呈现我的自定义“404 页面未找到”?
  2. Is it normal for (C) and (D) to return 404 Not found? (C)和(D)返回404 Not found是否正常? Or should this be rendering index.phtml?还是应该渲染 index.phtml?

Examples of behavior when I use chrome to access www.domain.com (really it is 127.0.0.1:3080 via a virtual machine!)我使用 chrome 访问www.domain.com时的行为示例(实际上是通过虚拟机访问 127.0.0.1:3080!)

 (A) www.domain.com (Laminas renders custom index.phtml) -- OK, expected.
 (B) www.domain.com/ (Laminas renders custom index.phtml) -- OK, expected.
 (C) www.domain.com/index.php (Laminas renders custom 404 Not found) -- PROBLEM?? Or expected??
 (D) www.domain.com/index.php/ (Laminas renders custom 404 Page Not Found) -- PROBLEM?? Or expected??
 (E) www.domain.com/dog (Laminas renders custom 404 Page Not Found, page does not exist)  -- OK, expected.
 (F) www.domain.com/dog/ (Laminas renders custom 404 Page Not Found, page does not exist)  -- OK, expected.
 (G) www.domain.com/dog.php (Php:fpm "File Not Found", page does not exit) -- PROBLEM!
 (H) www.domain.com/dog.php/ (Laminas renders custom 404 Page Not Found, page does not exit)  -- OK, expected.

Nginx Configuration Nginx 配置

index index.php;

server {
    listen 80;
    listen  [::]:80;

    server_name www.domain.com;
    root        /var/www/public;

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

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass docker-php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

The relevant Laminas file/directory layout:相关的 Laminas 文件/目录布局:

- website/public/index.php  # Entry point of the web app
- website/module/Application/view/application/index.phtml  # Custom index page to render
- module/Application/view/error/404.phtml # Custom 404 page
- dog, dog.php do not exist

EDIT: I do not believe (G) is even making it Laminas.编辑:我不相信(G)甚至使它成为 Laminas。 I get the following error message in this case (logs from running docker-compose):在这种情况下,我收到以下错误消息(运行 docker-compose 的日志):

docker-php    | 172.20.0.5 -  30/Jan/2023:02:41:41 +0000 "GET /dog.php" 404
docker-nginx  | 2023/01/30 02:41:41 [error] 24#24: *13 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 10.0.2.2, server: www.domain.com, request: "GET /dog.php HTTP/1.1", upstream: "fastcgi://172.20.0.3:9000", host: "127.0.0.1:3080"
docker-nginx  | 10.0.2.2 - - [30/Jan/2023:02:41:41 +0000] "GET /dog.php HTTP/1.1" 404 27 "-" "Mozilla/5.0 [...]

Compared to the properly working (H) case: The properly routed 404.phtml (logs from running docker-compose):与正常工作 (H) 情况相比:正确路由的 404.phtml(运行 docker-compose 的日志):

docker-php    | 172.20.0.5 -  30/Jan/2023:02:47:01 +0000 "GET /index.php" 404
docker-nginx  | 10.0.2.2 - - [30/Jan/2023:02:47:01 +0000] "GET /dog.php/ HTTP/1.1" 404 311 "-" "Mozilla/5.0 
  1. To have (G) and (H) treated the same by Laminas, you should add routes to the same controller action for both URIs in your application's router configuration.要让 Laminas 对 (G) 和 (H) 进行相同的处理,您应该在应用程序的路由器配置中为两个 URI 添加路由到相同的 controller 操作。 To display your custom '404 Page Not Found', you can catch exceptions in your controller action and render the custom error page.要显示您的自定义“404 页面未找到”,您可以在 controller 操作中捕获异常并呈现自定义错误页面。

  2. If (C) and (D) are not defined routes in your application, it is normal for them to return a 404 Not Found error.如果 (C) 和 (D) 不是您应用程序中定义的路由,则它们返回 404 Not Found 错误是正常的。 However, if you want them to render the index.phtml page, you should add a default route to your application's router configuration that maps to the controller action responsible for rendering index.phtml.但是,如果您希望它们呈现 index.phtml 页面,您应该将默认路由添加到应用程序的路由器配置中,该路由映射到负责呈现 index.phtml 的 controller 操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM