Url Rewrite When Using Form

Hello,

The goal: rewrite urls like mycontroller/something?var1=first&var2=second to /seoname/first-second

The problem: Url rewrite works well when I’m using link, but doesn’t when I’m using form.

So it [color="#00FF00"]works[/color] great if I use:


CHtml::link('something', array('var1'=>'first', 'var2'=>'second'));

But when I use the following, it [color="#FF0000"]doesn’t[/color] rewrite the url:


<form action="something">

  <input type="hidden" name="var1" value="first">

  <select name="var2">

    <option value="second">second</option>

  </select>

  <input type="submit">

</form>

I have the following rule in config/main.php:


'seoname/<var1>-<var2>'=>'mycontroller/something'

Any ideas how to make it work with form, also?

The building of the URL is done by the HTML Form and than, I think that PHP have not control on it.

A possibly solution should be to use jQuery in that way:




<form action="something">

	<input type="hidden" id="var1" name="var1" value="first">

	<select id="var2" name="var2">

	<option value="second">second</option>

	</select>

	<input type="submit">

</form>


<script type="text/javascript">

	$(function() {

		$("form").submit( function (e) 

		{

			e.preventDefault();

			var url = "something/"+

				$("#var1").val()+

				"-"+

				$("#var2").val();

			window.location = url;

		});

	});

</script>



However, this solution does not use the URL Manager of Yii and that any change in the URL Manager have no effect on this view.

A possibly improvement to this idea should be to not calculate the variable "url" on client side but to get it through an ajax request to an action which has the task of replacing the classic URL with an URL produced by the URL manager.

This is not the best way but just an idea :).

This solution sounds reasonable. Thank you very much!

However I’m still wondering if anyone has an idea solving this issue without using javascript.