Özel bir bileşendeki belirli bir görünüm için bir menü öğem var. Bu menü öğesine bağlı başka standart bir Template Style
seçtim. Görünüme menü aracılığıyla erişmek, URL'ye Itemid
eklediğinden iyi çalışır.
Şimdi JRoute kullanarak başka bir görünümle bağlantı kurmak istiyorum, ancak JRoute istenen URL'yi oluşturmuyor.
echo JRoute::_('index.php?option=com_example&view=reporting');
/index.php?option=com_example&view=reporting
JRoute
, URL'ye ÖğeKimliği eklemeyecek ve menü öğesinin seçili şablon stilinin çalışmamasına neden olacaktır.
Itemid'i "hesaplamanın" (link
sütununda jos_menu
tablosunda sorgu yapmak dışında) ve JRoute'a eklensin mi?
İşte kullandığım bir teknik (nerede bulduğumu hatırlayamıyorum).
$app = JFactory::getApplication();
$menu = $app->getMenu();
$menuItem = $menu->getItems( 'link', 'index.php?option=com_example&view=reporting', true );
echo JRoute::_('index.php?Itemid='.$menuItem->id);
Bu benim için harikalar yarattı.
jRoute çıktısı ne beslediğinize bağlı olacaktır.
JRoute::_("index.php?option=com_content&view=article&id=10");
başka bir şey döndürebilir
JRoute::_("index.php?option=com_content&view=article&id=10&catid=5");
Aslında catid = 5 için bir menü öğesi kategorisi blog listeniz varsa, ikincisi size menu-url verebilir (Bu tam url'yi test etmedim). Farklı sonuçlar almak için farklı get parametreleriyle deneyin (@Fedik'in söylediği gibi bazen çok yanlış)
Buradaki anahtar, bileşenler için yönlendirici.php dosyanızı (ön uçtaki bileşeninizin kök klasöründe bulunması gerekir) uygun menü öğesini arayacak ve seçecek mantıkla ayarlamaktır. Bunun otomatik olarak gerçekleştiğini görmek isterdim, ancak bildiğim kadarıyla durum böyle değil.
Muhtemelen bu kod bloğunu, içeriğe en uygun menü öğesini otomatik olarak bulmak için kullanılabilecek bir tür yardımcı işlevde çalışmak en iyisi olacaktır.
İşte benim en uygun menü öğesi almak için benim özel bileşenleri birkaç kullandığınız kodu:
// I use this first empty array to avoid having unset properties in my query
$base_array = array('Itemid'=>'', 'option'=>'', 'view'=>'', 'layout'=>'', 'id'=>'');
$app =& JFactory::getApplication();
$menu = $app->getMenu();
$active = $menu->getActive();
// hack to protect the actual current item as well as the search module or other places that use JRoute::_('index.php');
if (count($query)==2 && isset($query['option']) && isset($query['Itemid']) && $query['option'] && $query['Itemid']) {
return $segments;
}
// start with no match found
$match = false;
$match_level = 0;
$query += $base_array;
// we want to find a menu item for this if possible. If the active menu item is the current menu item then we should see if there is a better match.
if (empty($query['Itemid']) || ($query['Itemid'] == $active->id && empty($query['task']))) {
// load all menu items
$items = $menu->getMenu();
// use the current item over others if it ties for the best match
if ($active->query['option'] == $query['option']) {
$match_level = 1;
$match = $active;
if ($active->query['view'] == $query['view']) {
$match_level = 2;
if ($active->query['layout'] == $query['layout'] || ($query['layout']=='default' && !$active->query['layout'])) {
$match_level = 3;
if ($active->query['id'] == $query['id']) {
$match_level = 4;
}
}
}
}
// loop through each menu item in order
foreach ($items as $item) {
$item->query += $base_array;
// base check is that it is for this component
// then cycle through each possibility finding it's match level
if ($item->query['option'] == $query['option']) {
$item_match = 1;
if ($item->query['view'] == $query['view']) {
$item_match = 2;
if (!$query['layout'] && $item->query['layout']) {
$query['layout'] = 'default';
}
if ($item->query['layout'] == $query['layout'] || ($query['layout']=='default' && !$item->query['layout'])) {
$item_match = 3;
if ($item->query['id'] == $query['id']) {
$item_match = 4;
}
}
}
}
// if this item is a better match than our current match, set it as the best match
if ($item_match > $match_level) {
$match = $item;
$match_level = $item_match;
}
}
// if there is a match update Itemid to match that menu item
if ($match) {
$query['Itemid'] = $match->id;
$menuItem = $menu->getItem($match->id);
} else {
$menuItem = $menu->getActive();
}
}
Bütün bunlar bir tür karışıklık (ve eğer kimse varsa iyileştirmeleri isterim!), Ama işi halleder. Geçerli menü öğesi en iyi eşleşmediyse, her zaman buna sadık kalacaktır.
Aksi takdirde Bileşen adı -> görünüm adı -> düzen adı -> kimlik değerine göre en iyi eşleşmeyi bulmalıdır. Ne kadar sağda eşleşirse, maçı o kadar iyi görüyorum!
Afaik JRoute, hiçbiri yoksa aktif Itemid
(ve ayrıca option
etkinliğini) alacaktır. Bu işe yaramazsa, kodunuza yapılan çağrı Itemid olmadan başlar demektir. Öyleyse, en kolay şey, Itemid'i ilk çağrıya eklemek olacaktır.
Menü öğesine bakmanız gerekiyorsa, doğrudan bir sorgu yapmazdım ama bunun yerine JMenu kullanın.