Skip to content
Advertisement

how to make an automatic redirect to 404 every $slug that is not in views [closed]

here is my code, i need to redirect every $slug that is not in views to 404 page automaticly

class HomeController extends Controller
{
    public function getPages($slug)
    {
        return view($slug);
    }

    public function getHome()
    {
        return view('home');
    }
}

Advertisement

Answer

You can use

view()->exists($slug) 

to check wheater the view exist or not

if not present you can use below snippet to redirect to 404

return abort(404);

complete code:

class HomeController extends Controller
{
    public function getPages($slug) 
    {
        if(view()->exists($slug))
          return view($slug);
        else
          return abort(404);
    }
    public function getHome()
    {

        return view('home');
    }
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement