Howto implent the new Route in my component

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Locked
User avatar
nicholai
Joomla! Explorer
Joomla! Explorer
Posts: 293
Joined: Sun Jun 11, 2006 10:29 am
Location: Netherlands
Contact:

Howto implent the new Route in my component

Post by nicholai » Fri Mar 09, 2007 11:32 am

Hello

(yes i have read http://forum.joomla.org/index.php/topic ... .html  ;))

Ok lets start to say what I know:
- put a file named router.php in your folder
- put 2 functions in it: ComponentBuildRoute and ComponentParseRoute

Then my knowledge stops :)

Ok the name I use in my classes for my component is JMS (JMSBuildRoute & JMSParseRoute)

I use links like:
- http://127.0.0.1/index.php?option=com_j ... &Itemid=51
- http://127.0.0.1/index.php?option=com_j ... =component
- http://127.0.0.1/index.php?option=com_j ... &Itemid=51
No where in my links I use &task=. Tasks are all done trough forms.

This is a part of form with the hidden values.




Ok. Now how can I change this all to integrate with the new Itemid way?
Do I have to change the links? the forms? what code should be in the router.php

I hope I have provided enough info, if not please ask for more  ;)

Thanks in advance and I hope I its not that hard as I think it is  :)
Founder and Lead developer of JAM (http://joomladev.org)
JAM is the first J!1.5 only component using AJAX!

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: Howto implent the new Route in my component

Post by ianmac » Fri Mar 09, 2007 1:29 pm

Basically, ComponentBuildRoute needs to take the query that is passed to it and return an array with the items in the right order.

Then ComponentParseRoute will get an array back and it needs to set the request variables.

One thing you should know first is that this only works on the frontend (I think you know this, but just for clarification).

I don't think you need to worry about your post variables.  Post variables usually imply you are doing some sort of data manipulation.  The main point of JRouter is to make it easy for people to remember your URLs and make them easy for the search engine to distinguish.  You don't really want nor expect search engines to pick up i.e. the submit a message form or whatnot.  task is hidden from the query string, so you don't have to worry about it being unfriendly/unreadable.

Hope this helps and gets you started.  Again, I guess to put things another way, JRouter allows you to pass variables over the query string without specifying what the variable names are in the query string.  This is done by placing the variables on the query string in a predictable order.  In your example you might have URLs like:
index.php/jms/message/234
message is the view name, and 234 is the message number.  We know how to decode this: if the first parameter is message, then we know that the second one is the message number.

index.php/jms/messages/joeblow
messages is the view name, and joeblow is the user name.  We display the users messages.  We know how to decode this again because it is in a predictable order.

The jms is automatically added for you.

Does this help?
Ian

User avatar
nicholai
Joomla! Explorer
Joomla! Explorer
Posts: 293
Joined: Sun Jun 11, 2006 10:29 am
Location: Netherlands
Contact:

Re: Howto implent the new Route in my component

Post by nicholai » Fri Mar 09, 2007 5:30 pm

Great, I understand the working now. Thanks Ian.  :-* :laugh:

Edit: Now I just have to find out how to create the right funtions....
Last edited by nicholai on Fri Mar 09, 2007 5:48 pm, edited 1 time in total.
Founder and Lead developer of JAM (http://joomladev.org)
JAM is the first J!1.5 only component using AJAX!

User avatar
nicholai
Joomla! Explorer
Joomla! Explorer
Posts: 293
Joined: Sun Jun 11, 2006 10:29 am
Location: Netherlands
Contact:

Re: Howto implent the new Route in my component

Post by nicholai » Fri Mar 09, 2007 7:07 pm

nicholai wrote: Great, I understand the working now. Thanks Ian.  :-* :laugh:

Edit: Now I just have to find out how to create the right funtions....
Ok, I believe I have the functions done.

Code: Select all

function JMSBuildRoute(&$query)
{
	$segments = array();

	if(isset($query['view']))
	{
		$segments[] = $query['view'];
		unset($query['view']);
	};

	if(isset($query['id']))
	{
		$segments[] = $query['id'];
		unset($query['id']);
	};

	if(isset($query['toid']))
	{
		$segments[] = $query['toid'];
		unset($query['toid']);
	};

	return $segments;
}

function JMSParseRoute($segments)
{
	// Get the active menu item
	$menu =& JMenu::getInstance();
	$item =& $menu->getActive();

	// Count route segments
	$count = count($segments);

	// Handle View and Identifier
	switch($item->query['view'])
	{
		case 'new' :
		{
			if($count == 2) {
				JRequest::setVar( 'toid', $segments[1], 'get');
			}
			$view = 'new';
		} break;

		case 'read' :
		{
			JRequest::setVar( 'id', $segments[1], 'get');
			$view = 'read';
		} break;

		case 'inbox' :
		{
			$view = 'inbox';
		} break;

		case 'outbox' :
		{
			$view = 'outbox';
		} break;

		case 'archive' :
		{
			$view = 'archive';
		} break;

		case 'trash' :
		{
			$view = 'trash';
		} break;

		case 'settings' :
		{
			$view = 'settings';
		} break;
	}

	JRequest::setVar( 'view', $view, 'get');
}
I have also changed all urls to use the JRoute::_() function.

Anyway, if I submit a form, all routing things are messed up from then, AND the urls are back to the 'old' style again.
Also trying manually to get back to the 'new' style (link = index.php/jms/new) results into the default page (inbox).
???

I also have another question. Is it possible to change the key in the url?
Example:
Original : index.php&option=com_jms&view=read&id=523
New : index.php/jms/read/subject_of_the_message

And if that's possible, how ?

Thanks in advance and bless J 1.5  8)
Founder and Lead developer of JAM (http://joomladev.org)
JAM is the first J!1.5 only component using AJAX!

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: Howto implent the new Route in my component

Post by ianmac » Fri Mar 09, 2007 7:38 pm

I'm not really sure what is going on with your code...  maybe you can post the requests that aren't working or that mess things up (along with the intended get and post variables).

As for changing keys, absolutely - all you have to do is convert in the build and parse it in parse.  As long as subject is unique, you should be fine.

Ian

User avatar
nicholai
Joomla! Explorer
Joomla! Explorer
Posts: 293
Joined: Sun Jun 11, 2006 10:29 am
Location: Netherlands
Contact:

Re: Howto implent the new Route in my component

Post by nicholai » Fri Mar 09, 2007 8:59 pm

ianmac wrote: I'm not really sure what is going on with your code...  maybe you can post the requests that aren't working or that mess things up (along with the intended get and post variables).

As for changing keys, absolutely - all you have to do is convert in the build and parse it in parse.  As long as subject is unique, you should be fine.

Ian
No, of course the subjects can not always be the same. I ll look in the other comps as they use things like 45-example-here, where the id is 45
Founder and Lead developer of JAM (http://joomladev.org)
JAM is the first J!1.5 only component using AJAX!

User avatar
Jinx
Joomla! Champion
Joomla! Champion
Posts: 6508
Joined: Fri Aug 12, 2005 12:47 am
Contact:

Re: Howto implent the new Route in my component

Post by Jinx » Tue Mar 20, 2007 2:47 am

Nicholai,

Your parse route function is wrong. You are looking at the active menu item to determine the parent view this is done in the core components t create a hierarchical structure. In your case this is not needed. You need something like :

Code: Select all

function JMSParseRoute($segments)
{
	JRequest::setVar( 'view', $sgements[0], 'get');
}
Johan Janssens - Joomla Co-Founder, Lead Developer of Joomla 1.5

http://www.joomlatools.com - Joomla extensions that just work

User avatar
nicholai
Joomla! Explorer
Joomla! Explorer
Posts: 293
Joined: Sun Jun 11, 2006 10:29 am
Location: Netherlands
Contact:

Re: Howto implent the new Route in my component

Post by nicholai » Sun Mar 25, 2007 11:02 am

Jinx wrote: Nicholai,

Your parse route function is wrong. You are looking at the active menu item to determine the parent view this is done in the core components t create a hierarchical structure. In your case this is not needed. You need something like :

Code: Select all

function JMSParseRoute($segments)
{
	JRequest::setVar( 'view', $sgements[0], 'get');
}
Thanks Jinx it is working.

I have other problems now:
After a form action the index.php/jms/blabla structure is ruined; it changes into something like "index.php/component/jms?view=settings", so the itemid is also forgotten which results in modules set by itemid = 1;
???

PS. The component is inserted, so the problem can be experienced
You do not have the required permissions to view the files attached to this post.
Founder and Lead developer of JAM (http://joomladev.org)
JAM is the first J!1.5 only component using AJAX!

User avatar
Jinx
Joomla! Champion
Joomla! Champion
Posts: 6508
Joined: Fri Aug 12, 2005 12:47 am
Contact:

Re: Howto implent the new Route in my component

Post by Jinx » Sun Mar 25, 2007 4:02 pm

Don't have the time to look at your code, sorry ! Quick question, are you doing a JRoute for the form action ?
Johan Janssens - Joomla Co-Founder, Lead Developer of Joomla 1.5

http://www.joomlatools.com - Joomla extensions that just work

User avatar
nicholai
Joomla! Explorer
Joomla! Explorer
Posts: 293
Joined: Sun Jun 11, 2006 10:29 am
Location: Netherlands
Contact:

Re: Howto implent the new Route in my component

Post by nicholai » Sun Mar 25, 2007 4:19 pm

Jinx wrote: Don't have the time to look at your code, sorry ! Quick question, are you doing a JRoute for the form action ?

Code: Select all

<form action="index.php" method="post" name="adminForm" enctype="multipart/form-data">
[.....]
<input type="hidden" name="option" value="com_jms" />
<input type="hidden" name="task" value="savesettings" />
</form>
Founder and Lead developer of JAM (http://joomladev.org)
JAM is the first J!1.5 only component using AJAX!

User avatar
Jinx
Joomla! Champion
Joomla! Champion
Posts: 6508
Joined: Fri Aug 12, 2005 12:47 am
Contact:

Re: Howto implent the new Route in my component

Post by Jinx » Sun Mar 25, 2007 4:39 pm

Make that :

Code: Select all

<form action="<?php echo JRoute::_('index.php') ?>" method="post" name="adminForm" enctype="multipart/form-data">
Johan Janssens - Joomla Co-Founder, Lead Developer of Joomla 1.5

http://www.joomlatools.com - Joomla extensions that just work

User avatar
nicholai
Joomla! Explorer
Joomla! Explorer
Posts: 293
Joined: Sun Jun 11, 2006 10:29 am
Location: Netherlands
Contact:

Re: Howto implent the new Route in my component

Post by nicholai » Sun Mar 25, 2007 7:40 pm

Jinx wrote: Make that :

Code: Select all

<form action="<?php echo JRoute::_('index.php') ?>" method="post" name="adminForm" enctype="multipart/form-data">
Thanks Jinx  :)
Founder and Lead developer of JAM (http://joomladev.org)
JAM is the first J!1.5 only component using AJAX!

User avatar
epsi
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Wed May 20, 2009 7:32 pm
Contact:

Re: Howto implent the new Route in my component

Post by epsi » Sat May 30, 2009 12:24 pm

Hello joomla people,
I had similar problem (I know this is old thread).

I've mimic Khepri functionality in front end:
http://www.jlleblanc.com/option,com_fir ... 2947/#2943

But JToolbar doesn't work with SEF.
It works well without SEF.

Most used button [apply, save, cancel] is likely forwarded
to the same url as edit form instead of main form.
I don't know if it is submitform request problem or the url problem.

I already made router.php as below.
Maybe submitbutton task request (post ???) always replaced by router' task.

I know we don't need any data entry to be known in search engine.
And since mixing post and get is confusing,
I've decided to disable $vars['task'].

The url looks like something?task=edit&cid[]=2 ..
Everything seems to be okay,
and everything is redirected to main form.

But then apply button is still
redirected to main form instead of edit form.

I've check the mvc controller,
and trapped task after clicking apply button.

Code: Select all

	function __construct( $config = array() )
	{
		parent::__construct( $config );
		echo 'task = JRequest::getCmd('task');
		exit;
It shows nothing

Code: Select all

task = 
Am I missing something fool here?



Thank You

~epsi
---------------------

I'm using post from adminFrom.
I've check with alert(document.adminForm.task.value);
After clicking apply button, it say `alert`.

Code: Select all

<form action="#" method="post" name="adminForm" id="adminForm">
	...
	<input type="hidden" name="task" value="" />
</form>

Code: Select all

<?php
/**
* SEF target
* /component/sample/master/
* /component/sample/master/add
* /component/sample/master/edit?cid[]=3
* /component/sample/detail/3
* /component/sample/detail/3/add
* /component/sample/detail/3/edit?cid[]=6
**/

// no direct access
defined('_JEXEC') or die('Restricted access');

function PortsBuildRoute(&$query)
{	
	$segments = array();

	if(isset($query['controller']))	// segment[0]
	{	
		$segments[] = $query['controller'];
		unset( $query['controller'] );
	}	
	
	// master-id for detail, if any
	if(isset($query['mid']))		// segment[1]	
	{	
		$segments[] = $query['mid'];
		unset( $query['mid'] );
	}		
/* 
	if(isset($query['task']))		// segment[1]
	{	
		$segments[] = $query['task'];
		unset( $query['task'] );
	}
*/
	return $segments; 
}

function PortsParseRoute($segments)
{
	$count = count( $segments );	

	$vars = array();			
	$vars['controller'] = array_shift($segments);
	
	if ($vars['controller']=='details')	
		$vars['mid'] = (int) array_shift($segments);

//	$vars['task']	= array_shift($segments);			

	return $vars;
}

Sorry for the long story.

~epsi

hifive
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Thu Sep 23, 2010 7:50 am
Contact:

Re: Howto implent the new Route in my component

Post by hifive » Thu Sep 23, 2010 7:58 am

HELLO, all. how to remove the id number in the sef url? I upgraded joomla to 1.5 and got the problem.
Finding blog:Farlee

User avatar
mr_coffee
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Tue Oct 19, 2010 11:50 pm
Location: Indianapolis, IN

Re: Howto implent the new Route in my component

Post by mr_coffee » Wed Oct 20, 2010 12:24 am

SEF doesn't work due to the # in the url. On a side note - the javascript used to process the action isn't cross-browser. No, I don't know how to fix that (didn't try). Yes, I know this is an old thread but the issue still exists. I had this issue twice within two forms. For the first, the fix was was easy as the resulting URL was supposed to be an item we had a menu item already set for (SEF style). So for the first... a new "thing"... for which there was an "add" button in toolbar for... I modified the toolbar to have no spaces so that the id would be "legally" accessible from the DOM:

Code: Select all

$bar = new JToolBar( 'MyToolbar' );
Note, the id of the toolbar is now "MyToolbar". Buttons are given ids automatically... like "MyToolbar-add". Fortunately, we had a menu item set up - as I mentioned - for the "add" procedure... that resulting page. So instead of trying to figure out the JavaScript being used here, it made more sense to replace that procedure and just link straight to that page. So.. in JavaScript... assuming you have it attached to this particular view: view.html.php -

Code: Select all

JHTML::_('behavior.mootools');  // activate mootools first (assumes you have mootools upgrade enabled)
$js = "components/com_yourcomponent/yourjs.js";
$document->addScript(JURI::base() . $js);
Then in that JS, you can do this:

Code: Select all

if ($('MyToolbar-add')) {
	var toolbar = $('MyToolbar-add');
	var link = toolbar.getElement('a');
	link.setProperty('href','your-sef-enabled-menu-item.html');
	link.removeProperty('onclick');
}
This removes the built in "onclick" action, and replaces the href # with your desired destination.

Now... then there's the form itself. This only half-solved it for me since the form itself had YET ANOTHER toolbar on it. I didn't bother with trying to fix it with JavaScript like the above. I'll say it MIGHT work that way, but here's the thing... you can STYLE buttons to look like that. The jtoolbar has been nothing but a hindrance up to now, so I skipped it. Plus I wasn't sure about how the form "action" would be affected. The new toolbar button to fight with was "save". Left to it's own, it would work about 1/2 the time when clicked in Firefox, and none of the time in Internet Explorer. I fixed this by commenting out the "save" button in the toolbar, and just using a real button... for which the following must be changed in the form itself:

Code: Select all

<input type="hidden" name="task" value="save" />
<input type="hidden" name="controller" value="yourcontroller" />
<input type="submit" value="Save"  />
(index.php?controller=yourcontroller&view=thisform&task=save)

This did not adversely affect the "cancel" button also available in the toolbar. However, it would make sense to style the submit button to look like the toolbar button used to... and if so, remove the cancel and make a "real" button for that too, then put them both back in the form, but at the top where the toolbar used to be... and just not use the toolbar. It shouldn't be necessary to change the "action" of the form, but you should be JRoute::_('formurl.php')-ing.

The above is only a good example if you're having the issue on the front end (not administrator) of things. The admin side seems not to care about jtoolbar and behaves normally. In the case presented here, the cancel button on the front end worked without any modification, as did the delete button they also put in. Post deleting, you'd still have to click another item to see the form again, and so the location url wouldn't have a hash (#). Clicking cancel reloaded the page... removing the hash as well. I'm therefore about 99.999% certain the hash is what kills SEF. However, since the buttons behaved differently in IE and Firefox, I'd advise using REAL buttons in front end forms instead of using JToolbar for anything.

bobz
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Wed Sep 26, 2012 8:39 pm

Re: Howto implent the new Route in my component

Post by bobz » Thu Sep 27, 2012 3:48 am

Jinx wrote:Make that :

Code: Select all

<form action="<?php echo JRoute::_('index.php') ?>" method="post" name="adminForm" enctype="multipart/form-data">
<?php echo JRoute::_('index.php') ?>
What does this add ?

I am trying to set the menu of a component to a certain ItemId ( the current one ), but do not seem to know how.

Currently I have a module which passes data to a component, the component is displayed, but instead of using current Itemid it always uses the default menu Itemid,
How can I force it to use current ItemId ?

MarkRS
Joomla! Explorer
Joomla! Explorer
Posts: 310
Joined: Thu Oct 29, 2009 8:28 am
Location: UK

Re: Howto implent the new Route in my component

Post by MarkRS » Wed Dec 05, 2012 1:00 pm

@bobz, in case you're still watching this, that function is the one that calls the (ie "your") component router to convert the URL (if reqd.).
It's a community, the more we all contribute, the better it will be.


Locked

Return to “Joomla! 1.5 Coding”