0

My controller is already working and complete. But I observe redundant/repetition of the code.

Specifically for the code below:

$this->authorize('applicant', $job);

Here's the entire code in my controller:

public function interviewees(Job $job)
{       
    //more codes
}

public function applicants(Job $job)
{       
    //more codes
}

public function apply(Job $job)
{   
    $this->authorize('applicant', $job);

    //more codes
}

public function cancel(Job $job)
{   
    $this->authorize('applicant', $job);

    //more codes
}

//and 5 more methods using same code of $this->authorize('applicant', $job);


My question is there a way in php or in laravel we can handle this situation, lessen redundant codes?
charles
  • 364
  • 6
  • 18
  • 1
    If this needs to fire for every action, then just move the authorize call to your constructor. (Or whatever equivalent start-up mechanism Laravel provides.) – Alex Howansky Jan 08 '20 at 18:13
  • @AlexHowansky I think that also, another problem is, there are some methods does not have the `$this->authorize('applicant', $job);`. Thanks for reply Sir – charles Jan 08 '20 at 18:14
  • Ah ok, I'm not familiar with Laravel and wasn't sure if `setUp()` and `ignore()` were actions. – Alex Howansky Jan 08 '20 at 18:15
  • You could split this into two controllers, putting the actions that require auth in one and the actions that don't require auth in the other. Then put the auth in the constructor. – Alex Howansky Jan 08 '20 at 18:16
  • @AlexHowansky I could just use the `__construct()` to call that code in one line. Another problem not all methods requires that code. Hmm.. Maybe I'll wait for the others, if they have an idea for this. else I will do your suggestion. Thanks for the suggestion though. – charles Jan 08 '20 at 18:19
  • I don't know much about Laravel, but I don't think what's described in the question is much of an issue. Redundant code often refers to multiple lines of code, or one that could be simplified / transformed into a simpler call to a common method. Here, you have one simple statement, so you don't really need to do anything (as long as it's not bad Laravel practice, which I can't comment on - guess https://laravel.com/docs/5.8/authorization would let you know about that if need be). – Jeto Jan 08 '20 at 18:19
  • @Jeto actually that is a policy authorization of Laravel Sir the code `$this->authorize('applicant', $job);` I just felt I keep on repeating the code, see it redundant. Thanks for reply Sir. – charles Jan 08 '20 at 18:22

1 Answers1

2

You can use a laravel-middleware instead of the policy, to apply for specific methods inside your __construct():

php artisan make:middleware OperatorMiddleware

Do some filter inside the handle methods.

class OperatorMiddleware
{
    public function handle($request, Closure $next, $guard = null)
    {
        //do some filter here

        return $next($request);
    }
}

Inside the \Http\Kernel.php, register the middleware inside the routeMiddleware.

protected $routeMiddleware = [
    'operator' => \App\Http\Middleware\OperatorMiddleware::class,
],

And you can call it for specific method(s):

public function __construct()
{
    $this->middleware('operator', ['only' => ['apply','cancel']]);
}
tempra
  • 2,455
  • 1
  • 12
  • 27