Welcome Guest, Not a member yet? Register   Sign In
Multiple Custom 404
#1
Sad 

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'));
 
}

Actually learning CodeIgniter4 and creating a complex CMS system for fan-translations teams (Kagescan)
@LoganTann on GitHub // French // He-Him
Reply
#2

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();

Reply
#3

Many thanks.
Reply
#4

(07-16-2021, 08:37 AM)paulbalandan Wrote: 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();

Hi, thanks for the reply.
I managed to solve this, but the question was 'is there a way to return 404 error code with a custom view in the body response'.

I no longer need an anwser with this question, I complete it manually using the response class.
Actually learning CodeIgniter4 and creating a complex CMS system for fan-translations teams (Kagescan)
@LoganTann on GitHub // French // He-Him
Reply




Theme © iAndrew 2016 - Forum software by © MyBB