Joomla SEF url in CLI script

by Ekta Puri   Last Updated September 06, 2019 06:10 AM

I am new to Joomla, I am writing a script under folder cli. I am calling helper file of admin component where I fetch category and return url of category.

The function in helper is fine and works well when executed through admin menu.

The issue is with category URL not being generated when used in cli script.

$app    = JApplication::getInstance('site'); 
$router = &$app->getRouter();     
$newUrl = ContentHelperRoute::getCategoryRoute('10'); 
$newUrl = $router->build($newUrl); 
$url = $newUrl->toString(); 
$url = str_replace('/administrator', '', $url);

when used with CLI it throws error for strict standards for $router = &$app->getRouter();

i have also tried

JRoute::_(ContentHelperRoute::getCategoryRoute('10'));

It just throws error saying failed to start application.

I want to get SEF url of category so that I can also get it returned in my cli file.

My cli code:

class Abc extends JApplicationCli
{
    public function doExecute()
    {
        JFactory::getSession()->gc();

        JLoader::register('AbcHelper',JPATH_BASE.'/administrator/components/com_abc/helper.php');
        $url = AbcHelper::getUrl();
        echo $url;
    }
}

JApplicationCli::getInstance('Abc')->execute();

helper.php

public static function getUrl()
{
    $url='';
    $app    = JApplication::getInstance('site');
    $router = &$app->getRouter();    
    $newUrl = ContentHelperRoute::getCategoryRoute('10');
    $newUrl = $router->build($newUrl);
    $url = $newUrl->toString();
    $url = str_replace('/administrator', '', $url);
    return $url;
}

Any help is appreciated.



Answers 1


Looks like ContentHelperRoute is missing. You have import it:

JLoader::register('ContentHelperRoute', JPATH_SITE . '/components/com_content/helpers/route.php');

The strict standards can be solved by removing the ampersand before getRouter() call:

$router = $app->getRouter();

i have also tried

JRoute::_(ContentHelperRoute::getCategoryRoute('10'));

It just throws error saying failed to start application.

JRoute::_() will not work in CLI because it's based on current application and CLI doesn't have a router. JRoute::link() should work but, apparently, it has the same issue at the moment.

Sharky
Sharky
September 06, 2019 05:59 AM

Related Questions


Updated February 08, 2018 15:10 PM

Updated July 15, 2017 21:10 PM

Updated August 27, 2017 19:10 PM

Updated December 17, 2018 07:10 AM