3

I needed to get JRoute working in backend so that from admin I can create an event with my event component and have an email sent that contains the SEF url. After researching, I found some tips that involve setting the app instance to site, call JRoute, then set app back to admin. However, while I am getting the SEF url, something isn't quite right during the redirect after the event creation.

Here is how I do this, which is basically a copy from an older post I found:

$live_site = substr(JURI::root(), 0, -1);
    if(JPATH_BASE == JPATH_ADMINISTRATOR) {
        // In the back end we need to set the application to the site app instead in order for JRoute to work
        JFactory::$application = JApplication::getInstance('site');
    }

    // Create the URL
    $url = $live_site . JRoute::_('index.php?option=com_icagenda&view=list&layout=event&id=' . $id . '&Itemid=' . $Itemid);

    // Check if we are in the backend again
    if(JPATH_BASE == JPATH_ADMINISTRATOR) {
         // In backend we need to remove administrator from URL as it is added even though we've set the application to the site app
        $eventLink= str_replace($live_site .'/administrator', $live_site, $url);
        // Set the appilcation back to the administartor app
        JFactory::$application = JApplication::getInstance('administrator');
    }               

The above seems to work, but the problem is during the redirect, after the event has been created and the email sent. Normally, the event view page in admin has this type of url:

administrator/index.php?option=com_icagenda&layout=edit&id=23

But because of the JApplication::getInstance business, the redirect url is messed up and I get a 500 error. The messed up url has this form

administrator/index.php/component/icagenda/?view=event&layout=edit&id=23

I think maybe the solution I pasted was from Joomla 1.5 and so maybe I need to do something differently in 3?

Any ideas?

Thanks!

2 Answers 2

3

JRoute is not reliable on the backend. Your approach may work on a simple install, but if you have a third-party SEF plugin, or even some slightly unorhodox routing in third-party components, this will fail miserably.

So I won't answer how to get rid of the 500 error. But propose an alternative solution.

In a popular SEF extension, ACEsef if I remember correctly, they used the simplest and most effective approach: make an ajax call to a frontend component (or plugin, your choice) and let it run JRoute from the real frontend.

0
3

Here is how I ended up getting it to work:

$live_site = substr(JURI::root(), 0, -1);
$app    = JApplication::getInstance('site');
$router = &$app->getRouter();
$url = $router->build($live_site.'/index.php?option=com_icagenda&view=list&layout=event&id=' . $id . '&Itemid=' . $Itemid);
$url= $url->toString();
$eventLink= str_replace($live_site .'/administrator', $live_site, $url);

Not sure though what I should do with that $app afterwards...

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.