Joomla: redirect to login page if user is not logged in and back to custom component

Here's what I want to do: I have a custom component that in one of its views requires the user to be logged in. If it doesn't, for now I just throw an exception and it is really ugly.

So, I want to redirect the user to the login page. This is not a problem, I know how to do it.

BUT. Of course, there is a way to temporarily "override" the default redirect after the login page to the view the user wanted to see. In fact, the menu items do this, for example. The problem is I can't figure it out. Does anyone know how to override the default redirect for the login page ONLY FOR ONE CALL?

+3


source to share


3 answers


You don't need to create a plugin for this.

The component com_user

takes a parameter return

to know where to send the user after login. This is exactly used if you described: a user tried to access a restricted area, he is redirected to the login page and returned to the page he tried to access earlier.



To do this, 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);

      

+8


source


There are 3 ways to accomplish this.

  • If you have a menu item for your custom component view, there is an Access option on the menu page . Just select the registered option and it. Now, when the user has tried to access this page, it will ask the user to login to see this page. enter image description here

  • You can create plugin

    or call this plugin from where this page is loaded.

  • Or you can directly add this code to your views, the default.



<?php
$user = JFactory::getUser();
if($user->id=='' || $user->id==NULL){
// Your message here.
}else{

// Your code here
}
?>

      

Hope this gives you an idea.

+4


source


You can create a plugin, but there is a more elegant way to do it using inline or inline existing code.

Instead of throwing an exception, use this line:

JFactory::getApplication()->redirect(JRoute::_('index.php?option=com_users&view=login', JText::_("You must be logged in to view this content"));

      

Another option is to create a menu link for the placement. This is a menu group that is not tied to a module and is displayed on the website. It is used for a variety of reasons, but in this case you will create a link to the menu and assign it a restricted level.

Now, if you want the user to be able to access through the displayed menu, you simply convert the public link to the menu alias type and point to the menu link of the placement with access level restrictions.

If you just want to use inline anchor links, you can use the placement menu alias as the href attribute to achieve the same result.

Automatically redirecting the user to the login page.

I would use one of these two versus a custom plugin.

+1


source







All Articles