Useful snippets in Joomla

Return a full page error, good for using when there is no way to get into some kind of requests combination from the normal page flow. (as for hacking the requests)

JError::raiseError( 404, 'Contact not found' );
return;

We can place this snippet at the start of our view.html.php to allow only registered users to have access to this content, and we can allow guest just changing the $accessLevel variable, you could easily load that variable from the component xml configuration.

$user        = &JFactory::getUser();
// $accessLevel for guests 0, for registered users 1
$accessLevel = 1;
if (($accessLevel > $user->get('aid', 0))) {

$uri        = JFactory::getURI();
$return        = $uri->toString();

$url  = 'index.php?option=com_user&view=login';
$url .= '&return='.base64_encode($return);
$mainframe->redirect($url, JText::_('You must login first') );

}

That snippet called from a view class returns a JParameter object with the parameters that belong to config.xml in the administrator side (The ones that are settable using the component preferences dialog) and (something I didn´t realize until) the parameters belonging to the actual view layout, usually default.xml inside com_mycomponent/view/tmpl/default.xml.

global $mainframe;
$params = &$mainframe->getParams('com_mycomponent');



One thought on “Useful snippets in Joomla”

  1. I’m extremely impressed along with your writing skills as smartly as with the format on your blog. Is this a paid topic or did you customize it your self? Either way stay up the nice high quality writing, it is rare to look a great weblog like this one nowadays..

Leave a Reply

Your email address will not be published. Required fields are marked *