CodeIgniter Forums

Full Version: Multiple Custom 404
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I'm creating a feature for my website that requires to send a custom error page, while keeping the default one for the rest of the website.
The controller haves to send different error code (404, 501...) and its corresponding page using a code similar to this :
Code:
throw new ImageException(404, "error Message")
or just like this if it's not possible (separate exception for different error code) :
Code:
throw new ImageNotFound("error message");

It is possible to achieve such thing ?

(I tried this, but it doesn't work : )
Image.php :
PHP Code:
// ...
 
public function index()
 {
  throw new ImageException(""); 
}
// ... 

ImageException.php :
PHP Code:
<?php

namespace App\Controllers\Img;

use 
CodeIgniter\Exceptions\ExceptionInterface;
use 
RuntimeException;

class 
ImageException extends RuntimeException implements ExceptionInterface
{
 protected 
$log false;
 protected 
$code 404;

 public static function 
forPageNotFound(string $message null)
 {
   return view("img/404");
   //return new static($message ?? lang('HTTP.pageNotFound'));
 
}

I'm not sure if I get your situation correctly. Do you want to throw an exception when a certain page is accessed in the browser? So, for example, if a user accesses the /image page defined by the Image controller you want to throw a 404 error?
PHP Code:
public function index()
{
    throw 
ImageException::forPageNotFound();

Many thanks.