$api->version('v1', ['middleware' => 'api.auth'], function($api){
$api->get('auth/user', 'App\Http\Controllers\Api\ApiUserController#getAuthUser');
$api->get('auth/getInfo', 'App\Http\Controllers\Api\ApiUserAppointmentController#getInfo');
$api->get('auth/show/{id}', 'App\Http\Controllers\Api\ApiUserAppointmentController#show');
});
public function show($id)
{
echo $id;die;
}
Error
"message": "404 Not Found",
"status_code": 404,
"debug": {
"line": 161,
"file": "C:\\xampp\\htdocs\\G2Project\\medcrip\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\RouteCollection.php",
I stuck when adding parameter in get method don't know why this is say not found. if i remove {id} from route it works fine but when i do add {id} that says to me not found.
Please advice thanks in advance
To make the auth/show/{id} route work, you should use this URI:
/api/auth/show/53
instead of this:
/api/auth/show/?id=53
Related
I've been searching a solution in Google but couldn't find anything similar tho this.
I'm basically trying to create a custom response upon the error methodNotAllowed in the framework Laravel 8.x
So I have this route:
Route::get('/test', function(Request $request){
return response([
'status' => 200,
'data' => 'Test'
]);
});
On requesting GET:/api/test I'm getting the expected response:
{
"status": 200,
"data": "Test"
}
But when requesting POST:/api/test or any other method it obviously throws an error 405 Method Not Allowed because I haven't setup any router for this.
Is there a "cleen way" to change the error response from 405 Method Not Allowed to
{
"status": 405,
"data": "Method Not Allowed"
}
By a "cleen way" I mean not creating aditional 100 routes just for catching the right method.
The Solution was adding the custom Response to App\Exceptions\Handler by doing this:
Add this to the top:
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
And add the custom response to the method register:
public function register()
{
$this->renderable(function (MethodNotAllowedHttpException $e, $request) {
return response()->json([
'status' => 405,
'message' => 'Method Not Allowed'
], 405);
});
}
Source: https://laravel.com/docs/8.x/errors
I am using slim framework. But when i refresh the page i got the following error. My all paths are correct but still getting this error.
{
"message": "Slim Application Error",
"exception": [
{
"type": "DI\\DependencyException",
"code": 0,
"message": "Error while injecting dependencies into App\\Action\\User\\UserListDataTableAction: No entry or class found for 'App\\Domain\\User\\Service\\UserListDataTable'",
"file": "/opt/lampp/htdocs/slim/vendor/php-di/php-di/src/Definition/Resolver/ObjectCreator.php",
"line": 147
},
{
"type": "DI\\NotFoundException",
"code": 0,
"message": "No entry or class found for 'App\\Domain\\User\\Service\\UserListDataTable'",
"file": "/opt/lampp/htdocs/slim/vendor/php-di/php-di/src/Container.php",
"line": 135
}
]
}
<?php
namespace App\Action\User;
use App\Domain\User\Service\UserListDataTable;
use App\Responder\JsonResponder;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
final class UserListDataTableAction
{
private $userListDataTable;
private $responder;
public function __construct(
UserListDataTable $userListDataTable,
JsonResponder $responder
) {
$this->userListDataTable = $userListDataTable;
$this->responder = $responder;
}
}
Please guide me where i am wrong.
Any solution appreciated!
I have a common Larvel 8 project with this codes:
In the routes/api.php file:
Route::resources([
'menu/menu' => Menu\MenuController::class,
]);
In the app/Http/Controllers/Menu/MenuController.php file:
public function destroy(DestroyMenuRequest $request, Menu $menu) {
$menu->delete();
return Response::HTTP_OK;
}
In the DestroyMenuRequest.php file:
class DestroyMenuRequest extends FormRequest {
public function authorize() {
return $this->user()->can('destroy.menu');
}
public function rules() {
return [
'id' => 'required|integer|exists:menus,id',
];
}
}
Then I send a DELETE HTTP request to the url {{domain}}/api/menu/menu/2 and I get back this:
In header 422 (Unprocessable entry), and in the body:
{
"message": "The given data was invalid.",
"errors": {
"id": [
"The id field is required."
]
}
}
If I list routes with php artisan r:l I see the route with the right controller:
DELETE | api/menu/menu/{menu} | menu.destroy | App\Http\Controllers\Menu\MenuController#destroy | api auth:api
Any idea what is the solution?
Thanks!
As the comment above me pointed out, you are generating routes for resource controller.
In your routes list, you can see that the route does not expect an actual ID as the parameter, but rather it expects Menu model instance from which Laravel will automatically grab the ID.
Example if you are submitting a form:
<form method="POST" action="{{ route('menu.destroy', ['menu' => $menuObject]) }}">
#method("DELETE")
...
</form>
Where $menuObject is of type Models\Menu. In other words, it's an instance of Menu model you have created
I'm developing a Laravel 5.6 API and I'm using Resources and Collections, Route Model Binding.
To show an item, I currently use following code in my controller:
public function show(Todo $todo)
{
TodoResource::withoutWrapping();
return new TodoResource($todo);
}
In the Exceptions > Handler.php I have the following:
public function render($request, Exception $exception)
{
// This will replace our 404 response with
// a JSON response.
if ($exception instanceof ModelNotFoundException) {
return response()->json([
'error' => 'Resource not found'
], 404);
}
return parent::render($request, $exception);
}
This works perfectly when the item is found in the database. If the item is not in the database I get a (when using a browser):
"Sorry, the page you are looking for could not be found"
When using POSTMAN rest client, I'm getting
{
"message": "No query results for model [App\\Todo].",
"exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
....
....
I would like to simply retrieve a 404 error with text "Resource not found", using both a browser or POSTMAN.
* Update with Routing info *
In my api.php, I have the following:
Route::apiResource('todos', 'TodoController');
Route::fallback(function () {
return response()->json(['message' => 'Not Found!'], 404);
});
In web.php, I have:
Route::Resource('todos', 'TodoController');
What is the best way to achieve this?
Make sure to alias the exception class you are checking for.
use Illuminate\Database\Eloquent\ModelNotFoundException;
Without this you are checking for an instance of App\Exceptions\ModelNotFoundException.
Im using Dingo to build out an API and up until this point I've had no problems with the routes, until trying to add show into the controller, I'm just getting a 404.
Details here:
{
"error": {
"message": "404 Not Found",
"status_code": 404,
"debug": {
"line": 179,
"file": "/var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
"class": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
"trace": [
"#0 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(546): Illuminate\\Routing\\RouteCollection->match(Object(Dingo\\Api\\Http\\Request))",
"#1 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(525): Illuminate\\Routing\\Router->findRoute(Object(Dingo\\Api\\Http\\Request))",
"#2 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(511): Illuminate\\Routing\\Router->dispatchToRoute(Object(Dingo\\Api\\Http\\Request))",
"#3 /var/www/html/myapi/api/vendor/dingo/api/src/Routing/Adapter/Laravel.php(81): Illuminate\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))",
"#4 /var/www/html/myapi/api/vendor/dingo/api/src/Routing/Router.php(513): Dingo\\Api\\Routing\\Adapter\\Laravel->dispatch(Object(Dingo\\Api\\Http\\Request), 'v1')",
"#5 /var/www/html/myapi/api/vendor/dingo/api/src/Http/Middleware/Request.php(126): Dingo\\Api\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))",
"#6 /var/www/html/myapi/api/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/myapi/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
"#8 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
"#9 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
"#10 /var/www/html/myapi/api/vendor/dingo/api/src/Http/Middleware/Request.php(127): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
"#11 /var/www/html/myapi/api/vendor/dingo/api/src/Http/Middleware/Request.php(103): Dingo\\Api\\Http\\Middleware\\Request->sendRequestThroughRouter(Object(Dingo\\Api\\Http\\Request))",
"#12 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Dingo\\Api\\Http\\Middleware\\Request->handle(Object(Illuminate\\Http\\Request), Object(Closure))",
"#13 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
"#14 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
"#15 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
"#16 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))",
"#17 /var/www/html/myapi/api/public/index.php(54): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))",
"#18 {main}"
]
}
}
}
Here is a portion of my route file
api.php
$api->group(['middleware' => 'jwt.auth'], function(Router $api) {
$api->get('protected', function() {
return response()->json([
'message' => 'Access to protected resources granted! You are seeing this text as you provided the token correctly.'
]);
});
$api->get('refresh', [
'middleware' => 'jwt.refresh',
function() {
return response()->json([
'message' => 'By accessing this endpoint, you can refresh your access token at each request. Check out this response headers!'
]);
}
]);
$api->get('leads', 'App\\Api\\V1\\Controllers\\LeadController#index');
$api->get('leads/{$id}', 'App\\Api\\V1\\Controllers\\LeadController#show');
$api->post('leads/store', 'App\\Api\\V1\\Controllers\\LeadController#store');
$api->put('leads/update/{$id}', 'App\\Api\\V1\\Controllers\\LeadController#update');
$api->post('leads/update/{$id}', 'App\\Api\\V1\\Controllers\\LeadController#update');
});
And the show function in the controller:
LeadController.php
public function show(Lead $leads, $id)
{
dd($id);
$lead = Lead::with('user', 'source', 'industry', 'status')->find($id);
//if(!$lead)
// return $this->response->error('invalid_data', 400);
//return fractal($lead, new LeadTransformer())->respond();
}
I've attempted to do a Die'n'Dump to ensure the ID is coming through, but it doesn't appear to be getting that far. Both the #index and #store work no problems and if I change LeadController#index to LeadController#show, the route works, and I of course get the error regarding the second parameter.
Really at a loss as to why this won't work, so help here would be really appreciated.
Wow, from looking too hard, and not enough sleep, the reason it didn't work was because the variable was set as {$id} not {id} as it should have been.
When using these variables, don't use the $ sign!