Heim  >  Artikel  >  Backend-Entwicklung  >  Lumen 框架如何注册多种路由(或 响应所有http请求)

Lumen 框架如何注册多种路由(或 响应所有http请求)

WBOY
WBOYOriginal
2016-06-06 20:32:132019Durchsuche

正尝试用Lumen开发一个微信接口,折腾下来后发现微信服务器第一次验证URL时使用的是GET方式请求接口,之后使用的是POST方式访问接口。

所以我想将微信的路由注册为可响应GET和POST两种请求:

查了下laravel 5的文档,为多种请求匹配路由:

<code>Route::match(['get', 'post'], '/', function()
{
    return 'Hello World';
});
</code>

注册为响应所有请求:

<code>Route::any('foo', function()
{
    return 'Hello World';
});
</code>

然后我尝试在 Lumen routes.php文件中将原来的 $app->get()$app->post() 换成 $app->any() ,再现错误:
Fatal error: Call to undefined method Laravel\Lumen\Application::any()

所以,请问:在Lumen框架中,如果要将某个路径注册为可响应所有请求(或者能同时响应POST和GET)应该怎样做呢?

回复内容:

正尝试用Lumen开发一个微信接口,折腾下来后发现微信服务器第一次验证URL时使用的是GET方式请求接口,之后使用的是POST方式访问接口。

所以我想将微信的路由注册为可响应GET和POST两种请求:

查了下laravel 5的文档,为多种请求匹配路由:

<code>Route::match(['get', 'post'], '/', function()
{
    return 'Hello World';
});
</code>

注册为响应所有请求:

<code>Route::any('foo', function()
{
    return 'Hello World';
});
</code>

然后我尝试在 Lumen routes.php文件中将原来的 $app->get()$app->post() 换成 $app->any() ,再现错误:
Fatal error: Call to undefined method Laravel\Lumen\Application::any()

所以,请问:在Lumen框架中,如果要将某个路径注册为可响应所有请求(或者能同时响应POST和GET)应该怎样做呢?

复制一份 get post 各写一次
或者 Function名称不带请求方式 在路由里get post 各定义一次

可以扩展Application类,然后自己写一个方法map

<code><?php namespace App;

use Laravel\Lumen\Application as BaseApplication;

class Application extends BaseApplication {

    public function map($methods, $uri, $action)
    {
        foreach ($methods as $method) {
            $this->addRoute($method, $uri, $action);
        }
    }
}
</code>

然后在routes.php

<code>$app->map(['GET','POST'], 'foo', function() {
    return 'Hello World';
});
</code>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn