  {"id":461,"date":"2022-09-24T16:08:28","date_gmt":"2022-09-24T16:08:28","guid":{"rendered":"https:\/\/247blogs.com\/wp\/2022\/09\/24\/laravel-9-tutorial-how-to-create-custom-404-page-in-laravel-9-application\/"},"modified":"2022-09-24T17:37:12","modified_gmt":"2022-09-24T17:37:12","slug":"laravel-9-tutorial-how-to-create-custom-404-page-in-laravel-9-application","status":"publish","type":"post","link":"https:\/\/247blogs.com\/wp\/laravel-9-tutorial-how-to-create-custom-404-page-in-laravel-9-application\/","title":{"rendered":"Laravel-9 Tutorial &#8211; How to Create Custom 404 Page in Laravel 9 Application"},"content":{"rendered":"<div id=\"\">\n<p>Through this Laravel tutorial, we would like to share with you how to easily create a custom error page 404 and 403, 500, 419, 255, 405 error pages in the Laravel application.<\/p>\n<p>While working on a web application errors or more precisely exception can manifest at any time, Laravel framework handles exceptions beautifully.<\/p>\n<p>It offers a handy error handler class which looks for almost every errors displayed in Laravel environment and returns an adequate response.<\/p>\n<p>Generically, you can get the default error response if you configure debug property to false; nevertheless, you can create a custom error handling template.<\/p>\n<p>Let\u2019s handle custom errors in Laravel.<\/p>\n<h2>Create New Laravel Project<\/h2>\n<p>Start this tutorial by creating a brand new Laravel project using the following command:<\/p>\n<pre class=\"  language-bash\"><code class=\"  language-bash\">composer create-project laravel\/laravel --prefer-dist laravel-error-handling-example<\/code><\/pre>\n<h2>Create Custom 404 Error Page<\/h2>\n<p>You need to create blade views for error pages, move to this path resources\/views\/ inside here create errors folder and within the directory create 404.blade.php file. It will redirect you to the 404 page if you don\u2019t find the associated URL.<\/p>\n<p>Similarly you can create rest of the error handling blade views for 403, 500, 419, 255 and 405 exceptions:<\/p>\n<p>Include the following code in <strong>resources\/views\/errors\/404.blade.php<\/strong> error file.<\/p>\n<pre class=\" language-markup\"><code class=\"  language-php\"><span class=\"token operator\"><<\/span><span class=\"token operator\">!<\/span><span class=\"token constant\">DOCTYPE<\/span> html<span class=\"token operator\">><\/span>\n<span class=\"token operator\"><<\/span>html lang<span class=\"token operator\">=<\/span><span class=\"token string double-quoted-string\">\"{{ str_replace('_', '-', app()->getLocale()) }}\"<\/span><span class=\"token operator\">><\/span>\n<span class=\"token operator\"><<\/span>head<span class=\"token operator\">><\/span>\n    <span class=\"token operator\"><<\/span>meta charset<span class=\"token operator\">=<\/span><span class=\"token string double-quoted-string\">\"utf-8\"<\/span><span class=\"token operator\">><\/span>\n    <span class=\"token operator\"><<\/span>meta name<span class=\"token operator\">=<\/span><span class=\"token string double-quoted-string\">\"viewport\"<\/span> content<span class=\"token operator\">=<\/span><span class=\"token string double-quoted-string\">\"width=device-width, initial-scale=1\"<\/span><span class=\"token operator\">><\/span>\n    <span class=\"token operator\"><<\/span>title<span class=\"token operator\">><\/span><span class=\"token number\">404<\/span> Custom Error Page Example<span class=\"token operator\"><<\/span><span class=\"token operator\">\/<\/span>title<span class=\"token operator\">><\/span>\n    <span class=\"token operator\"><<\/span>link rel<span class=\"token operator\">=<\/span><span class=\"token string double-quoted-string\">\"stylesheet\"<\/span> href<span class=\"token operator\">=<\/span><span class=\"token string double-quoted-string\">\"https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.4.1\/css\/bootstrap.min.css\"<\/span><span class=\"token operator\">><\/span>\n<span class=\"token operator\"><<\/span><span class=\"token operator\">\/<\/span>head<span class=\"token operator\">><\/span>\n<span class=\"token operator\"><<\/span>body<span class=\"token operator\">><\/span>\n    <span class=\"token operator\"><<\/span>div <span class=\"token keyword\">class<\/span><span class=\"token operator\">=<\/span><span class=\"token string double-quoted-string\">\"container mt-5 pt-5\"<\/span><span class=\"token operator\">><\/span>\n        <span class=\"token operator\"><<\/span>div <span class=\"token keyword\">class<\/span><span class=\"token operator\">=<\/span><span class=\"token string double-quoted-string\">\"alert alert-danger text-center\"<\/span><span class=\"token operator\">><\/span>\n            <span class=\"token operator\"><<\/span>h2 <span class=\"token keyword\">class<\/span><span class=\"token operator\">=<\/span><span class=\"token string double-quoted-string\">\"display-3\"<\/span><span class=\"token operator\">><\/span><span class=\"token number\">404<\/span><span class=\"token operator\"><<\/span><span class=\"token operator\">\/<\/span>h2<span class=\"token operator\">><\/span>\n            <span class=\"token operator\"><<\/span>p <span class=\"token keyword\">class<\/span><span class=\"token operator\">=<\/span><span class=\"token string double-quoted-string\">\"display-5\"<\/span><span class=\"token operator\">><\/span>Oops<span class=\"token operator\">!<\/span> Something is wrong<span class=\"token operator\">.<\/span><span class=\"token operator\"><<\/span><span class=\"token operator\">\/<\/span>p<span class=\"token operator\">><\/span>\n        <span class=\"token operator\"><<\/span><span class=\"token operator\">\/<\/span>div<span class=\"token operator\">><\/span>\n    <span class=\"token operator\"><<\/span><span class=\"token operator\">\/<\/span>div<span class=\"token operator\">><\/span>\n<span class=\"token operator\"><<\/span><span class=\"token operator\">\/<\/span>body<span class=\"token operator\">><\/span>\n<span class=\"token operator\"><<\/span><span class=\"token operator\">\/<\/span>html<span class=\"token operator\">><\/span><\/code><\/pre>\n<p>To test out the 404 custom error template, you need to start the application.<\/p>\n<pre class=\" language-markup\"><code class=\" language-bash\">php artisan serve<\/code><\/pre>\n<p>As you know, 404 errors occur when you visit the non-existed link, so type the wrong URL in the browser\u2019s address bar.<\/p>\n<pre class=\" language-markup\"><code class=\" language-properties\"><span class=\"token attr-name\">http<\/span><span class=\"token punctuation\">:<\/span><span class=\"token attr-value\">\/\/127.0.0.1:8000\/not<\/span><\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.positronx.io\/wp-content\/uploads\/2020\/11\/custom-error-12211-01.jpg\" alt=\"Create Custom 404 Page in Laravel\" width=\"800\" height=\"245\" class=\"alignnone size-full wp-image-12219\" srcset=\"https:\/\/www.positronx.io\/wp-content\/uploads\/2020\/11\/custom-error-12211-01.jpg 800w, https:\/\/www.positronx.io\/wp-content\/uploads\/2020\/11\/custom-error-12211-01-768x235.jpg 768w, https:\/\/www.positronx.io\/wp-content\/uploads\/2020\/11\/custom-error-12211-01-302x92.jpg 302w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\"><\/p>\n<p>Easy, isn\u2019t it. This tutorial is over, i hope you liked it.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Through this Laravel tutorial, we would like to share with you how to easily create a custom error page 404 and 403, 500, 419, 255, 405 error pages in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":462,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,14],"tags":[],"class_list":["post-461","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","category-tutorial","clearfix"],"_links":{"self":[{"href":"https:\/\/247blogs.com\/wp\/wp-json\/wp\/v2\/posts\/461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/247blogs.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/247blogs.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/247blogs.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/247blogs.com\/wp\/wp-json\/wp\/v2\/comments?post=461"}],"version-history":[{"count":1,"href":"https:\/\/247blogs.com\/wp\/wp-json\/wp\/v2\/posts\/461\/revisions"}],"predecessor-version":[{"id":642,"href":"https:\/\/247blogs.com\/wp\/wp-json\/wp\/v2\/posts\/461\/revisions\/642"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/247blogs.com\/wp\/wp-json\/wp\/v2\/media\/462"}],"wp:attachment":[{"href":"https:\/\/247blogs.com\/wp\/wp-json\/wp\/v2\/media?parent=461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/247blogs.com\/wp\/wp-json\/wp\/v2\/categories?post=461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/247blogs.com\/wp\/wp-json\/wp\/v2\/tags?post=461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}