PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 24, 2022

[FIXED] Laravel 5.1 custom 404 based on route

 January 24, 2022     http-status-code-404, laravel, laravel-5.1, php     No comments   

Issue

I'm new to laravel. I want to display a custom 404 error depending on requested url.

Example:
site.com/somepage
and
site.com/admin/somepage

I've see this answer but it's for laravel 4. I can't find the equivalent answer for Laravel 5.1

Please help. Thanks!


-- ANSWER --

My code based on suggestion by Abdul Basit

//Handle Route Not Found
if ($e instanceof NotFoundHttpException) {
    //Ajax Requests
    if ($request->ajax() || $request->wantsJson()) {
        $data = [
            'error' => true,
            'message' => 'The route is not defined',
        ];
        return Response::json($data, '404');
    } else {
        //Return view
        if (Request::is('admin/*')) {
            return Response::view('admin.errors.404', array(), 404);
        } else {
            return Response::view('errors.404', array(), 404);
        }
    }
}

//Handle HTTP Method Not Allowed
if ($e instanceof MethodNotAllowedHttpException) {
    //Ajax Requests
    if ($request->ajax() || $request->wantsJson()) {
        $data = [
            'error' => true,
            'message' => 'The http method not allowed',
        ];
        return Response::json($data, '404');
    } else {
        //Return view
        if (Request::is('admin/*')) {
            return Response::view('admin.errors.404', array(), 404);
        } else {
            return Response::view('errors.404', array(), 404);
        }
    }
}

Solution

You can do the logic for that in render method of App\Exceptions\Handler in app\Exceptions\Handler.php

public function render($request, Exception $e)
{
    //Handle Route Not Found
    if ($e instanceof Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
    {
        //Ajax Requests
        if($request->ajax() || $request->wantsJson())
        {
            $data = [
                'error'   => true,
                'message' => 'The route is not defined',
            ];
            return Response::json($data, '404');
        }
        else
        {
            //Return view 
            return response()->view('404');
        }
    }
    //Handle HTTP Method Not Allowed
    if ($e instanceof Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException)
    {
        //Ajax Requests
        if($request->ajax() || $request->wantsJson())
        {
            $data = [
                'error'   => true,
                'message' => 'The http method not allowed',
            ];
            return Response::json($data, '404');
        }
        else
        {
            //Return view 
            return response()->view('404'); 
        }
    }
    return parent::render($request, $e);
}


Answered By - Basit
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing