I have custom component. For now everyone can access the page with this component. I want to redirect user to login page if he is not loged in. How to get login page url?
In my components view I write:
if($user->id != 0) {
..
}
else {
$link = JRoute::_(???????);
$this->setRedirect($link);
}
Another question - is it ok to put into view or should I put it somwhere else? I am using Joomla 2.5.
Try this ,
In that component view you have to restrict only for logged in users then,
You can use this code any where inside view.html.php,layouts etc.
This is ideally placed in the controller: but you could do much the same in the view.
first, encode your return link:
then, build the route to the login page:
You don't specify, so I'm presuming Joomla 2.5.x.
In you view class seems to be the standard place... e.g.
com_content
checks in it'sarticle/view.html.php
file, with the following:As you can see,
com_content
doesn't redirect the user, it simply raises a blocking warning message.com_users
can be called with aredirect
parameter that contain the URL you want the user sent to after they login. The parameter should bebase64
encoded and escaped withurlendcode
. You can read about the redirect after login mechanism on docs.joomla.org.To achieve a redirect to the login and then return to the original request after a successful login you can use something similar to this in your
else
block: