在 Joomla 管理员中使用 JRoute::_()
我正在处理一个自定义组件,并且正在编写一个在管理区域中运行的导入脚本。我有以下代码:
$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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于那些在 Google 上找到此内容并在使用 JRoute::_() 和 contentHelper::getArticleRoute() 时遇到困难的人。
For those who find this on Google and struggle with using JRoute::_() and contentHelper::getArticleRoute().
这是适用于 Joomla 3.6 的代码片段
Here's a snippet that will work for Joomla 3.6
在 Joomla 3.9 中,他们扩展了 JRoute 类(现在称为 Route 类)以包含解决此问题的 link() 静态方法。
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.
Route::link()
works just the same asRoute::_()
except that youcanmust 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.更好的解决方案是创建一个新的路由器实例,因此,代码将如下所示:
这样,您将始终获得该项目的正确 URL,无论它是 joomla 文章、K2 文章等。 。
A nicer solution would be to create a new router instance, so, the code will be something like this:
This way you will always obtain the right URL for the item, no matter if it is a joomla article, K2 article, etc...
我认为这将是一个更简单的解决方案:
这将为您提供与其他两个答案相同的结果,但编码较少。
希望这有帮助。
I think that this one would be an easier solution:
This will give you the same result as the other two previous answers but with less coding.
Hope this helps.