如何只允许 POST 和 GET http 动词用于 laravel 中的隐式控制器方法


how to allow only POST and GET http verb for methods of implicit controller in laravel

假设我有一个这样的隐式控制器:

class UserController extends Controller
{
    public function getShow($id)
    {
        //
    }
}

在路线中,我写道:

Route::controller('users', 'UserController');

我们知道,如果只有用户通过 GET http 请求请求它,users/show响应。

但如果我希望它只响应 GET 和 POST(而不是其他方法,如 PUT、DELETE ,...(我该怎么做?

当然,我们知道我们可以为上述方法使用any前缀,但它响应所有http方法。 像这样:

class UserController extends Controller
    {
        public function anyShow($id)
        {
            //
        }
    }

如果您的隐式控制器仅包含 GET 和 POST(前缀(方法 – 没有其他方法将起作用,因此无需担心。

例如,如果您有:

class UserController extends Controller
{
    public function getShow($id)
    {
    }
    public function postShow($id)
    {
    }
}

Laravel只允许"GET/show"和"POST/show"。

但请注意,Laravel很快就会放弃隐式控制器,因为即使它们提供了简单性,隐式控制器也带来了许多缺点。这篇文章有一个很好的分析。