Php Yii-在URL路径中获取参数
我在config/main中有以下URL规则:Php Yii-在URL路径中获取参数,php,yii,Php,Yii,我在config/main中有以下URL规则: 'urlManager'=>array( //'appendParams' => true, // I've tried it without success. 'urlFormat'=>'path', 'rules' => array( // other rules... 'experience/search/<param:\w+>' => 'exp
'urlManager'=>array(
//'appendParams' => true, // I've tried it without success.
'urlFormat'=>'path',
'rules' => array(
// other rules...
'experience/search/<param:\w+>' => 'experience/searchExperiences',
$('#form-search').submit(function(event) {
var searchText = $('#search-text').val();
$('#form-search').attr("action", '/experience/search/' + searchText);
});
--
如何设置Yii来执行此操作?您需要在URL规则中添加GET变量的名称:
'experience/search/<searchText:(.*)>' => 'experience/searchExperiences',
“体验/搜索/”=>“体验/搜索体验”,
我还将
\w+
更改为(.*)
,因为第一个只匹配字母数字字符。后一个匹配所有字符。使用的规则:'experience/search/'=>'experience/searchExperiences',
提交时添加的JavaScript代码:
'urlManager'=>array(
//'appendParams' => true, // I've tried it without success.
'urlFormat'=>'path',
'rules' => array(
// other rules...
'experience/search/<param:\w+>' => 'experience/searchExperiences',
$('#form-search').submit(function(event) {
var searchText = $('#search-text').val();
$('#form-search').attr("action", '/experience/search/' + searchText);
});
将表单的方法更改为POST
:
<form id="form-search" action="/experience/search" method="post" class="search-form">
我仍然会等待任何更好的建议。你不能。只有使用以下选项,才能正确获取URL:
Yii::app()->createUrl(
'experience/search',
array(
'param' => 'whatever'
)
);
请注意,如果浏览器中没有任何JavaScript,这是不可能的,因为表单(没有JavaScript)无法提交到由表单数据构造的URL(带有GET方法的查询字符串除外)。@Lukas您能举个例子吗?我应该如何准备我的表单来使用JavaScript实现这一点?也许,
CActiveForm
有些东西。让我看看。请告诉我们您调用url的代码。@Lukas此解决方案类似于您的第一个注释吗?