我想用laravel创建web服务


I want to create web services with laravel

我想用laravel创建web服务,但我不这么做。

我使用的路线有些像这样:

Route::group(array('prefix' => 'api/v1', 'before' => 'basic.outh'), function(){
    Route::resource('url',      'UrlController@index');
    Route::resource('show',     'UrlController@show');
    Route::resource('destroy',  'UrlController@destroy');
});

但是这个路由过滤器只想用户名,像这样:

Route::filter('auth.basic', function()
{
    return Auth::basic("username");
});

我想让我的系统像Codeigniter RESTful api。这是可能的吗?

你能给我举几个例子吗?

是的,肯定有可能。

就我个人而言,我建议使用OAuth2进行基于令牌的身份验证,这更适合API。OAuth有一个相当陡峭的学习曲线,但幸运的是,有一个Laravel包(OAuth2包装器)可以让它变得非常容易,因为它将为您生成和验证令牌。

包:
https://github.com/lucadegasperi/oauth2-server-laravel

示例:
我有一个类似的设置。下面的代码并不意味着要取代遍历文档,但这与使用此包装器的路由类似。

Route::group(['prefix' => 'api/v1', 'before' => 'apiErrors'], function()
{
    // Returns a valid token based on grant_type and credentials when a request is made to the accessToken endpoint. 
    // I use 'client_credentials' and 'refresh_token' for APIs serving mobile apps, for example.  You can use that, or roll your own.
    Route::post('accessToken', function()
    {
        return AuthorizationServer::performAccessTokenFlow();
    });
    // 'oauth' filter makes sure there is a valid token present
    Route::group(['before' => 'oauth'], function()
    {
        // Your protected endpoints
        Route::resource('url',      'UrlController@index');
        Route::resource('show',     'UrlController@show');
        Route::resource('destroy',  'UrlController@destroy');
    });
});