Route fallback for POST methods in Laravel?
Asked Answered
K

7

9

Is there a way to check route fallback for post methods? This code works for any get url in my routes file i.e. I get this response("Page Not Found.") if I type and wrong GET url.

Is there a way to check the same for POST urls?

Route::fallback(function(){
    return response()->json([
        'status'    => false,
        'message'   => 'Page Not Found.',
    ], 404);
});
Kudu answered 2/4, 2019 at 7:27 Comment(0)
A
5
use Request;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

In app/Exceptions/Handler.php replace render function

public function render($request, Exception $exception)
    {
        if (Request::isMethod('post') && $exception instanceof MethodNotAllowedHttpException) {
            return response()->json([
                'message' => 'Page Not Found',
                'status' => false
                ], 500
            );
        }
        return parent::render($request, $exception);
    }

Or if you want both NotFound And MethodNotAllowed then

public function render($request, Exception $exception)
    {
        if ((Request::isMethod('post') && $exception instanceof MethodNotAllowedHttpException) || (Request::isMethod('post') && $exception instanceof NotFoundHttpException)) {
            return response()->json([
                'message' => 'Page Not Found',
                'status' => false
                ], 500
            );
        }
        return parent::render($request, $exception);
    }
Aramen answered 2/4, 2019 at 8:56 Comment(0)
B
15

Define custom fallback route at end of your routes/api.php

Route::any('{any}', function(){
    return response()->json([
        'status'    => false,
        'message'   => 'Page Not Found.',
    ], 404);
})->where('any', '.*');
Beaner answered 9/4, 2020 at 5:4 Comment(0)
A
5
use Request;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

In app/Exceptions/Handler.php replace render function

public function render($request, Exception $exception)
    {
        if (Request::isMethod('post') && $exception instanceof MethodNotAllowedHttpException) {
            return response()->json([
                'message' => 'Page Not Found',
                'status' => false
                ], 500
            );
        }
        return parent::render($request, $exception);
    }

Or if you want both NotFound And MethodNotAllowed then

public function render($request, Exception $exception)
    {
        if ((Request::isMethod('post') && $exception instanceof MethodNotAllowedHttpException) || (Request::isMethod('post') && $exception instanceof NotFoundHttpException)) {
            return response()->json([
                'message' => 'Page Not Found',
                'status' => false
                ], 500
            );
        }
        return parent::render($request, $exception);
    }
Aramen answered 2/4, 2019 at 8:56 Comment(0)
S
3

Put this script end of your routes file.

Route::any('{url?}/{sub_url?}', function(){
    return response()->json([
        'status'    => false,
        'message'   => 'Page Not Found.',
    ], 404);
})

It will automatically detect if someone try to hit any other routes like below.

laravel_project_path/public/any_string
laravel_project_path/public/any_string/any_string
Scavenger answered 2/4, 2019 at 7:36 Comment(2)
It's not working. And the given code is now not working on GET url as well. And do I need to pass something in {url} ?Kudu
This can be done only up to 2 depth. Use regex with where().Beaner
C
0

Try: In Route API

Route::fallback(function () { abort(500);  });

In resources folder

Follow:

  1. Go to resources
  2. Go to views
  3. Create errors folder
  4. Create blade 500.blade.php
Casefy answered 27/3, 2022 at 8:49 Comment(0)
A
0

Create an any request with any URL.

//in web.php

Route::any('{any}', function(){
    return 'Hello, I will accept any type of request!';
})->where('any', '.*');

You may receive '419 PAGE EXPIRED' error for POST requests. It is because of CsrfToken. You can globally disable the CSRF token check, which is not a recommended method as it causes security risks.

//in \App\Http\Middleware\VerifyCsrfToken.php

protected $except = [
        '*' //Allow for all, or write custom exceptions. 
    ];
Abundant answered 9/1, 2023 at 12:0 Comment(0)
R
0

REASON : composer has not seen this URL yet.

SOLUTION :

  1. check if this URL is defined to your composer through php artisan route:list command.
  2. if the URL is not defined """"use the php artisan route:clear command to refresh routes.
  3. then you can use php artisan route:list command again, to check that URL has been defined.
Raving answered 23/12, 2023 at 20:12 Comment(0)
F
-1

You can use given method to check POST URL and return 404 error page. Please keep the code in web.php at the last line.

Route::fallback(function(){
    return \Response::view('errors.404');
});
Foulard answered 28/4, 2020 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.