JRoute doesn't work on JApplicationWeb

60 views
Skip to first unread message

Federico Liva

unread,
Feb 12, 2016, 4:51:17 AM2/12/16
to Joomla! General Development
In /plugins/system/addrealhits/app/app.php there is a standalone Joomla application, that has to build the SEF URLs for each article written into the site.

Here, I'm querying the database to extract the id and the alias of each article, joined to the category id and the category alias. Fields required to build the right SEF URL.
Then, I'm using JRoute to build the SEF URL for each article, but it results isn't what I'm expecting is.

define('_JEXEC', 1);
define('JPATH_BASE', realpath(__DIR__ . '/../../../../')); // Find the root of the real Joomla site

// Import needed stuff to run the application
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_LIBRARIES . '/import.legacy.php';
require_once JPATH_LIBRARIES . '/cms.php';

/**
* Class BuildArticlesSefUrl
*/
class BuildArticlesSefUrl extends JApplicationWeb
{
/**
* @var JDatabaseDriver
*/
protected $db;

/**
*
*/
public function execute()
{
$this->db = JFactory::getDbo(); // Get the database
$articles = $this->getArticles(); // Get the articles
$app = JFactory::getApplication('site'); // Call the app to get JRoute works

JLoader::import('components.com_content.helpers.route', JPATH_BASE);

foreach ($articles as $article)
{
$article->slug = $article->id . ':' . $article->alias;
$article->catslug = $article->catid . ':' . $article->catalias;

// Build the url
$url = ContentHelperRoute::getArticleRoute($article->slug, $article->catid, $article->language);
echo "URL: $url </br>"; // Print the original URL (debug)

$url = JRoute::_($url);
echo "SEF URL: $url </br>"; // Print the SEF URL (debug)

exit; // Exit after the first article (debug)
};
}

/**
* @return mixed
*/
protected function getArticles()
{
$query = $this->db
->getQuery(true)
->select('a.id, a.alias, a.catid, a.language, b.alias catalias')
->from('#__content a')
->innerJoin('#__categories b ON a.catid = b.id')
->order('a.id DESC');

$result = $this->db
->setQuery($query)
->loadObjectList();

return $result;
}
}

// Run the application
JApplicationWeb::getInstance('BuildArticlesSefUrl')->execute();

When I run the app, I get URLs like this:

URL: index.php?option=com_content&view=article&id=4328:architect-for-you-restructure-your-home&catid=55&Itemid=100 
SEF URL: /plugins/system/addrealhits/cli/architecture/criteria-design/4328-architect-for-you-restructure-your-home 

The expected URL should be instead:

/architecture/criteria-design/4328-architect-for-you-restructure-your-home 

Without the path of te folder in which is placed my app.php file.

What I'm wrong?

Korzo

unread,
Feb 16, 2016, 9:45:48 AM2/16/16
to Joomla! General Development
Reply all
Reply to author
Forward
0 new messages