php - Annoation Route 在 symfony 4.2.8 中不起作用
问题描述
我使用 symfony 4创建页面。
像这样创建 ArticleController:
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ArticleController extends Controller
{
public function index()
{
/**
* @Route("/")
* @Method({"GET"})
*/
return new Response(
'<html><body>Hello !!!</body></html>'
);
}
}
我使用作曲家安装注释,这是我的composer.json
文件:
{
"type": "project",
"license": "proprietary",
"require": {
"php": "^7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"sensio/framework-extra-bundle": "^5.3",
"symfony/console": "4.2.*",
"symfony/dotenv": "4.2.*",
"symfony/flex": "^1.1",
"symfony/framework-bundle": "4.2.*",
"symfony/twig-bundle": "4.2.*",
"symfony/yaml": "4.2.*"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"paragonie/random_compat": "2.*",
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php71": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php56": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "4.2.*"
}
}
}
并将此.htaccess
文件添加到公用文件夹中:
<IfModule mod_rewrite.c>
RewriteEngine On
# Determine the RewriteBase automatically and set it as environment variable.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/index.php [L]
</IfModule>
现在,当我查看浏览器 url 时:smfony.local
我看到了 symfony 欢迎页面。我使用文件检查路线routes.yaml
,我的路线工作正常(显示你好!!!!消息)但注释路线不适用于我。
我该如何解决这个问题?!
解决方案
我认为您需要在路线之前移动注释:
/**
* @Route("/", name="index")
*/
public function index()
{
return new Response(
'<html><body>Hello !!!</body></html>'
);
}
还要确保添加注释composer require annotations
推荐阅读
- install4j - install4j:是否有可能对所有媒体都有完整的更新体验?
- javascript - 如何获取 Ant Design Option 值并将其传递到 React 中的 POST
- python - ModuleNotFoundError:没有名为“PyQt5”的模块 - Windows PyCharm
- terraform - 可以使用字符串和地图元素的 Terraform 地图吗?
- mysql - 将日期范围转换为月份范围,除以金额[不是什么程序]
- excel - EXCEL 函数仅将最后 3 个值与逗号分隔的地址分开
- node.js - 在 Atom(编辑器)中放置一个 Electron BrowserView/BrowserWindow
- r - 从列表中的数据框的多个列中删除异常值
- json - Azure 数据工厂 - 带筛选器的 REST 到 Azure SQL
- javascript - 无法使用 Vanilla JS 传递此错误:无法读取未定义的属性“地图”