1

I am trying to redirect to a page after I unset a session. I read this article but I am pretty new to Drupal module building.

Here this amazing guy says to do the comming thing:

If you know that the destination is an external URL then you are encouraged to use TrustedRedirectResponse instead:

return new RedirectResponse(\Drupal::url('<front>', [], ['absolute' => TRUE]));
  return new TrustedRedirectResponse('http://www.bbc.com/');

I don't understand how I should use this code and where to put it...

Any help would be great This is my controller file content.

<?php

namespace Drupal\achva_sso\Controller;

use Drupal\Core\Controller\ControllerBase;


class SsoController extends ControllerBase{
    public function ssoLogout(){
        unset($_SESSION['name']);

        if(isset($_SESSION['name'])){
            $status = $_SESSION['name'] .' is logged in';
        }else{
            $status = 'is logged out';
        }
        return[
            '#type' => 'markup',
            '#markup' => '<p>' . $status . '</p>' ,
        ];


    }
}

closed as too broad by Kevin, 4k4, Pierre.Vriens, leymannx, kiamlaluno Nov 24 '18 at 6:32

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

2

Well if you want to redirect to an external page this can be your code:

<?php

namespace Drupal\achva_sso\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Routing\TrustedRedirectResponse;

class SsoController extends ControllerBase{
    public function ssoLogout(){
        unset($_SESSION['name']);

        // Redirect.
        return new TrustedRedirectResponse('http://www.bbc.com/');

    }
}

In this case, the redirection will happen when your ssoLogout() method will be called.

  • 1
    As the documentation states, you need to use TrustedRedirectResponse instead to redirect to external URLs. – Kevin Nov 14 '18 at 17:28
  • 1
    But don't send, return the response as stated in the documentation. – 4k4 Nov 14 '18 at 17:33
  • 1
    @DavSev I come from update and test the answer and it works – Adrian Cid Almaguer Nov 14 '18 at 18:18
  • 1
    Thanks @Kevin I thought it was to an internal URL. – Adrian Cid Almaguer Nov 14 '18 at 18:19
  • 1
    @4k4 can you share the doc link? I used before the send and it works fine, jut to be updated I want to read more about this, thanks. – Adrian Cid Almaguer Nov 14 '18 at 18:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.