samedi 22 septembre 2018

allow controller method access for multiple middlewares laravel

I am working on a project with multiple authentication that has admins, students and teachers as three levels of authentication. I have changed default authenticable user model to student and added two more authenticable models that have their own logins.

I have the CourseController as follows:

    use App\Course;
        use App\Invoice;
        use Illuminate\Http\Request;
        use Illuminate\Support\Facades\Auth;

    class CourseController extends Controller
    {

        public function __construct()
        {
            $this->middleware('auth', ['only' => ['index']]);
            $this->middleware('auth:teacher', ['only' => ['index']]);
            $this->middleware('auth:admin', ['only' => ['index', 'create', 'store', 'edit', 'update', 'delete', 'search', 'destroy']]);
        }

        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function index()
        {
            $data = Course::get();
            if (Auth::user()->role == 'admin') {
                return view('admin.course.index', compact('data'));
            } elseif (Auth::user()->role == 'student') {
                return view('student.course.index', compact('data'));
            } elseif (Auth::user()->role == 'teacher') {
                return view('teacher.course.index', compact('data'));
            }
        }
    }

config/auth.php as follows:(default guard is student)

'defaults' => [
        'guard'     => 'web',
        'passwords' => 'students',
    ],    

    'guards' => [
        'web' => [
            'driver'   => 'session',
            'provider' => 'students',
        ],

        'api' => [
            'driver'   => 'token',
            'provider' => 'students',
        ],

        'admin' => [
            'driver'   => 'session',
            'provider' => 'admins',
        ],


        'admin-api' => [
            'driver'   => 'token',
            'provider' => 'admins',
        ],

        'teacher' => [
            'driver'   => 'session',
            'provider' => 'teachers',
        ],


        'teacher-api' => [
            'driver'   => 'token',
            'provider' => 'teachers',
        ],
    ],

My problem:

I want CourseController@index to be accessible for all three guards and pass the $data to their respective view. How would I modify the CourseController so that I can achieve this?? Please help

If you have any other ideas you can suggest me that too...



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire