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?


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
    // Redirect the user

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);
| improve this answer | |

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"));

        parent::display($cachable, $urlparams);
| improve this answer | |

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.