1
votes

I'm running into an issue with the latest version of Symfony 4.1

I'm just getting up-to-speed with the latest version of Symfony. The last time I used Symfony, it was still in version 2.8

Anyways, my issue is that Symfony is returning a 404 on any route any other than the first route of my DefaultController.

<?php
namespace App\Controller;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class DefaultController
{
    public function index()
    {
        return new JsonResponse(['world'], Response::HTTP_OK);
    }

    public function show($slug)
    {
        return new JsonResponse(['slug' => $slug], Response::HTTP_OK);
    }
}

This is my config/routes.yaml:

index:
  path: /
  controller: App\Controller\DefaultController::index
  condition: "context.getMethod() in ['GET']"

blog_show:
  path:     /blog/{slug}
  controller: App\Controller\DefaultController::show
  condition: "context.getMethod() in ['GET']"

Visiting localhost, returns the response ['world'] as expected, and if I change that word, the response changes as well.

However, when I try to hit the URL http://localhost/blog/iliketacos I get a 404 error.

If I run the route debugger php bin/console debug:router I get the following table:

 -------------------------- -------- -------- ------ -----------------------------------
  Name                       Method   Scheme   Host   Path
 -------------------------- -------- -------- ------ -----------------------------------
  _twig_error_test           ANY      ANY      ANY    /_error/{code}.{_format}
  _wdt                       ANY      ANY      ANY    /_wdt/{token}
  _profiler_home             ANY      ANY      ANY    /_profiler/
  _profiler_search           ANY      ANY      ANY    /_profiler/search
  _profiler_search_bar       ANY      ANY      ANY    /_profiler/search_bar
  _profiler_phpinfo          ANY      ANY      ANY    /_profiler/phpinfo
  _profiler_search_results   ANY      ANY      ANY    /_profiler/{token}/search/results
  _profiler_open_file        ANY      ANY      ANY    /_profiler/open
  _profiler                  ANY      ANY      ANY    /_profiler/{token}
  _profiler_router           ANY      ANY      ANY    /_profiler/{token}/router
  _profiler_exception        ANY      ANY      ANY    /_profiler/{token}/exception
  _profiler_exception_css    ANY      ANY      ANY    /_profiler/{token}/exception.css
  index                      ANY      ANY      ANY    /
  blog_show                  ANY      ANY      ANY    /blog/{slug}
 -------------------------- -------- -------- ------ -----------------------------------

I can also successfully match the route in the console:

php bin/console router:match /blog/iliketacos



 [OK] Route "blog_show" matches


+--------------+---------------------------------------------------------+
| Property     | Value                                                   |
+--------------+---------------------------------------------------------+
| Route Name   | blog_show                                               |
| Path         | /blog/{slug}                                            |
| Path Regex   | #^/blog/(?P<slug>[^/]++)$#sD                            |
| Host         | ANY                                                     |
| Host Regex   |                                                         |
| Scheme       | ANY                                                     |
| Method       | ANY                                                     |
| Requirements | NO CUSTOM                                               |
| Class        | Symfony\Component\Routing\Route                         |
| Defaults     | _controller: App\Controller\DefaultController::show     |
| Options      | compiler_class: Symfony\Component\Routing\RouteCompiler |
+--------------+---------------------------------------------------------+

The cache has been cleared multiple times, I even cleared it manually (deleting both: dev and prod directories)

I'm running my symfony application inside a Docker container with the root user if that matters.

Why is my browser unable to get a proper response from Symfony, but all the symfony tools determine that everything should be good?

EDIT: Docker stuff (none of these credentials are prod credentials, so no worries)

version: '3'
services:
  daracatum_db:
    image: mysql:5.7
    volumes:
      - ./db:/var/lib/mysql
      - ./db_init:/docker-entrypoint-initdb.d
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: tYAQORgNyV
      MYSQL_DATABASE: daracatum_backend
      MYSQL_USER: daracatum
      MYSQL_PASSWORD: tdAY8zKfkz
    ports:
      - "3306:3306"
  daracatum_app:
    depends_on:
      - daracatum_db
    image: php:7.2-apache
    ports:
      - "80:80"
    restart: always
    environment:
      DB_HOST: daracatum_db:3306
      DB_USER: daracatum
      DB_PASSWORD: tdAY8zKfkz
      APP_ENV: dev
      APP_SECRET: 2e812144275b4979c2ef671ee72f7148
      DATABASE_URL: mysql://db_user:[email protected]:3306/db_name
      MAILER_URL: null://localhost
    volumes:
      - ./app:/var/www
      - ./app/public:/var/www/html
      - ./scripts:/scripts

I then SSH into the instance, and run this script:

#!/bin/bash
# ./scripts/composer.sh

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
mv composer.phar /usr/bin/composer
chmod +x /usr/bin/composer
apt-get update
apt-get install -y git zip unzip
cd /var/www/
composer install 
composer require annotations
2
paste your docker setup?delboy1978uk
Is it Apache's 404 or Symfony's? I don't see any virtual host configuration, so maybe you forgot about rewrite rules?MakG
Haven’t checked apache but you’re right that’s the o my thing I haven’t ruled out and it would make sense only the index controller works because that’s the defaultILikeTacos
Very good point by @MakG. Try exec-ing into the container, running php server and curl-ing that path to see if it is due to Docker/FPM or Symfony.Jovan Perovic

2 Answers

2
votes

This pack worked for me

symfony/apache-pack

so try to run this command inside container:

composer require symfony/apache-pack

or put below in Dockerfile

RUN composer require symfony/apache-pack
0
votes

The first route in config/routes.yaml is matching pretty much every possible route.

If you put the most general route later, and more specific routes (such as anything prefixed by /blog/), then the matches will be more as you expect.