Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shows "message":"404 Not Found", while using dingo to create api in laravel 5.1

I have configure dingo package in laravel 5.1 but while I try to access

http://localhost:8000/test

it is showing following error

{"message":"404 Not Found","status_code":404,"debug":{"line":161,"file":"C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\RouteCollection.php","class":"Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException","trace":["#0 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(750): Illuminate\\Routing\\RouteCollection->match(Object(Dingo\\Api\\Http\\Request))","#1 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(659): Illuminate\\Routing\\Router->findRoute(Object(Dingo\\Api\\Http\\Request))","#2 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(635): Illuminate\\Routing\\Router->dispatchToRoute(Object(Dingo\\Api\\Http\\Request))","#3 C:\\wamp64\\www\\second\\vendor\\dingo\\api\\src\\Routing\\Adapter\\Laravel.php(81): Illuminate\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))","#4 C:\\wamp64\\www\\second\\vendor\\dingo\\api\\src\\Routing\\Router.php(513): Dingo\\Api\\Routing\\Adapter\\Laravel->dispatch(Object(Dingo\\Api\\Http\\Request), 'v1')","#5 C:\\wamp64\\www\\second\\vendor\\dingo\\api\\src\\Http\\Middleware\\Request.php(126): Dingo\\Api\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))","#6 [internal function]: Dingo\\Api\\Http\\Middleware\\Request->Dingo\\Api\\Http\\Middleware\\{closure}(Object(Dingo\\Api\\Http\\Request))","#7 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(139): call_user_func(Object(Closure), Object(Dingo\\Api\\Http\\Request))","#8 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken.php(50): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))","#9 [internal function]: Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))","#10 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(124): call_user_func_array(Array, Array)","#11 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Middleware\\ShareErrorsFromSession.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))","#12 [internal function]: Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))","#13 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(124): call_user_func_array(Array, Array)","#14 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php(62): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))","#15 [internal function]: Illuminate\\Session\\Middleware\\StartSession->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))","#16 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(124): call_user_func_array(Array, Array)","#17 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))","#18 [internal function]: Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))","#19 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(124): call_user_func_array(Array, Array)","#20 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\EncryptCookies.php(59): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))","#21 [internal function]: Illuminate\\Cookie\\Middleware\\EncryptCookies->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))","#22 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(124): call_user_func_array(Array, Array)","#23 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode.php(44): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))","#24 [internal function]: Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))","#25 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(124): call_user_func_array(Array, Array)","#26 [internal function]: Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))","#27 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(103): call_user_func(Object(Closure), Object(Dingo\\Api\\Http\\Request))","#28 C:\\wamp64\\www\\second\\vendor\\dingo\\api\\src\\Http\\Middleware\\Request.php(127): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))","#29 C:\\wamp64\\www\\second\\vendor\\dingo\\api\\src\\Http\\Middleware\\Request.php(103): Dingo\\Api\\Http\\Middleware\\Request->sendRequestThroughRouter(Object(Dingo\\Api\\Http\\Request))","#30 [internal function]: Dingo\\Api\\Http\\Middleware\\Request->handle(Object(Illuminate\\Http\\Request), Object(Closure))","#31 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(124): call_user_func_array(Array, Array)","#32 [internal function]: Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))","#33 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\\Http\\Request))","#34 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(122): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))","#35 C:\\wamp64\\www\\second\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(87): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))","#36 C:\\wamp64\\www\\second\\public\\index.php(54): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))","#37 C:\\wamp64\\www\\second\\server.php(21): require_once('C:\\\\wamp64\\\\www\\\\s...')","#38 {main}"]}}

I have following code in routes.php

<?php

$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {
    $api->get('test', 'App\Http\Controllers\ApiController@index');
});

Route::get('/', function () {
    return view('welcome');
});

While U check for api:route, it shows following following route:

enter image description here

like image 838
santosh Avatar asked Feb 13 '17 11:02

santosh


People also ask

How to handle error messages in Laravel with web and API?

If you’re building a Laravel project with both Web and API sides, you need to customize error messages for each of them separately. In web-view there should be error pages, and API exceptions should return JSON with status codes. How to handle it? I will show you an example with case of Model Not Found 404.

Is it possible to set a fallback route in Laravel 404 page?

I’d encourage you to read through Mohamed Said’s Better 404 responses using Laravel +5.5 post and his pull request for the feature 5.5 Add ability to set a fallback route by themsaid . Our release announcement post Laravel 5.5.5 Released With a New Route Fallback to Help Customize Your 404 Views has more information on this feature as well.

How do I get Started with Laravel?

To get started, create a Laravel application. To do this, run the following command in your terminal: or, if you have installed the Laravel Installer as a global composer dependency: Next, start up the Laravel server if it’s not already running: You will visit your application on http://localhost:8000. Create a new database for your application.

How to generate url from user profile in Laravel?

Normally you could fix this by just doing /profile/ { { $user->id }}. However I suggest you should use Laravel's URL generator helpers to generate URLs. The second parameter in route will fill in required route parameters.


Video Answer


1 Answers

First: You need to run

php artisan api:cache

after adding or editing on your routes.php file. Dingo API get your routes from route cache. You can not define anonymous function on your route file. Dingo through an error while caching route. Please move your welcome view into a controller's method. Then run php artisan api:cache. I hope this will solve your problem. Additionally you can check second solution.

Second:

Dingo have a couple of configuration that need to be on your .env file.

API_PREFIX=api

Tells you that all of your api endpoint will be prefix by "api". So your api endpoint for test will be.

http://localhost:8000/api/test
like image 99
Tuhin Bepari Avatar answered Oct 06 '22 00:10

Tuhin Bepari