0

When directly accessing a URL which use Route::post(), Laravel shows the following 405 Method Not Allowed error screen:

Oops! An Error Occurred The server returned a "405 Method Not Allowed". Something is broken. Please let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused.

I think it is not good to give bad people information about the correct method. Am I right?

How to hide 405 Method Not Allowed error screen and show 404 Not Found error instead?

It is better to configure .env or Laravel than to configure nginx or apache. And I don't want to write 404 error redirect in every controller methods.

I want to hide the error screen only in the production because it is good to know that method is not correct while developing (debug mode is on).

user_
  • 852
  • 7
  • 23
  • just put `.env` inside `APP_ENV=production` and `APP_DEBUG=false` – Kamlesh Paul Nov 25 '21 at 04:34
  • "*Am I right*" - well ... no, not really. If your users are seeing that error, there's a problem in your application flow, or broken links. There should be no way for a normal user to see that when using your app. How is it happening, how can a user do a GET on a POST route? Are you using [post-redirect-get (PRG)](https://stackoverflow.com/questions/10827242/understanding-the-post-redirect-get-pattern)? – Don't Panic Nov 25 '21 at 07:08
  • @Don'tPanic thank you for the comment. in normal web pages, you are right. i don't use the PRG, the URL is for an API which accepts only POST and the API should be used only by specific users who knows there is the API. so, i think it is not good that other people know API URL exists by showing 405 error. – user_ Nov 25 '21 at 08:47
  • @KamleshPaul thank you for the comment. The error screen and message in my question is shown when `.env` is `APP_ENV=production` and `APP_DEBUG=false`. When `APP_DEBUG=true`, Laravel's debug screen is shown. – user_ Nov 25 '21 at 08:50
  • Does this answer your question? [how to handle 405 error for displaying as webpage laravel](https://stackoverflow.com/questions/58829850/how-to-handle-405-error-for-displaying-as-webpage-laravel) – Don't Panic Nov 25 '21 at 23:44
  • @Don'tPanic thank you. but i just want to hide 405 error from other people by Laravel's default functionality if such functionality exists. i think the user of my api can handle 405 error. i understand why Laravel shows 405 error, but i thought Laravel might offer the option to show 404 error instead of 405 when a route of GET is not found. i think 404 is more secure than 405. – user_ Nov 26 '21 at 12:25
  • Maybe I don't understand, but AFAICT the answer I linked to shows you *exactly* how to do what you are asking - it even includes code you can copy-paste. It also links to the docs which explains the more general case of how to override any of Laravel's error rendering. In the question, the code returns a JSON 405 response - you would replace that with a plain 404 response, eg maybe `abort(404)`, or maybe `return response()->view('some.view', $data, 404);`, [plenty of examples here on SO of how to 404](https://stackoverflow.com/questions/42614264/how-to-make-laravel-5-return-404-status-code) – Don't Panic Nov 26 '21 at 21:05
  • @Don'tPanic thank you for the link. i know that 404 page can be shown if i write code in the controller. what i'm looking for is doing it without writing code in the controller. – user_ Nov 28 '21 at 02:36
  • 1
    I did not suggest writing code in the controller. The question I linked as a duplicate does not suggest writing code in the controller. I am not sure how else to explain it. The answer to your question is the accepted answer in the question I linked 2 days ago: https://stackoverflow.com/questions/58829850/how-to-handle-405-error-for-displaying-as-webpage-laravel It shows how to catch the `MethodNotAllowedHttpException`, and do anything you want with it. Eg throw a 404, instead of a 405. – Don't Panic Nov 28 '21 at 03:27
  • @Don'tPanic sorry for my misunderstanding. i panicked. the accepted answer https://stackoverflow.com/questions/58829850/how-to-handle-405-error-for-displaying-as-webpage-laravel and the link https://laravel.com/docs/5.8/errors#render-method can solve my question. thank you! – user_ Nov 28 '21 at 03:47
  • Glad to help, and glad it makes sense :-) If you agree this question is a duplicate, I suggest we close it as a duplicate. – Don't Panic Nov 28 '21 at 03:53
  • @Don'tPanic i agree with you. please close it. i don't know how to close it as a duplicate myself. – user_ Nov 28 '21 at 05:53
  • @Don'tPanic but the title of question which already answered is misleading for me. so, i will post an answer myself and accept it if this question is not closed until accepted. closed question might be deleted later. – user_ Nov 28 '21 at 10:23

1 Answers1

0

Thanks to @Don'tPanic in the comments of my question, I found the answer.

Laravel offers the option to hide 405 error screen.

You can change 405 error to 404 error and display 404 Not Found error page instead of 405 error page without writing code in every controller action method.

For more detail, see the answer and documentation below:

how to handle 405 error for displaying as webpage laravel

The Render Method https://laravel.com/docs/5.8/errors#render-method

user_
  • 852
  • 7
  • 23