lundi 25 mars 2019

Laravel handle error that only thrown by controller

I am creating a logic where a controller can call another controller depend on Auth::user() role, but not all of controller shared same method, so i want if controller calling a method that does not exist it will throw a 404 not found. here is my controller

class LokalController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    Public $controller;
    public function __construct()
    {
        //$this->middleware('adminakses');
        $this->middleware(function ($request, $next) {
            $this->setController();
            return $next($request);
        });
    }
    public function setController()
    {
        $role = Auth::user()->role;
        switch ($role)
        {
            case 'admin':
                $this->controller = new \SIA\Http\Controllers\Admin\LokalController;
                break;
            case 'guru':
                $this->controller = new \SIA\Http\Controllers\Guru\LokalController;
                break;
            case 'siswa':
                $this->controller = new \SIA\Http\Controllers\Guru\LokalController;
                break;

        }
    }
    public function index()
    {
        return $this->controller->index();
    }

for example Admin\LokalController has method A(), but Guru\LokalController doesn't, and if user logged in as guru and trying to cal method A() it should returning not found exception or something user understandable message, but currently showing BadMethodCallException method A() does not exist



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2TYGrwL
via IFTTT

Aucun commentaire:

Enregistrer un commentaire