无需身份验证的 Laravel 5.3 RESTFul API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40040476/
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 14:36:20  来源:igfitidea点击:

Laravel 5.3 RESTFul API without authentication

apilaravelauthenticationrestful-architecture

提问by csharper

I want to create an API with Laravel 5.3 but i don't need any kind of authentication. Is it possible to get rid of it? I don't want any token or any kind of authentication.

我想用 Laravel 5.3 创建一个 API,但我不需要任何类型的身份验证。有没有可能摆脱它?我不想要任何令牌或任何类型的身份验证。

回答by Adrien C

To help anyone in my situation who arrive here : be aware that any route in api.php is prefixed by "api/". It is set in /app/Providers/RouteServiceProvider.php.

为了帮助到达这里的任何人:请注意 api.php 中的任何路由都以“api/”为前缀。它设置在/app/Providers/RouteServiceProvider.php.

So :

所以 :

Route::get('/delegates', "APIController@delegate");

Route::get('/delegates', "APIController@delegate");

Will be accessible from

可从

http://www.yourdomain.com/api/delegates

http://www.yourdomain.com/api/delegates

Sorry if it's a bit off-topic, but hope it can help someone.

对不起,如果它有点跑题,但希望它可以帮助某人。

回答by sgtcadet

Yes, it's possible normally in your

是的,通常可以在您的

route/api.php

路由/api.php

you'd have something like

你会有类似的东西

Route::middleware('auth:api')->get('/user', function (Request $request) {
   return $request->user();
});

you just need to remove the part of the middleware that's referencing auth. So the above would look like:

您只需要删除引用身份验证的中间件部分。所以上面看起来像:

Route::middleware('api')->get('/user', function (Request $request) {
  return $request->user();
  //middleware('api') URI prefix. which would become '/api/user'
});

or

或者

Route::apiResource('user', 'UserController');
//same as above but includes crud methods excluding 'create and edit'

回答by Elias Soares

Of course you can get rid of it. Just setup your routes to don't use any middleware.

当然你可以摆脱它。只需将您的路由设置为不使用任何中间件。

Create your API routes on routes/api.phpfile, then modify the app/Http/Kernel.phpfile to set your middlewares correctly:

routes/api.php文件上创建 API 路由,然后修改app/Http/Kernel.php文件以正确设置中间件:

Remove (or add) the middlewares you don't want on apimiddleware group.

删除(或添加)api中间件组中不需要的中间件。

By default, L5.3 comes with two middlewares on apigroup:

默认情况下,L5.3 在apigroup上带有两个中间件:

'api' => [
            'throttle:60,1',
            'bindings',
        ],

The first one provides a rate limiting to your API (60 requests/minute), the second substitutes your model bindings.

第一个为您的 API 提供速率限制(60 个请求/分钟),第二个替代您的模型绑定。

回答by Marek Skiba

It's possible, just create route to your controller and return data (Without any auth middleware).

这是可能的,只需创建到您的控制器的路由并返回数据(没有任何身份验证中间件)。

回答by Komal

Allow your route to run without auth

允许您的路线在没有身份验证的情况下运行

Http\Middleware\VerifyCsrfToken
public function handle($request, Closure $next)
{
  if (!$request->is('api/*'))
  {
    return parent::handle($request, $next);
  }

  return $next($request);
}

Set route like this

像这样设置路线

'api' => 'APIController'

This is method in APIController ('/api/data')

这是 APIController ('/api/data') 中的方法

public function getData(Request $request)
{
  return "Hello";
}