首页 > 解决方案 > 类中 HttpPost 的 PHP 等效项

问题描述

我已经搜索了一段时间,但没有找到太多信息。我在 PHP 项目中使用 MVC 设计,我想将逻辑排除在我的视图之外,所以我一直在尝试看看我是否可以做到这一点......

在 C#/ASP.NET MVC 中,您可以使用如下所示的内容:

public void MyClass {      
[HttpPost]
      public IActionResult Index(string firstName)
      {
          return Content($"Hello {firstName}");
      }  
}

这使得表单在您需要的任何类中发布属性。

我想知道在 PHP 中有没有办法做到这一点,你可以在类内有 Post 请求?也许是这样的?

class MyClass {
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // Do your thing here
    }
}

标签: phpasp.net-mvc

解决方案


你有没有尝试过,

respond('POST', '/', function ($request, $response) 
{
    $request->validate('url')->notNull();
    $response->url= $request->url;
    $response->render('any page');
});

推荐阅读