naekobest
4 months ago
310
2
Laravel

Authentication, best way to block access for guests?

Posted 4 months ago by naekobest

Hey! So currently im blocking all access for guests in my controller

Controller Example

<?php

namespace App\Http\Controllers;

Use App\Exam;
Use App\Subject;
Use App\Topic;

use Illuminate\Http\Request;

class ExamController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $exams = Exam::with(['subjects'])->get();

        return view('exam.index', compact('exams'));
    }

    public function create()
    {
        return view('exam.create');
    }

    public function store()
    {
        $exam = Exam::create($this->validateData());

        return redirect('/exam');
    }

    public function show(Exam $exam)
    {
        $exam->load('subjects');

        return view('exam.show', compact('exam'));
    }

    public function edit()
    {
        
    }

    public function update(Exam $exam)
    {
        $exam->update($this->validateData());

        return redirect('exam');
    }

    public function destroy()
    {
        
    }

    protected function validateData()
    {
        return request()->validate([
            'name' => 'required',
            'lehrgang' => 'required',
            'abbr' => 'required'
        ]);
    }
}

but i cant figure out how to only apply the function to specific methods like create/store/edit/destory.

I could use php ->middleware('auth') behind each route in my web.php but this looks like not the right way to do it

Please sign in or create an account to participate in this conversation.