O
O
Orbb2017-10-01 20:58:11
PHP
Orbb, 2017-10-01 20:58:11

How to hang authorization on all routes?

Greetings!
Then my application grew, moved to Slim, and here's the question:
now I insert a controller into each route that checks access rights to this section + authorization.
for example:
/cabinet
/cabinet/1
/cabinet/2
/cabinet/2/2
/cabinet/2/3/info
And everything would be fine, but if there are 100 such pages, there will be a lot of extra code.
Is it possible to somehow determine once that the controller worked for each route. I so understand it is necessary to look in the direction of Route Group and Middleware?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
DmitrSha, 2017-10-01
@humiliation

yes, you need to use middleware, grouping routes as needed
Video course Authentication with Slim 3

M
MaximMRX, 2017-10-01
@GM_pAnda

Look, the cabinet controller must inherit the base controller, get the desired route in the base controller and check something like. Or rather, see the documentation of your framework) Or wait for the answer of someone who understood SLIM

if(Auth::check() == false && $route['controller'] != 'Login'){
      redirect('/admin/login/'); die;
}

A
Anton, 2017-10-02
@Eridani

I don’t know what kind of slim, etc., but, as it were, it would be logical to divide the application into two parts - UserController and AdminController, roughly speaking. Let the rest be inherited from them, and the parent one will just check for auth.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question