2

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.

1 Answer 1

0

Use factory to get site application: Also looks like ContentHelperRoute is missing. You have import it. The strict standards can be solved by removing the ampersand before getRouter() call.

public static function getUrl()
{
    // Register required classes.
    JLoader::register('ContentHelperRoute', JPATH_SITE . '/components/com_content/helpers/route.php');

    // Get site router.
    $router = JFactory::getApplication('site')->getRouter('site');

    // Build URI object.
    $url = $router->build(ContentHelperRoute::getCategoryRoute('10'));

    return $url->toString();
}

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.

5
  • & is already there and it throws error "PHP Strict standards: Only variables should be assigned by reference" adding JLoader::register('ContentHelperRoute', JPATH_SITE . '/components/com_content/helpers/route.php'); does not help
    – Ekta Puri
    Sep 6, 2019 at 6:08
  • if i remove & then it throws error "failed to start application"
    – Ekta Puri
    Sep 6, 2019 at 6:10
  • Use JFactory::getApplication('site') to get the application.
    – Sharky
    Sep 6, 2019 at 6:27
  • can you please also suggest how can i use "rtrim( JURI::root(), '/') . JRoute::_('', true, 1);" in cli?
    – Ekta Puri
    Sep 6, 2019 at 6:39
  • JRoute::_() does not and will not work in CLI. JRoute::_link() but it currently has a bug which prevents it from working in CLI. JUri::root() issuecan only be solved by manually setting $live_site option in configuration.php.
    – Sharky
    Sep 10, 2019 at 5:23

Your Answer

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

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