I am developing a custom component for Joomla! for the first time and I am stuck on the creation of a router.php. I alredy read the Joomla! Documentation but without clearly understand what to do.
Actually, the link to the single view of my component is:
index.php?option=com_layadvisory&view=layadvisory&id={$dataItem->id}
But I want the user to display OR:
www.mysite.com/Lay-Advisory/1
OR:
www.mysite.com/Lay-Advisory/person/1
Where 1 is the ID of the selected person. I know that I must create the build(&$query) and the parse($segments) function, but I can't understand how. Is there anyone that can direct me to the right path to follow? Thank you.
P.S. $dataItem->id is pulled from $this->data (via a foreach loop).
UPDATE:
I managed to get this result:
www.mysite.com/Lay-Advisory/id-Name-of-the-person
But when I go on the details/single page of the component, this notice appears:
Notice: Undefined index: name in components\com_layadvisory\router.php
My router.php is
* @copyright 2016 RCPSG * @license GNU General Public License version 2 or later; see LICENSE.txt */ // No direct access defined('_JEXEC') or die; JLoader::registerPrefix('Layadvisory', JPATH_SITE . '/components/com_layadvisory/'); /** * Class LayadvisoryRouter * * @since 3.3 */ class LayadvisoryRouter extends JComponentRouterBase { /** * Build method for URLs * This method is meant to transform the query parameters into a more human * readable form. It is only executed when SEF mode is switched on. * * @param array &$query An array of URL arguments * * @return array The URL arguments to use to assemble the subsequent URL. * * @since 3.3 */ public function build(&$query) { $segments = array(); if (isset($query['view'])) { //$segments[] = $query['view']; unset($query['view']); } if(isset($query['id'])) { $segments[] = $query['id']."-".$query['name']; unset($query['id']); unset($query['name']); } return $segments; } /** * Parse method for URLs * This method is meant to transform the human readable URL back into * query parameters. It is only executed when SEF mode is switched on. * * @param array &$segments The segments of the URL to parse. * * @return array The URL attributes to be used by the application. * * @since 3.3 */ public function parse(&$segments) { $vars = array(); $vars['view'] = "layadvisory"; $aux = explode(":",$segments[0]); $vars['id'] = $aux[0]; return $vars; } }
My JRoute link is:
$link = JRoute::_( "index.php?option=com_layadvisory&view=layadvisory&id={$dataItem->id}&name={$dataItem->name}" );