Skip to content

Commit

Permalink
feat: create handler to redirect 404 page if not found
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhongit committed Nov 23, 2024
1 parent 2907552 commit ff431f0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Exceptions/NotFoundHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace CSlant\Blog\Core\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Response;
use Throwable;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class NotFoundHandler extends ExceptionHandler
{
public function render($request, Throwable $e): Response|JsonResponse|RedirectResponse|\Symfony\Component\HttpFoundation\Response
{
if ($e instanceof NotFoundHttpException) {
return redirect('https://cslant.com/not-found', 301);
}

return parent::render($request, $e);
}
}
3 changes: 3 additions & 0 deletions src/Providers/BlogCoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CSlant\Blog\Core\Providers;

use CSlant\Blog\Core\Exceptions\NotFoundHandler;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\ServiceProvider;

class BlogCoreServiceProvider extends ServiceProvider
Expand All @@ -25,6 +27,7 @@ public function boot(): void
*/
public function register(): void
{
$this->app->singleton(ExceptionHandler::class, NotFoundHandler::class);
$this->registerConfigs();

$this->registerCommands();
Expand Down

0 comments on commit ff431f0

Please sign in to comment.