简体   繁体   English

如何从后端访问Joomla中的前端菜单对象?

[英]How to access the frontend menu object in Joomla from the backend?

I'm writing a component, and I have this code for the frontend that works great to pull the menu object: 我正在编写一个组件,我有这个代码用于前端,它可以很好地拉取菜单对象:

$menuObjects= JFactory::getApplication()->getMenu();
$menuArrays = $menuObjects ->getMenu(); 

From there, I can generate an article URL given an article ID (which is what I'm trying to do). 从那里,我可以根据文章ID生成文章URL(这是我正在尝试做的)。 However, from the backend administrator side, this comes back empty. 但是,从后端管理员方面来看,这是空的。

Without querying the database, how can I access the frontend menu object from the backend? 在不查询数据库的情况下,如何从后端访问前端菜单对象?

#

Solution: 解:

Thanks to @borracciaBlu for getting me on the right track: 感谢@borracciaBlu让我走上正轨:

For Joomla 2.5 (use JApplication ): 对于Joomla 2.5(使用JApplication ):

$app = JApplicationCms::getInstance('site');
$menuObjects= $app->getMenu();
$menuArrays = $menuObjects ->getMenu();

For Joomla 3.x (use JApplicationCms ): 对于Joomla 3.x(使用JApplicationCms ):

 $app = JApplicationCms::getInstance('site'); $menuObjects= $app->getMenu(); $menuArrays = $menuObjects ->getMenu(); 

Ok I did some tests and those are the results. 好的,我做了一些测试,结果就是这些。

The reason why you have two different results is because you are using JFactory::getApplication() . 您有两个不同结果的原因是因为您正在使用JFactory::getApplication()

In fact in the frontend this method is returning a JApplicationSite object instead in the backend is returning a JApplicationAdministrator . 事实上,在前端,这个方法返回一个JApplicationSite对象,而不是在后端返回一个JApplicationAdministrator

That's the test : 这是测试:

$app = JFactory::getApplication();
var_dump($app);

If you try them in your component you'll see the results above. 如果您在组件中尝试它们,您将看到上面的结果。

Now, in theory JFactory was designed to accept parameters to override the std behaviour and instantiate the Object in the flavour you desire. 现在,理论上JFactory被设计为接受参数来覆盖std行为并在你想要的味道中实例化Object。

I'm saying that in " theory " method accepts several parameters. 我说在“ 理论 ”方法中接受几个参数。

From the documentation those are : 从文档中可以看到:

  • $id A client identifier or name. $id客户端标识符或名称。
  • $config An optional associative array of configuration settings. $config配置设置的可选关联数组。
  • $prefix Application prefix $prefix应用前缀

Unfortunately that's just a theory.. 不幸的是,这只是一个理论..
In reality the method is ignoring $config and $prefix in any case and $id if self::$application is already instantiated. 实际上,该方法在任何情况下都忽略$config$prefix ,如果self::$application已经实例化,则忽略$config $id

Guess what.. Unluckily for you at this point self::$application already exists. 猜猜是什么..此时不幸的是, self::$application已经存在。

Murphy would be proud of you.. :) 墨菲会为你感到骄傲.. :)

As first thing in administrator/index.php@39 we have: 作为管理员/index.php@39的第一件事,我们有:

$app = JFactory::getApplication('administrator');

So as you can see here the method is totally ignoring anything you pass to at this point. 所以你可以看到这个方法完全忽略了你传递给它的任何东西。 It can be used only to get an instance of JApplicationAdministrator. 它只能用于获取JApplicationAdministrator的实例。

//@see libraries/joomla/factory.php@101

/**
 * Get a application object.
 *
 * Returns the global {@link JApplicationCms} object, only creating it if it doesn't already exist.
 *
 * @param   mixed   $id      A client identifier or name.
 * @param   array   $config  An optional associative array of configuration settings.
 * @param   string  $prefix  Application prefix
 *
 * @return  JApplicationCms object
 *
 * @see     JApplication
 * @since   11.1
 * @throws  Exception
 */
public static function getApplication($id = null, array $config = array(), $prefix = 'J')
{
    if (!self::$application)
    {
        if (!$id)
        {
            throw new Exception('Application Instantiation Error', 500);
        }

        self::$application = JApplicationCms::getInstance($id);
    }

    return self::$application;
}

Btw the good news is that this method is not doing anything special except caching and wrapping JApplicationCms::getInstance($id); 顺便说一句,好消息是这个方法没有做任何特别的事情,除了缓存和包装JApplicationCms::getInstance($id); . So instead of your old code using JFactory::getApplication() : 因此,使用JFactory :: getApplication()代替旧代码:

$menuObjects= JFactory::getApplication()->getMenu();
$menuArrays = $menuObjects ->getMenu();

You can use it directly : 您可以直接使用它:

$app = JApplicationCms::getInstance('site');
$menuObjects= $app->getMenu();
$menuArrays = $menuObjects ->getMenu();

PS Yes as you may thinking the interface of JFactory::getApplication() it's a little bit buggy. PS是的,因为您可能认为JFactory::getApplication()的界面有点儿错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM