【问题标题】:How run subdomain in Symfony 4 with built in web server?如何在 Symfony 4 中使用内置 Web 服务器运行子域?
【发布时间】:2019-06-28 12:08:06
【问题描述】:

我在doc 之后阅读了有关子域的信息。

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

  • src/控制器/管理员
  • src/Controller/Main

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

例如:

#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

如何使用built-in web server 运行subdomain

【问题讨论】:

  • 您如何将project_demo.test 指向内置服务器?您对子域也这样做了吗?
  • 我只是运行 bin/console server:start 并为两者(admin.localhost 和 localhost)使用相同

标签: symfony


【解决方案1】:

我使用这个命令行调试

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

因为同名,只有最后一个读过:

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

我只是更改了管理员的名称

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

现在可以了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-21
    • 2014-09-03
    • 1970-01-01
    • 2014-06-02
    • 2019-11-03
    • 1970-01-01
    • 2015-07-27
    • 2023-04-02
    相关资源
    最近更新 更多