我有自定义组件。现在,每个人都可以使用此组件访问该页面。如果他没有进入,我想将用户重定向到登录页面。如何获取登录页面网址?
在我的组件视图中,我写道:
if($user->id != 0) {
..
}
else {
$link = JRoute::_(???????);
$this->setRedirect($link);
}
另一个问题 - 是否可以放入视野或者我应该把它放在其他地方? 我正在使用Joomla 2.5。
答案 0 :(得分:2)
试试这个,
在该组件视图中,您必须仅限制登录用户,
$user = JFactory::getUser();
if($user->id != 0){
// show your view
}
else{
$mainframe = JFactory::getApplication();
$mainframe->redirect('index.php?option=com_users&view=login');
}
您可以在view.html.php,layouts等中的任何位置使用此代码。
答案 1 :(得分:0)
你没有指定,所以我假设Joomla 2.5.x。
在您的视图中,类似乎是标准的地方......例如com_content
检入article/view.html.php
文件,其中包含以下内容:
// Check the view access to the article (the model has already computed the values).
if ($item->params->get('access-view') != true && (($item->params->get('show_noauth') != true && $user->get('guest') ))) {
JError::raiseWarning(403, JText::_('JERROR_ALERTNOAUTHOR'));
return;
}
如您所见,com_content
不会重定向用户,只会引发阻止警告消息。
com_users
参数调用 redirect
,该参数包含用户登录后要发送到的URL。该参数应base64
编码并使用urlendcode
进行转义。您可以在redirect after login上了解docs.joomla.org机制。
为了实现重定向到登录,然后在成功登录后返回到原始请求,您可以在else
块中使用与此类似的内容:
// Redirect to login
$uri = JFactory::getURI();
$return = $uri->toString();
$url = 'index.php?option=com_users&view=login&return=' . urlencode(base64_encode($return));
$jAp->redirect($url, JText::_('Message_about_this_view_requiring_login'));
答案 2 :(得分:0)
理想情况下,它位于控制器中:但您可以在视图中执行相同的操作。
首先,编码您的返回链接:
$link = base64_encode(JRoute::_("index.php?option=com_yourcomponent&view=someview&tmpl=somelayout", false));
然后,构建到登录页面的路线:
$rlink = JRoute::_("index.php?option=com_users&view=login&return=$link", false);
$msg = JText::_("COM_YOURCOMPONENT_LOGIN_TO_ACCESS");
$app = JFactory::getApplication();
$app->redirect($rlink, $msg);