0

I am looking to return a view from the HttpException that is triggered by my package's middleware, currently, it returns a static 403 exception and I'd like to change that to a custom view from the package.

I have attempted returning a view in the exception itself, however, that proved unfruitful due to a receiving a TypeError. TypeError Return value of Username\Roles\Exceptions\InsufficientRoleException::forRoles() must be an instance of Username\Roles\Exceptions\InsufficientUsergroupException, instance of Illuminate\View\View returned

Here's the block of code itself, if anybody knows a way that this can be done without modifying the App\Exceptions\Hander.php it would be amazing, as I'm trying to keep this within package code itself and to not make manual edits for each Laravel app.

<?php

namespace Username\Roles\Exceptions;

use Symfony\Component\HttpKernel\Exception\HttpException;

class InsufficientUsergroupException extends HttpException
{
    private $requiredRoles = [];

    public static function forRoles(array $$roles): self
    {
        $permStr = implode(', ', $roles);

        $error = 'Insufficient permissions to access this page.';
        $exception = new static(403, $error, null, []);
        $exception->requiredRoles = $roles;

        //return $exception;

        return view('role::InsufficientRoleAccess')->with('role', $permStr);
    }
    
    public function getRequiredRoles): array
    {
        return $this->requiredRoles;
    }
}

Any support you can provide would be most appreciated! Thank you for reading this. :)

2
  • how about you remove the return type from that method or correct it? you defined it to return self but are returning a View object ... and array $$roles ? Commented Aug 31, 2020 at 11:18
  • Wouldnt it be easier to publish laravel errors blade template and customize the statics as you see fit? Commented Aug 31, 2020 at 11:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.