我想用 laravel 创建网络服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25543734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:00:51  来源:igfitidea点击:

I want to create web services with laravel

phpweb-serviceslaravellaravel-4

提问by hkucuk

I want to create web services with laravel but i don't do this.

我想用 laravel 创建网络服务,但我不这样做。

I use routes some one like this:

我使用这样的路线:

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');

});

But this Route filter just want to username, like this:

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

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

I want to be make my system like Codeigniter RESTful api. This is possible?

我想让我的系统像 Codeigniter RESTful api。这个有可能?

Could you suggest me any examples?

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

回答by c-griffin

Yes, definitely possible.

是的,绝对有可能。

Personally, i would recommend using OAuth2 for token based authentication, which is better suited for APIs. OAuth has a fairly steep learning curve, but luckily, there is a package for Laravel (an OAuth2 wrapper) that makes it pretty easy, as it will generate and validate the tokens for you.

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

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

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

Example:
I have a setup similar to this. The code below isn't meant to replace going through the documentation, but this is something like what your routes would look like using this wrapper.

示例:
我有一个与此类似的设置。下面的代码并不意味着替换通过文档,但这类似于使用此包装器的路由的外观。

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');

    });

});