dimanche 29 avril 2018

Laravel: Catch all nonexistent pages

I want to make a 404 page for all nonexistent pages. For example I have this code

public function category($category)
    {
        $category = Category::find($category);
        if($category === null) return abort(404); // return 404 page with 404 header status error
        return view('pages.category', compact('category'));
    }

My routes file

Route::get('/category/{category}', 'CategoryController@category');

I have another components like subcategories, articles, reviews and etc. In first case I can type app/category/1234.
If my app doesn't have category where id = 1234 in category table then without checking variable === null I can get a default but empty page and it will be with a 200 http status. It's not normal.

I can check for null in all components but I think it's not correct.

And one more question about SQL error. "id" field have integer type and if I go to app/category/1sdsds in browser I will get

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer

How can I fix it?

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire