php Xdebug检测到可能的无限循环

uqdfh47h  于 2024-01-05  发布在  PHP
关注(0)|答案(1)|浏览(154)

我正在尝试设置一个Laravel后端,它使用Sanctum与React-Native前端应用程序交互。
我在routes/api.php中设置了一个简单的测试路由,但是每当我向它发出请求时,我都会得到以下错误:

  1. local.ERROR: Xdebug has detected a possible infinite loop, and aborted your script with a stack depth of '512' frames {"exception":"[object] (Error(code: 0): Xdebug has detected a possible infinite loop, and aborted your script with a stack depth of '512' frames at
  2. C:\\Users\\George\\Projects\\gig-app\\backend\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php:1339)
  3. [stacktrace]
  4. #0 C:\\Users\\George\\Projects\\gig-app\\backend\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php(968): Illuminate\\Foundation\\Application->isDeferredService('config')
  5. #1 C:\\Users\\George\\Projects\\gig-app\\backend\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php(955): Illuminate\\Foundation\\Application->loadDeferredProviderIfNeeded('config')
  6. #2 C:\\Users\\George\\Projects\\gig-app\\backend\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(731): Illuminate\\Foundation\\Application->resolve('config', Array)
  7. #3 C:\\Users\\George\\Projects\\gig-app\\backend\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php(942): Illuminate\\Container\\Container->make('config', Array)
  8. #4 C:\\Users\\George\\Projects\\gig-app\\backend\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\helpers.php(120): Illuminate\\Foundation\\Application->make('config', Array)
  9. #5 C:\\Users\\George\\Projects\\gig-app\\backend\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\helpers.php(274): app('config')
  10. #6 C:\\Users\\George\\Projects\\gig-app\\backend\\vendor\\laravel\\sanctum\\src\\Guard.php(56): config('sanctum.guard', 'web')
  11. #7 [internal function]: Laravel\\Sanctum\\Guard->__invoke(Object(Illuminate\\Http\\Request), Object(Illuminate\\Auth\\EloquentUserProvider))
  12. #8 C:\\Users\\George\\Projects\\gig-app\\backend\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\RequestGuard.php(57): call_user_func(Object(Laravel\\Sanctum\\Guard), Object(Illuminate\\Http\\Request), Object(Illuminate\\Auth\\EloquentUserProvider))
  13. #9 C:\\Users\\George\\Projects\\gig-app\\backend\\vendor\\laravel\\sanctum\\src\\Guard.php(57): Illuminate\\Auth\\RequestGuard->user()
  14. #10 [internal function]: Laravel\\Sanctum\\Guard->__invoke(Object(Illuminate\\Http\\Request), Object(Illuminate\\Auth\\EloquentUserProvider))
  15. #11 C:\\Users\\George\\Projects\\gig-app\\backend\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\RequestGuard.php(57): call_user_func(Object(Laravel\\Sanctum\\Guard), Object(Illuminate\\Http\\Request), Object(Illuminate\\Auth\\EloquentUserProvider))

字符串
路线是:

  1. Route::middleware('auth:sanctum')->get('/test', function (Request $request) {
  2. return response()->json(['message' => 'Welcome to Laravel'], 200);
  3. });


请注意堆栈中的错误#6,我不确定这是从哪里来的,因为我已经将所有内容设置为api

  1. #6 C:\\Users\\George\\Projects\\gig-app\\backend\\vendor\\laravel\\sanctum\\src\\Guard.php(56): config('sanctum. Guard', 'web')


即使我从路由中删除中间件,我仍然会得到同样的错误。
我在config/auth.php中安装并配置了Sanctum,如下所示:

  1. return [
  2. 'defaults' => [
  3. 'guard' => 'api',
  4. 'passwords' => 'users',
  5. ],
  6. 'guards' => [
  7. 'web' => [
  8. 'driver' => 'session',
  9. 'provider' => 'users',
  10. ],
  11. 'api' => [
  12. 'driver' => 'sanctum',
  13. 'provider' => 'users',
  14. ]
  15. ],
  16. 'providers' => [
  17. 'users' => [
  18. 'driver' => 'eloquent',
  19. 'model' => App\Models\User::class,
  20. ],
  21. // 'users' => [
  22. // 'driver' => 'database',
  23. // 'table' => 'users',
  24. // ],
  25. ],
  26. 'passwords' => [
  27. 'users' => [
  28. 'provider' => 'users',
  29. 'table' => 'password_reset_tokens',
  30. 'expire' => 60,
  31. 'throttle' => 60,
  32. ],
  33. ],
  34. 'password_timeout' => 10800,
  35. ];


Kernel.php中,我取消了对EnsureFrontendRequestsAreStateful的注解:

  1. protected $middlewareGroups = [
  2. 'web' => [
  3. \App\Http\Middleware\EncryptCookies::class,
  4. \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
  5. \Illuminate\Session\Middleware\StartSession::class,
  6. \Illuminate\View\Middleware\ShareErrorsFromSession::class,
  7. \App\Http\Middleware\VerifyCsrfToken::class,
  8. \Illuminate\Routing\Middleware\SubstituteBindings::class,
  9. ],
  10. 'api' => [
  11. \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
  12. \Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
  13. \Illuminate\Routing\Middleware\SubstituteBindings::class,
  14. ],
  15. ];


这是React-Native应用程序中的App.js

  1. import React, { useEffect, useState } from "react";
  2. import { StatusBar } from "expo-status-bar";
  3. import { StyleSheet, Text, View } from "react-native";
  4. export default function App() {
  5. const [message, setMessage] = useState("");
  6. useEffect(() => {
  7. fetch("https://ngrok-free.app/api/test", {
  8. headers: {
  9. "Content-Type": "application/json",
  10. Accept: "application/json",
  11. },
  12. })
  13. .then((response) => response.json())
  14. .then((data) => setMessage(data.message))
  15. .catch((error) => console. Error(error));
  16. }, []);
  17. return (
  18. <View style={styles.container}>
  19. <Text>The message from the server is:</Text>
  20. <Text>{message}</Text>
  21. <StatusBar style="auto" />
  22. </View>
  23. );
  24. }
  25. const styles = StyleSheet.create({
  26. container: {
  27. flex: 1,
  28. backgroundColor: "#fff",
  29. alignItems: "center",
  30. justifyContent: "center",
  31. },
  32. });

hc8w905p

hc8w905p1#

这些是我将采取的一些步骤来调试问题:
1-您正在将请求发送到https://ngrok-free.app/api/test域,因此您的路由:

  1. Route::middleware('auth:sanctum')->get('/test',...

字符串
应该 Package (如果还没有完成,这里没有显示)在一个路由::域如下:

  1. Route::domain('https://ngrok-free.app')->group(function(){
  2. Route::middleware('auth:sanctum')->get('/test',...


2-首先,我将测试没有认证中间件的端点,它应该已经工作在这一点上
3-如果测试一个auth端点,你首先应该登录,然后发送你从laravel收到的auth token,并在头中包含任何请求,比如Authentication:'Bearer your-token'
4-我建议你从fetch转移到axios(使用fetch await而不是then/catch),因为它在某些情况下是有用的/必要的。
另外,您显示的错误#6来自config.sanctum.guard,我建议您将其保留为空(空数组)

展开查看全部

相关问题