Symfony:路由PUT方法

时间:2019-04-29 16:45:30

标签: symfony

有人知道为什么使用PHP Symphony无法使用PUT方法吗? 如果我将PUT替换为POST,则一切正常

$request = Request::createFromGlobals();
echo $request->request->get('name');

我正在读取这样的变量

()

错误:  找不到“ PUT / api / product / update / 23”的路由(找不到404)

3 个答案:

答案 0 :(得分:3)

问题是您没有正确创建路由。基本上,您需要在路由中添加“ id”。

/**
 * @Route("/api/product/update/{id}", name="product_udpate", methods = {"PUT"})
 */
public function updateAction(Request $request, $id)
{
    // Your logic here
    $name = $request->get('name');

}

答案 1 :(得分:1)

由于未正确配置路由,因此出现以下错误。

//...

            [DllImport("user32.dll", SetLastError = true, EntryPoint = "CreateWindowExW")]
            public static extern IntPtr CreateWindowExW(
               int dwExStyle,
               UInt16 lpClassName, // <---
               [MarshalAs(UnmanagedType.LPWStr)]
               string lpWindowName,
               UInt32 dwStyle,
               int x,
               int y,
               int nWidth,
               int nHeight,
               IntPtr hWndParent,
               IntPtr hMenu,
               IntPtr hInstance,
               IntPtr lpParam);

//...

                UInt16 regResult = RegisterClassExW(ref wind_class);

                if (regResult == 0)
                {
                    uint error = GetLastError();
                    return false;
                }


                IntPtr hWnd = CreateWindowExW(0, regResult, "Hello Win32", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 300, 400, IntPtr.Zero, IntPtr.Zero, wind_class.hInstance, IntPtr.Zero);

如果您要添加ID和所需的网址,则必须在路线中进行定义。 因此,您可以更新路线:

error: No route found for "PUT /api/product/update/23" (404 Not Found)

答案 2 :(得分:-1)

如symfony文档How to Use HTTP Methods beyond GET and POST in Routes

中所述
  

不幸的是,生活并非如此简单,因为大多数浏览器不支持通过HTML格式的method属性发送PUT和DELETE请求。幸运的是,Symfony为您提供了一种解决此限制的简单方法。通过在查询字符串或HTTP请求的参数中包含_method参数,Symfony将在匹配路由时将其用作方法

因此您必须伪装这种方法

<IfModule mod_rewrite.c>

    # Enable URL rewriting
    RewriteEngine On

    # Store the current location in an environment variable CWD to use
    # mod_rewrite in .htaccess files without knowing the RewriteBase
    RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
    RewriteRule ^.*$ - [E=CWD:%2]

    # Rule for versioned static files, configured through:
    # - $GLOBALS['TYPO3_CONF_VARS']['BE']['versionNumberInFilename']
    # - $GLOBALS['TYPO3_CONF_VARS']['FE']['versionNumberInFilename']
    # IMPORTANT: This rule has to be the very first RewriteCond in order to work!
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ %{ENV:CWD}$1.$3 [L]

    # Access block for folders
    RewriteRule _(?:recycler|temp)_/ - [F]
    RewriteRule fileadmin/templates/.*\.(?:txt|ts)$ - [F]
    RewriteRule ^(?:vendor|typo3_src|typo3temp/var) - [F]
    RewriteRule (?:typo3conf/ext|typo3/sysext|typo3/ext)/[^/]+/(?:Configuration|Resources/Private|Tests?|Documentation|docs?)/ - [F]

    # Block access to all hidden files and directories with the exception of
    # the visible content from within the `/.well-known/` hidden directory (RFC 5785).
    RewriteCond %{REQUEST_URI} "!(^|/)\.well-known/([^./]+./?)+$" [NC]
    RewriteCond %{SCRIPT_FILENAME} -d [OR]
    RewriteCond %{SCRIPT_FILENAME} -f
    RewriteRule (?:^|/)\. - [F]

    # Stop rewrite processing, if we are in the typo3/ directory or any other known directory
    # NOTE: Add your additional local storages here
    RewriteRule ^(?:typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico|admin/) - [L]

    # If the file/symlink/directory does not exist => Redirect to index.php.
    # For httpd.conf, you need to prefix each '%{REQUEST_FILENAME}' with '%{DOCUMENT_ROOT}'.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^.*$ %{ENV:CWD}index.php [QSA,L]

</IfModule>