Yii:命名为url params

时间:2014-11-20 15:28:27

标签: php yii

public function actionTest($p1, $p2)
{
    var_dump($p1, $p2, $_GET);
    Yii::app()->end();
}

遵循请求
http://loc-proj/site/test/p1/param1/p2/param2

http://loc-proj/site/test/p2/param2/p1/param1

actionTest返回

string 'param1' //$p1
string 'param2' //$p2
array (size=2)  //$_GET
  'p2' => string 'param2' (length=6)
  'p1' => string 'param1' (length=6)

,没关系

我想从网址(http://loc-proj/test/p1/param1/p2/param2)中隐藏控制器 使用规则'http://loc-proj/test/<p1>/<p2>' => 'site/test',
根据请求http://loc-proj/test/p1/param1/p2/param2网站返回404
上述规则仅对请求http://loc-proj/test/param1/param2

有效

1 个答案:

答案 0 :(得分:0)

<?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;
    }
}