Laravel 中的 API 路由错误
Error on API Routes in Laravel
我在 Laravel 中的 API 路线上遇到错误。我用过 Dingo 包。安装成功,在 app.php 中添加,由 artisan.api.php 生成。
在路由器文件中,代码是:-
use Dingo\Api\Routing\Helpers;
$api = app('Dingo\Api\Routing\Router');
/*$api = app('Dingo\Api\Facade\Route');*/
$api->version('v1', function($api){
$api->get('hello', function(){
return "Hello !!!!!";
});
});
在与 php artisan api:routes 核实后,我得到了这样的路线 /api/routes/。但是,当我点击 url 时,会出现一个像这样的长错误:-
{"message":"404 Not Found","status_code":404,"debug":{"line":179,"file":"\/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/RouteCollection.php","class":"Symfony\Component\HttpKernel\Exception\NotFoundHttpException","trace":["#0 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Router.php(612): Illuminate\Routing\RouteCollection->match(Object(Dingo\Api\Http\Request))","#1 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Router.php(601): Illuminate\Routing\Router->findRoute(Object(Dingo\Api\Http\Request))","#2 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Router.php(590): Illuminate\Routing\Router->dispatchToRoute(Object(Dingo\Api\Http\Request))","#3 \/var\/www\/html\/exam\/vendor\/dingo\/api\/src\/Routing\/Adapter\/Laravel.php(81): Illuminate\Routing\Router->dispatch(Object(Dingo\Api\Http\Request))","#4 \/var\/www\/html\/exam\/vendor\/dingo\/api\/src\/Routing\/Router.php(512): Dingo\Api\Routing\Adapter\Laravel->dispatch(Object(Dingo\Api\Http\Request), 'v1')","#5 \/var\/www\/html\/exam\/vendor\/dingo\/api\/src\/Http\/Middleware\/Request.php(126): Dingo\Api\Routing\Router->dispatch(Object(Dingo\Api\Http\Request))","#6 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(114): Dingo\Api\Http\Middleware\Request->Dingo\Api\Http\Middleware\{closure}(Object(Dingo\Api\Http\Request))","#7 \/var\/www\/html\/exam\/vendor\/fideloper\/proxy\/src\/TrustProxies.php(56): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))","#8 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(149): Fideloper\Proxy\TrustProxies->handle(Object(Dingo\Api\Http\Request), Object(Closure))","#9 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Http\/Middleware\/TransformsRequest.php(30): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))","#10 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Dingo\Api\Http\Request), Object(Closure))","#11 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Http\/Middleware\/TransformsRequest.php(30): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))","#12 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Dingo\Api\Http\Request), Object(Closure))","#13 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Http\/Middleware\/ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))","#14 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Dingo\Api\Http\Request), Object(Closure))","#15 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Http\/Middleware\/CheckForMaintenanceMode.php(46): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))","#16 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Dingo\Api\Http\Request), Object(Closure))","#17 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(102): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))","#18 \/var\/www\/html\/exam\/vendor\/dingo\/api\/src\/Http\/Middleware\/Request.php(127): Illuminate\Pipeline\Pipeline->then(Object(Closure))","#19 \/var\/www\/html\/exam\/vendor\/dingo\/api\/src\/Http\/Middleware\/Request.php(103): Dingo\Api\Http\Middleware\Request->sendRequestThroughRouter(Object(Dingo\Api\Http\Request))","#20 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(149): Dingo\Api\Http\Middleware\Request->handle(Object(Dingo\Api\Http\Request), Object(Closure))","#21 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))","#22 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))","#23 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Http\/Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure))","#24 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Http\/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))","#25 \/var\/www\/html\/exam\/public\/index.php(55): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))","#26 \/var\/www\/html\/exam\/server.php(21): require_once('\/var\/www\/html\/e...')","#27 {main}"]}}
我得到了答案。
我正在尝试本地主机。
我给出了 API_DOMAIN 和 API_PREFIX 的值。但是,知道在这种情况下 API_DOMAIN 不是必需的。
我在 Laravel 中的 API 路线上遇到错误。我用过 Dingo 包。安装成功,在 app.php 中添加,由 artisan.api.php 生成。
在路由器文件中,代码是:-
use Dingo\Api\Routing\Helpers;
$api = app('Dingo\Api\Routing\Router');
/*$api = app('Dingo\Api\Facade\Route');*/
$api->version('v1', function($api){
$api->get('hello', function(){
return "Hello !!!!!";
});
});
在与 php artisan api:routes 核实后,我得到了这样的路线 /api/routes/。但是,当我点击 url 时,会出现一个像这样的长错误:-
{"message":"404 Not Found","status_code":404,"debug":{"line":179,"file":"\/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/RouteCollection.php","class":"Symfony\Component\HttpKernel\Exception\NotFoundHttpException","trace":["#0 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Router.php(612): Illuminate\Routing\RouteCollection->match(Object(Dingo\Api\Http\Request))","#1 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Router.php(601): Illuminate\Routing\Router->findRoute(Object(Dingo\Api\Http\Request))","#2 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Router.php(590): Illuminate\Routing\Router->dispatchToRoute(Object(Dingo\Api\Http\Request))","#3 \/var\/www\/html\/exam\/vendor\/dingo\/api\/src\/Routing\/Adapter\/Laravel.php(81): Illuminate\Routing\Router->dispatch(Object(Dingo\Api\Http\Request))","#4 \/var\/www\/html\/exam\/vendor\/dingo\/api\/src\/Routing\/Router.php(512): Dingo\Api\Routing\Adapter\Laravel->dispatch(Object(Dingo\Api\Http\Request), 'v1')","#5 \/var\/www\/html\/exam\/vendor\/dingo\/api\/src\/Http\/Middleware\/Request.php(126): Dingo\Api\Routing\Router->dispatch(Object(Dingo\Api\Http\Request))","#6 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(114): Dingo\Api\Http\Middleware\Request->Dingo\Api\Http\Middleware\{closure}(Object(Dingo\Api\Http\Request))","#7 \/var\/www\/html\/exam\/vendor\/fideloper\/proxy\/src\/TrustProxies.php(56): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))","#8 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(149): Fideloper\Proxy\TrustProxies->handle(Object(Dingo\Api\Http\Request), Object(Closure))","#9 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Http\/Middleware\/TransformsRequest.php(30): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))","#10 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Dingo\Api\Http\Request), Object(Closure))","#11 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Http\/Middleware\/TransformsRequest.php(30): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))","#12 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Dingo\Api\Http\Request), Object(Closure))","#13 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Http\/Middleware\/ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))","#14 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Dingo\Api\Http\Request), Object(Closure))","#15 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Http\/Middleware\/CheckForMaintenanceMode.php(46): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))","#16 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Dingo\Api\Http\Request), Object(Closure))","#17 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(102): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))","#18 \/var\/www\/html\/exam\/vendor\/dingo\/api\/src\/Http\/Middleware\/Request.php(127): Illuminate\Pipeline\Pipeline->then(Object(Closure))","#19 \/var\/www\/html\/exam\/vendor\/dingo\/api\/src\/Http\/Middleware\/Request.php(103): Dingo\Api\Http\Middleware\Request->sendRequestThroughRouter(Object(Dingo\Api\Http\Request))","#20 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(149): Dingo\Api\Http\Middleware\Request->handle(Object(Dingo\Api\Http\Request), Object(Closure))","#21 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))","#22 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))","#23 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Http\/Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure))","#24 \/var\/www\/html\/exam\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Http\/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))","#25 \/var\/www\/html\/exam\/public\/index.php(55): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))","#26 \/var\/www\/html\/exam\/server.php(21): require_once('\/var\/www\/html\/e...')","#27 {main}"]}}
我得到了答案。 我正在尝试本地主机。 我给出了 API_DOMAIN 和 API_PREFIX 的值。但是,知道在这种情况下 API_DOMAIN 不是必需的。