5

I'm developing new MVC custom Joomla component (com_helpdesk) and need to verify if user is logged in to site. If not then it should redirect to the login page with "return" parameter. Could someone help me with it?

10

You can use Joomla's user object for this.

$user = JFactory::getUser();        // Get the user object
$app  = JFactory::getApplication(); // Get the application

if ($user->id != 0)
{
    // you are logged in
}
else 
{
    // Redirect the user
    $app->redirect(JRoute::_('index.php?option=com_users&view=login'));
}

If you would like to display a message when they get redirected, you can use the following:

$msg = 'You must be logged in to view this content';
$app->redirect(JRoute::_('index.php?option=com_users&view=login'), $msg);
2

Althugh Lodder have answered my questions, Here's what I did which I feel more sophisticated way to deal with custom component. It adds return parameter to com_users component.

In Main Controller, (components/com_mycomponent/controller.php)

I override following method:

public function display($cachable = false, $urlparams = array()) {

        $user = JFactory::getUser();

        if ($user->get('guest') == 1) {
            $this->setRedirect(JRoute::_('index.php?option=com_users&view=login&return=' . base64_encode(JUri::current()), "You must be logged in to view this content"));
            return;
        }

        parent::display($cachable, $urlparams);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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