我正在使用Joomla 3.9 我已经开发了一个自定义组件。 在我的组件中,此路由器代码有效。 该代码适用于:
/ myform / first_speciality_alias
/ myform / second_speciality_alias
/ myform / first_speciality_alias
/ myform / second_speciality_alias
/ myform / first_speciality_alias / third_reference
/ myform / second_speciality_alias / fourth_reference
此路由器代码在做什么? 他将我的表单字段之一与URL同步。
问题是,我还需要特殊菜单项。 但是,一旦我创建了特色菜单项;
<?php
defined('_JEXEC') or die;
class MyComponentRouter extends JComponentRouterView
{
public function build(&$query)
{
$segments = array();
if (isset($query['idspeciality']))
{
$db = JFactory::getDbo();
$qry = $db->getQuery(true);
$qry->select('alias');
$qry->from('speciality');
$qry->where('id = ' . $db->quote($query['idspeciality']));
$db->setQuery($qry);
$alias = $db->loadResult();
$segments[] = $alias;
unset($query['idspeciality']);
}
if (isset($query['reference']))
{
$segments[] = $query['reference'];
unset($query['reference']);
}
unset($query['view']);
return $segments;
}
public function parse(&$segments)
{
$vars = array();
$db = JFactory::getDbo();
$qry = $db->getQuery(true);
$qry->select('id');
$qry->from('speciality');
$qry->where('alias = ' . $db->quote($segments[0]));
$db->setQuery($qry);
$id = $db->loadResult();
if(!empty($id))
{
$vars['idspeciality'] = $id;
$vars['view'] = 'mylistview';
}
if(!empty($segments[1]))
{
$vars['reference'] = $segments[1];
$vars['view'] = 'mydetailsview';
}
return $vars;
}
}
<?php
defined('_JEXEC') or die('Restricted access');
class MyComponentViewMyComponent extends JViewLegacy {
function display($tpl = null) {
...
$speciality_from_url = JRequest::getVar('idspeciality');
# If POST speciality sent I rewrite to the correct URL
if(isset($_POST['secteur'])){
$session->set('idspeciality', $_POST['secteur']);
$url = JRoute::_('index.php?option=com_mycomponent&view=mylistview'.'&idspeciality=' . $session->get('idspeciality'), false);
$app->redirect($url);
return false;
}
elseif($speciality_from_url != $session->get('idspeciality')){
$session->set('idspeciality', $speciality_from_url);
$url = JRoute::_('index.php?option=com_mycomponent&view=mylistview'.'&idspeciality=' . $session->get('idspeciality'), false);
$app->redirect($url);
return false;
}
...
}
}
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="List view">
<message>List view.</message>
</layout>
<fields
name="request"
addfieldpath="/administrator/components/com_mycomponent/models/fields"
>
<fieldset name="request">
<field
name="idspeciality"
type="customtype"
label="Speciality"
description="Sort list view"
>
<option value="">Pick one</option>
</field>
</fieldset>
</fields>
</metadata>