在 Joomla 管理员中使用 JRoute::_()

发布于 2024-11-25 12:36:45 字数 775 浏览 5 评论 0原文

我正在处理一个自定义组件,并且正在编写一个在管理区域中运行的导入脚本。我有以下代码:

$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);
$newUrl = JRoute::_($newUrl);

第一部分的工作返回与此类似:

index.php?option=com_content&view=article&id=45:joomla-sociable-and-sharethis-module&catid=18

第二部分显示如下:

/administrator/index.php?option=com_content&view=article&id=45:joomla-sociable-and-sharethis-module&catid=18

以上两个 url 均如您所期望的组件 com_content 呈现这些 url好像我想在管理区域内使用它们一样。

知道如何强制 JRoute 像在前端使用时一样工作吗?

注意:这是在我的组件的控制器中使用的,如果它有任何区别,并且我包括 require_once (JPATH_SITE . '/components/com_content/helpers/route.php');

I have a custom component I'm working on and I'm writing an import script which is running in the administration area. I have the following code:

$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);
$newUrl = JRoute::_($newUrl);

the first part works returning similiar to this:

index.php?option=com_content&view=article&id=45:joomla-sociable-and-sharethis-module&catid=18

the second part shows it like this:

/administrator/index.php?option=com_content&view=article&id=45:joomla-sociable-and-sharethis-module&catid=18

Both of the above urls are as you'd expect the component com_content to render these urls as if I wanted to use them within the administration area.

Any idea how to force JRoute to work as it would when used in the frontend?

NB: This is being used within a controller of my component, if it makes any difference and I'm including require_once (JPATH_SITE . '/components/com_content/helpers/route.php');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

听闻余生 2024-12-02 12:36:45

对于那些在 Google 上找到此内容并在使用 JRoute::_() 和 contentHelper::getArticleRoute() 时遇到困难的人。

$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);

// better will be check if SEF option is enable!
$router = new JRouterSite(array('mode'=>JROUTER_MODE_SEF));
$newUrl = $router->build($newUrl)->toString(array('path', 'query', 'fragment'));
// SEF URL !
$newUrl = str_replace('/administrator/', '', $newUrl);
//and now the tidying, as Joomlas JRoute makes a cockup of the urls.
$newUrl = str_replace('component/content/article/', '', $newUrl);

For those who find this on Google and struggle with using JRoute::_() and contentHelper::getArticleRoute().

$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);

// better will be check if SEF option is enable!
$router = new JRouterSite(array('mode'=>JROUTER_MODE_SEF));
$newUrl = $router->build($newUrl)->toString(array('path', 'query', 'fragment'));
// SEF URL !
$newUrl = str_replace('/administrator/', '', $newUrl);
//and now the tidying, as Joomlas JRoute makes a cockup of the urls.
$newUrl = str_replace('component/content/article/', '', $newUrl);
萤火眠眠 2024-12-02 12:36:45

这是适用于 Joomla 3.6 的代码片段

$routerOptions = [];
if (JFactory::getConfig()->get('sef')) {
    $routerOptions['mode'] = JROUTER_MODE_SEF;
}
$siteRouter = JRouter::getInstance('site', $routerOptions);
$link = $siteRouter->build($yourRoute)->toString();
$link = preg_replace('#^/administrator#', '', $link);

Here's a snippet that will work for Joomla 3.6

$routerOptions = [];
if (JFactory::getConfig()->get('sef')) {
    $routerOptions['mode'] = JROUTER_MODE_SEF;
}
$siteRouter = JRouter::getInstance('site', $routerOptions);
$link = $siteRouter->build($yourRoute)->toString();
$link = preg_replace('#^/administrator#', '', $link);
和影子一齐双人舞 2024-12-02 12:36:45

在 Joomla 3.9 中,他们扩展了 JRoute 类(现在称为 Route 类)以包含解决此问题的 link() 静态方法。

use Joomla\CMS\Router\Route;
$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);
$newUrl = Route::link("site", $newUrl);

Route::link() 的工作方式与 Route::_() 相同,只是您可以必须提供额外的第一个参数来指定您想要为其构建 URL 的客户端。请参阅https://api.joomla.org /cms-3/classes/Joomla.CMS.Router.Route.html#method_link

In Joomla 3.9 they extended the JRoute class (now called Route class) to include a link() static method which solves this problem.

use Joomla\CMS\Router\Route;
$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);
$newUrl = Route::link("site", $newUrl);

Route::link() works just the same as Route::_() except that you can must provide the additional first parameter to specify the client you want the URL built for. See https://api.joomla.org/cms-3/classes/Joomla.CMS.Router.Route.html#method_link.

很酷又爱笑 2024-12-02 12:36:45

更好的解决方案是创建一个新的路由器实例,因此,代码将如下所示:

$app    = JApplication::getInstance('site');
$router = &$app->getRouter();    

$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);

$newUrl = $router->build($newUrl);
$parsed_url = $newUrl->toString();
$parsed_url = str_replace('/administrator', '', $parsed_url);

这样,您将始终获得该项目的正确 URL,无论它是 joomla 文章、K2 文章等。 。

** 请注意,根据项目的类型(k2、joomla 等),应使用后续方法获取 $newUrl。

A nicer solution would be to create a new router instance, so, the code will be something like this:

$app    = JApplication::getInstance('site');
$router = &$app->getRouter();    

$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);

$newUrl = $router->build($newUrl);
$parsed_url = $newUrl->toString();
$parsed_url = str_replace('/administrator', '', $parsed_url);

This way you will always obtain the right URL for the item, no matter if it is a joomla article, K2 article, etc...

** Notice that depending on the type of the item ( k2, joomla, etc) , $newUrl should be obtained with the consequent method.

爱已欠费 2024-12-02 12:36:45

我认为这将是一个更简单的解决方案:

$newUrl = JRoute::_(ContentHelperRoute::getArticleRoute($import->id.':'.$import->alias, $import->catid));

这将为您提供与其他两个答案相同的结果,但编码较少。

希望这有帮助。

I think that this one would be an easier solution:

$newUrl = JRoute::_(ContentHelperRoute::getArticleRoute($import->id.':'.$import->alias, $import->catid));

This will give you the same result as the other two previous answers but with less coding.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文