Laminas Framework Routing Issues

393 Views Asked by At

I am developing a website using the Laminas Framework. Some URIs (listed below) are not behaving as expected. I am unsure if the problem is Laminas or php:fpm or nginx. Questions:

  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?
  2. Is it normal for (C) and (D) to return 404 Not found? Or should this be rendering 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!)

 (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

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:

- 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. I get the following error message in this case (logs from running 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):

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

There are 1 best solutions below

1
CodeWithArefin On
  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. To display your custom '404 Page Not Found', you can catch exceptions in your controller action and render the custom error page.

  2. If (C) and (D) are not defined routes in your application, it is normal for them to return a 404 Not Found error. 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.