symfony - 如何在 Symfony 4 中使用内置 Web 服务器运行子域?
问题描述
我阅读了以下有关子域的文档。
我的控制器文件夹结构是:
- src/控制器/管理员
- 源/控制器/主
所有路由都在控制器文件中使用注释定义。
前任:
#src/Controller/Admin/HomeController.php
class HomeController extends AbstractController
{
/**
* @Route("/", name="home")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function homepage(){...}
}
#src/Controller/Main/HomeController.php
class HomeController extends AbstractController
{
/**
* @Route("/", name="home")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function homepage(){...}
}
所以我在 config/routes.yaml 文件中添加了以下配置:
#config/routes.yaml
main:
host: "localhost"
resource: ../src/Controller/Main
type: annotation
admin:
host: "admin.localhost"
resource: ../src/Controller/Admin
type: annotation
我想要的是:
- 我使用以下命令运行服务器:
bin/控制台服务器:启动
结果除外:
访问http://admin.localhost/ => src/Controller/Admin/HomeController 中的主页方法
访问http://localhost/ => src/Controller/Main/HomeController 中的主页方法
但只有http://admin.localhost/有效并且http://localhost/得到 404 并显示以下消息:“欢迎使用 Symfony 4.2.2”
如果我在 yaml 文件中交换订单:
#config/routes.yaml
admin:
host: "admin.localhost"
resource: ../src/Controller/Admin
type: annotation
main:
host: "localhost"
resource: ../src/Controller/Main
type: annotation
http://localhost/有效并且http://admin.localhost/得到 404 和欢迎信息
如何使用内置 Web 服务器运行子域。
解决方案
我使用此命令行进行调试
php bin/控制台调试:路由器
因为同名,只有最后一个写着:
@Route("/", name="home")
我只是将管理员的名称更改为
@Route("/", name="admin-home")
现在它可以工作了
推荐阅读
- java - 使画布视图无效时单击按钮后应用程序崩溃(Android Studio,Java)
- javascript - 为什么需要 IIFE 来创建新范围?
- regex - 正则表达式将字符串与最新日期匹配
- magento - Magneto 1 - 产品系列产品模板/组件
- java - 您如何通过一台服务器为“n”个客户端创建加密密钥?
- visual-studio-code - 用于显示键输入的 VSCode 扩展
- python - 水平顺序遍历 BST 代码
- java - javafx 事件不起作用
- mysql - 在表中多次加入同一字段
- c# - 从 ASP.NET Core 中的传递 API 返回 HttpResponseMessage 内容