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?