【问题标题】:Symfony3 routing in production生产中的 Symfony3 路由
【发布时间】:2016-06-15 12:03:03
【问题描述】:

我有一个使用 Symfony 3.1 的项目,但在部署阶段我遇到了很大的困难。

我的路由在开发环境中运行良好,使用内置服务器,但在生产环境中,某些路由不起作用。他们触发 405 错误,说明不允许该方法。

例如,其中一条不起作用的路由是(它触发 405,不允许 POST,允许:“GET,HEAD”):

http POST http://domain/api/benefits

其他的都很好用,比如

http GET http://domain/api/benefits

我尝试通过以下方式重置缓存:

./bin/console cache:clear -e=prod --no-debug

此外,如果我打电话给:

./bin/console debug:router --env=prod

我的路线在那里。你可以在这里看到输出:

 ----------------------------- -------- -------- ------ ------------------------------- 
  Name                          Method   Scheme   Host   Path                           
 ----------------------------- -------- -------- ------ ------------------------------- 
  api_read_benefit              GET      ANY      ANY    /api/benefits/{id}             
  api_delete_benefit            DELETE   ANY      ANY    /api/benefits/{id}             
  api_edit_benefit              PUT      ANY      ANY    /api/benefits/{id}             
  api_edit_benefit_picture      POST     ANY      ANY    /api/benefits/picture/{id}     
  api_edit_benefit_attachment   POST     ANY      ANY    /api/benefits/attachment/{id}  
  api_list_benefits             GET      ANY      ANY    /api/benefits/                 
  api_new_benefits              POST     ANY      ANY    /api/benefits/                 
  api_read_entity               GET      ANY      ANY    /api/entities/{id}             
  api_delete_entity             DELETE   ANY      ANY    /api/entities/{id}             
  api_list_entities             GET      ANY      ANY    /api/entities/                 
  api_new_entities              POST     ANY      ANY    /api/entities/                 
  logout                        ANY      ANY      ANY    /logout                        
  lang                          ANY      ANY      ANY    /lang                          
  app                           ANY      ANY      ANY    /{url}

我的 routing.yml 是:

api:
    resource:   "@AppBundle/routing.yml"
    prefix:   /api

logout:
    path: /logout

lang:
    path: /lang
    defaults: {_controller: AppBundle:Default:lang}

app:
    path:     /{url}
    requirements:
            url: ".*"
    defaults: {_controller: AppBundle:Default:index} 

AppBundle/routing.yml:

api_benefits:
    resource:   "@AppBundle/Controller/benefits_routing.yml"
    prefix: /benefits

api_entities:
    resource:   "@AppBundle/Controller/entities_routing.yml"
    prefix: /entities

api_users:
    resource:   "@AppBundle/Controller/entities_routing.yml"
    prefix: /entities

AppBundle/Controller/benefits_routing.yml:

api_read_benefit:
    path: /{id}
    defaults: {_controller: "AppBundle:Benefits:read" }
    methods: [GET]
api_delete_benefit:
    path: /{id}
    defaults: {_controller: "AppBundle:Benefits:delete" }
    methods: [DELETE]
api_edit_benefit:
    path: /{id}
    defaults: {_controller: "AppBundle:Benefits:edit" }
    methods: [PUT]
api_edit_benefit_picture:
    path: /picture/{id}
    defaults: {_controller: "AppBundle:Benefits:pictureChange" }
    methods: [POST]
api_edit_benefit_attachment:
    path: /attachment/{id}
    defaults: {_controller: "AppBundle:Benefits:attachmentChange" }
    methods: [POST]
api_list_benefits:
    path: /
    defaults: {_controller: "AppBundle:Benefits:list" }
    methods: [GET]
api_new_benefits:
    path: /
    defaults: {_controller: "AppBundle:Benefits:new" }
    methods: [POST]

任何方向都将受到高度赞赏,因为我找不到问题。

非常感谢。

编辑:

以防万一,我发布了我的 Apache 的 Vhost:

<VirtualHost 51.254.96.87:443>
    ServerName domain

    SSLEngine On
    SSLProxyEngine On
    SSLCertificateFile /etc/letsencrypt/live/domain/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/domain/privkey.pem

    DocumentRoot dir/web
    <Directory dir/web>
            AllowOverride All
            Order allow,deny
            Allow from all
            <IfModule mod_rewrite.c>
                Options -MultiViews
                RewriteEngine On
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteRule ^(.*)$ app.php [QSA,L]
            </IfModule>
    </Directory>
     <Directory dir/web/bundles>
           <IfModule mod_rewrite.c>
                    RewriteEngine Off
            </IfModule>
    </Directory>
    LogLevel warn
    ErrorLog  ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost 51.254.96.87:80>
    ServerName domain
    Redirect permanent / https://domain
</VirtualHost>

【问题讨论】:

  • 看起来有问题的路由是为 PUT 而不是 POST 定义的?
  • 不是,在调试中它说: api_new_benefits POST ANY ANY /api/benefits/ 或者我错过了什么?无论如何,它在开发中运行良好。
  • 你是对的。您究竟是如何生成请求的? Ajax 或 html 表单。 Html 表单实际上只是为 POST 设计的。 symfony.com/doc/current/cookbook/routing/method_parameters.html
  • 我通过 3 种方式生成它,但都不起作用:1. AngularJS $http -> Ajax 2. 使用 github.com/jkbrzt/httpie (curl?) 3. Chrome 的邮递员
  • 不做任何跨域的东西?

标签: apache routing symfony


【解决方案1】:

终于找到了。我在 Symfony 的 Github 上打开了一个问题,因为它是路由 php 生成器的问题:

https://github.com/symfony/symfony/issues/19064

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2011-11-30
    • 1970-01-01
    • 2017-12-27
    相关资源
    最近更新 更多