Php Yii:命名url参数
通过以下请求Php Yii:命名url参数,php,yii,Php,Yii,通过以下请求 http://loc-proj/site/test/p1/param1/p2/param2 或 http://loc-proj/site/test/p2/param2/p1/param1 actionTest返回 public function actionTest($p1, $p2) { var_dump($p1, $p2, $_GET); Yii::app()->end(); } 而且还可以 我想从url中隐藏控制器(http://loc-proj/te
http://loc-proj/site/test/p1/param1/p2/param2
或
http://loc-proj/site/test/p2/param2/p1/param1
actionTest
返回
public function actionTest($p1, $p2)
{
var_dump($p1, $p2, $_GET);
Yii::app()->end();
}
而且还可以
我想从url中隐藏控制器(http://loc-proj/test/p1/param1/p2/param2
)使用规则
'http://loc-proj/test//“=>”站点/测试“,
根据请求<代码>http://loc-proj/test/p1/param1/p2/param2站点返回404
上述规则仅对请求<代码>http://loc-proj/test/param1/param2
谢谢伙计,谢谢你,我现在得到了答案!我想将一个url(例如products/product?p=productname)重写为products/product/productname,它现在可以工作了!谢谢:)这是一个有趣的地方,问了一个问题,帮助了某人)是的,对不起,伙计,哈哈!谢谢你。卡在可以有1个或2个参数的页面上。也许你也有答案?哈哈
string 'param1' //$p1
string 'param2' //$p2
array (size=2) //$_GET
'p2' => string 'param2' (length=6)
'p1' => string 'param1' (length=6)
<?php
class TestRule extends CBaseUrlRule
{
public $label;
public $route;
/**
* Creates a URL based on this rule.
* @param CUrlManager $manager the manager
* @param string $route the route
* @param array $params list of parameters (name=>value) associated with the route
* @param string $ampersand the token separating name-value pairs in the URL.
* @return mixed the constructed URL. False if this rule does not apply.
*/
public function createUrl($manager, $route, $params, $ampersand)
{
// TODO: Implement createUrl() method.
}
/**
* Parses a URL based on this rule.
* @param CUrlManager $manager the URL manager
* @param CHttpRequest $request the request object
* @param string $pathInfo path info part of the URL (URL suffix is already removed based on {@link CUrlManager::urlSuffix})
* @param string $rawPathInfo path info that contains the potential URL suffix
* @return mixed the route that consists of the controller ID and action ID. False if this rule does not apply.
*/
public function parseUrl($manager, $request, $pathInfo, $rawPathInfo)
{
$separator = '/';
$chunks = explode($separator, $pathInfo);
if (isset($chunks[0]) === true && $chunks[0] === $this->label) {
unset($chunks[0]);
$chunks = array_values($chunks);
$keys = $values = array();
foreach ($chunks as $i => $chunk) {
if ($i % 2 === 0) {
$keys[] = $chunk;
} else {
$values[] = $chunk;
}
}
$countKeys = count($keys);
$countValues = count($values);
if ($countKeys !== $countValues) {
$values[$countKeys] = '';
}
$_GET = array_merge($_GET, array_combine($keys, $values));
return $this->route;
}
return false;
}
}