首页 > 解决方案 > 如何在 Symfony 4 中使用内置 Web 服务器运行子域?

问题描述

我阅读了以下有关子域的文档

我的控制器文件夹结构是:

所有路由都在控制器文件中使用注释定义。

前任:

#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

我想要的是:

  1. 我使用以下命令运行服务器:

bin/控制台服务器:启动

  1. 结果除外:

但只有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 服务器运行子域

标签: symfony

解决方案


我使用此命令行进行调试

php bin/控制台调试:路由器

因为同名,只有最后一个写着:

@Route("/", name="home")

我只是将管理员的名称更改为

@Route("/", name="admin-home")

现在它可以工作了


推荐阅读