【问题标题】:I want to create web services with laravel我想用 laravel 创建 Web 服务
【发布时间】:2014-08-28 23:30:19
【问题描述】:

我想用 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。这可能吗?

你能给我推荐一些例子吗?

【问题讨论】:

    标签: php web-services laravel laravel-4


    【解决方案1】:

    是的,绝对有可能。

    就个人而言,我建议使用 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');
    
        });
    
    });
    

    【讨论】:

    • @c-griffin 我们可以同时使用令牌 csrf 令牌和 oauth2。用于 Web 表单的 CSRF 令牌和用于 api 的 Oauth2 令牌
    • 通过 oAuth 规范,令牌在标头中传递。如果您使用的是 https,则标头将被加密。但简而言之,没有什么能阻止任何人将 oAuth 和 CSRF 结合使用。
    猜你喜欢
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    相关资源
    最近更新 更多