Form get method with path URL format

Hi,

I’ve been struggling with this for a couple of hours now, so I hope someone can steer me in the right direction.

I’ve configured Yii to use the path URL format and hide the index page. I’ve written a number of forms using the POST method and they work okay. I’d like to use the GET method for the search form I’m currently working on, so that I can link to the results of the search.

Here’s a stripped down version of the view:




<?php

print_r($_GET); // Printing contents of GET array while trying to figure out this problem


echo CHtml::beginForm('', 'get');


// name

$field = 'name';

echo CHtml::activeLabel($model, $field);

echo CHtml::activeTextField($model, $field, array('class'=>'form_input', 'maxlength'=>'80'));

echo CHtml::error($model, $field);


echo CHtml::endForm();



A stripped down version of the form model:




<?php

class FindMemberForm extends CFormModel{

	

	public $name;


	public function rules()

	{

		return array(

			array('name', 'required'),

		);

	}

}



And a stripped down version of the controller action:




<?php

class MemberController extends AdminController {


	public function actionFind()

	{

		Yii::import('application.models.forms.member.*');

		$form = new FindMemberForm;

		

		if (isset($_GET['FindMemberForm']))

		{

			$form->attributes = $_GET['FindMemberForm'];

		}

		

		$this->render('/member/find_member', array('model'=>$form));

	}

}



At this point, the code should simply cause the form fields to retain their value after submission, by taking the submitted values from the GET array. The form reloads correctly, but the form data is inaccessible. For some reason, the GET data gets passed in a standard query string such as:


http://192.168.0.3/admin/member/find?FindMemberForm[name]=testvalue

This does not get loaded into the GET array, so the result of the print_r statement in the view is:


Array ( [/admin/member/find] => )

How can I retrieve the form data, given that Yii is expecting the GET parameters to be passed into the URL in the form /controller/action/param_name/param_value ? Is there some way of getting Yii to automatically load data passed in the standard format into the GET array when you are using the path URL format?

Thanks,

Keith

First of, instead of print_r() try CVarDumper::dump() method to print arrays or objects. It will be well formatted, n helpful.

Secondly if you have enabled path URL format then i guess following url


http://192.168.0.3/admin/member/find?FindMemberForm[name]=testvalue

will look like


http://192.168.0.3/admin/member/find/FindMemberForm[name]/testvalue

something similar… try it.

I switched the print_r() statement as you suggested, but the output in this case is exactly the same as for print_r().

I’m not sure I explained my problem adequately above. The form automatically posts an URL in the following format when submitted:


http://192.168.0.3/admin/member/find?FindMemberForm[name]=testvalue

In this format, the value of FindMemberForm[name] does not appear in the GET array.

However, if I manually type the following URL using Yii’s path format:


http://192.168.0.3/admin/member/find/FindMemberForm[name]/testvalue

the form field is repopulated correctly and the value is available in the GET array.

Given that the form sends the data in the first format, I need to find a way to allow Yii access to the data in that format. There may be some helper function to parse that part of the URL into an array, but I’m struggling to find one in the documentation.

Any suggestions would be much appreciated.

Ohh… CVarDumper::dump() would be helpful in CActiveRecords, i.e. for objects. So, keep habit of using it all the times.

And about that, I think you need to check your urlManager settings in main config

What are your settings in main config? for urlManager component other than


array(

    ......

    'components'=>array(

        ......

        'urlManager'=>array(

            'urlFormat'=>'path',



This is the current config for the urlManager:




		'urlManager'=>array(

			'urlFormat'=>'path',

			'showScriptName'=>false,

		),



Should Yii already be handling my GET data correctly or am I trying to do something non-standard?

Thanks for your help by the way.

I think this should work. I could not reproduce the problem with the code you supplied.

The steps I did:

[list=1]

[*]Create new webapp via the command shell

[*]Create Admin module via the command shell

[*]Create the files from your first post

[*]Edit config in main.php with the urlManager options from post #5 (and inserting the admin module)

[*]Copy standard .htaccess to webroot

[*]Run http:// localhost/admin/member/find

[/list]

This gives me a page with one form field. Entering ‘test’ and hitting Enter gives me:


array ( [FindMemberForm] => array ( [name] => 'test' ) )

and the following url:

http:// localhost/test/admin/member/find?FindMemberForm[name]=test

So I think that your problem is some where in the code that you left out. To me it looks like your GET-array is corrupted. Are you doing any sanitizing or filtering of post/get some where else in the app?

Well, thanks to your help in demonstrating how this should work, I’ve managed to figure out what was going wrong. Instead of the .htaccess file suggested in the Yii guide, I’d taken the one from CodeIgniter because it performed checks on the status of the mod_rewrite module and allowed direct access to existing files, such as css and scripts. I had a closer look at this file to see if this was causing the problem, and sure enough:


RewriteRule ^(.*)$ index.php?/$1 [L]

My interpretation of the above is that the query is being placed after a question mark character, so the get parameters were being hidden behind a second question mark when sent to the PHP interpreter, and being rejected.

I have since switched to the following:


RewriteRule ^.*$ index.php [NC,L]

from the Zend framework and the requests are now being routed correctly.

Thanks for your help on this guys. Is there any way to mark this as solved in case anyone else stumbles upon the same issue in future?

Thanks,

Keith

In your Model, Controller and View examples what should be the names of the files and the location?

Tks