John Davidson

php - PUT, DELETE redirecting with Lumen / Laravel

0 comments
Message:


I have a simple code like this in my "web.php" attempting to redirect a PUT request to another external "PUT" endpoint, the issue is that it is only redirecting it to the "GET" endpoint and not to the "PUT".


$router->put('endpoint/{id}', function ($id) use ($router) {
return redirect()->to( URL_address/newpoint/'.$id);
});

So how can I redirect it to the "PUT" ?



My Answer:
Lumen/Laravel application:

php
$app->put('/users/{id}', function ($id) use ($app) {
// Update user with id = $id
return response()->json(['message' => 'User updated successfully']);
});

$app->delete('/users/{id}', function ($id) use ($app) {
// Delete user with id = $id
return response()->json(['message' => 'User deleted successfully']);
});


If you want to redirect after a PUT or DELETE request, you can use the `redirect()` method provided by Laravel/Lumen. Here's an example of how you can redirect after a PUT request:

php
$app->put('/users/{id}', function ($id) use ($app) {
// Update user with id = $id
return redirect('/users')->with('message', 'User updated successfully');
});


And here's an example of how you can redirect after a DELETE request:

php
$app->delete('/users/{id}', function ($id) use ($app) {
// Delete user with id = $id
return redirect('/users')->with('message', 'User deleted successfully');
});


In the above examples, the `redirect()` method is used to redirect the user to the `/users` route after a PUT or DELETE request. The `with()` method is used to flash a message to the session, which can be accessed in the redirected route to display a success message to the user.

Rate this post

4 of 5 based on 5312 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us