我遇到了最新版本的Symfony 4.1的问题
我只是跟上最新版本的Symfony。我上次使用Symfony时,它仍处于2.8版中
无论如何,我的问题是Symfony在除DefaultController的第一条路线以外的任何路线上返回404。
<?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);
}
}
这是我的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']"
访问localhost,按预期返回响应['world']
,如果我更改了该单词,响应也将发生变化。
但是,当我尝试访问URL http://localhost/blog/iliketacos
时,出现404错误。
如果运行路由调试器php bin/console debug:router
,则会得到下表:
-------------------------- -------- -------- ------ -----------------------------------
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}
-------------------------- -------- -------- ------ -----------------------------------
我也可以在控制台中成功匹配路由:
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 |
+--------------+---------------------------------------------------------+
缓存已被清除多次,我什至手动清除了(删除dev
和prod
目录)
如果重要的话,我正在用root用户在Docker容器中运行我的symfony应用程序。
为什么我的浏览器无法从Symfony获得正确的响应,但是所有symfony工具都确定一切都应该很好?
编辑:Docker东西(这些凭证都不是prod凭证,所以不用担心)
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:db_password@127.0.0.1:3306/db_name
MAILER_URL: null://localhost
volumes:
- ./app:/var/www
- ./app/public:/var/www/html
- ./scripts:/scripts
然后我通过SSH进入实例,并运行以下脚本:
#!/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
答案 0 :(得分:0)
这个包对我有用
symfony/apache-pack
因此,请尝试在容器中运行以下命令:
composer require symfony/apache-pack
或放在Dockerfile中
RUN composer require symfony/apache-pack
答案 1 :(得分:0)
config / routes.yaml中的第一条路由几乎匹配所有可能的路由。
如果稍后放置最通用的路由以及更具体的路由(例如以/blog/
开头的任何路由),则匹配项将比您期望的更多。