XF 2.1 [SOLVED] Possible to redirect to another controller from one action method?

asprin

Active member
I've the following action method


PHP:
public function actionDoSomething(ParameterBag $params)
{
     $this->checkPermission();
     return $this->view('AFC:doSomething', 'mytemplate');
}

And the checkPermission function is
PHP:
public function checkPermission()
{
    // determine value of $crud
    .
    .
    .
    if(!$this->crud)
    {         
        return $this->redirect(
            $this->buildLink('myController/not-authorized')
        );
    }
}

And not-authorized action
PHP:
public function actionNotAuthorized()
{
    return $this->view('AFC:notAuthorized', 'not_authorzied');
}

I understand that this will not work as I'm not using return $this->checkPermission() in actionDoSomething() but then what could be the alternative.

The idea here is that I will call checkPermission() in all the action methods and let it redirect to the not authorized page if certain conditions fail. Is this doable?


EDIT: I've one thing in mind which is return boolean from $this->checkPermission() and then use the following:
PHP:
public function actionDoSomething(ParameterBag $params)
{
     if(!$this->checkPermission())
     {
         return $this->redirect(
            $this->buildLink('myController/not-authorized')
        );
     }

     return $this->view('AFC:doSomething', 'mytemplate');
}

But I see two issues here:
  1. Extra 3 lines each time I use this in each of the action methods that I intend to call this
  2. If in future I need to make a change to the buildLink value, I'll have to do it at every place instead of just one
 
Last edited:
Throw a reply exception instead:

PHP:
public function assertHasPermission()
{
    $hasPermission = mt_rand(0, 1) === 1;
    if ($hasPermission) {
        return;
    }

    throw $this->exception(
        $this->redirect($this->buildLink('not/good'))
    );
}
 
Back
Top Bottom