Here's what I want to do: I have a custom component which, in one of its views, requires the user to be logged in. If he isn't, for now, I only throw an Exception, and thats really ugly.
So I want to redirect the user to the login page. THAT is not the problem, I know how to do it.
BUT. There sure is a way of temporarily "overriding" the default redirect after login-page to the view the user wanted to see. In fact, the menu items do it, for instance. The problem is: I can't figure it out. Does anyone know how to override the default redirect of the login-page ONLY FOR ONE CALL?
You don't need to create a plugin in order to do this.
The com_user
component accepts a return
parameter to know where to send the user after login. This is used exactly in the case you described: The user tried to access a restricted area, he is redirected to the login page, and returns to the page he tried accessing before.
For that, you do what @Brian suggested, but with a slight change:
$app = JFactory::getApplication();
$message = "You must be logged in to view this content";
$url = JRoute::_('index.php?option=com_users&view=login&return=' . base64_encode('YOUR COMPONENT VIEW URL HERE')
$app->redirect($url, $message);