createUrl不以路径格式创建URL

时间:2014-02-07 13:06:50

标签: url yii yii-url-manager

我正在尝试在yii中创建路径格式的URL,但它总是以get格式创建它。我不明白什么是错的。

这是 main.php

 'urlManager'=>array(
                'urlFormat'=>'path',
                            'showScriptName'=>FALSE,
                'rules'=>array(
'airlineSearch/roundTripSearch/<origin:\w+>'=>'airlineSearch/roundTripSearch/<origin>',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
                ),
            ),

这是控制器

class AirlineSearchController extends Controller
{
public function actionRoundTripSearch($origin)
       {
           echo $origin;   
       }

       public function actionLets()
       {
          echo $this->createUrl('roundTripSearch',array('origin'=>'delhi'));
       }
}

但它始终会产生/services/airlineSearch/roundTripSearch?origin=delhi
问题: - 如何以路径格式获取?

2 个答案:

答案 0 :(得分:1)

我解决了这个问题。

'rules'=>array(
'airlineSearch/roundTripSearch/<origin:\w+>'=>'airlineSearch/roundTripSearch',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
                ),

我刚刚删除了&lt; origin&gt;从

'airlineSearch/roundTripSearch/<origin:\w+>'=>'airlineSearch/roundTripSearch/<origin>”,

答案 1 :(得分:0)

我总是建议删除默认的Yii URL规则并添加您自己的特定规则。另请尝试使用useStrictParsing。这两种方法都有助于更密切地控制您的网址,并会根据需要生成404。

这将是我的方法:

'urlManager'=>array(
    'showScriptName' => false,
    'urlFormat'=>'path',
    'useStrictParsing'=>true,
    'rules'=>array(

        'services/airline-search/<trip:round-trip|one-way>/<origin:\w+>' => 'airlineSearch/roundTripSearch',
    ),
),

然后在你的控制器中:

<?php

class AirlineSearchController extends Controller
{
    public function actionRoundTripSearch($origin)
    {
        print_r($_GET); // Array ( [trip] => round-trip [origin] => delhi )

        // Use the full route as first param 'airlineSearch/roundTripSearch'
        // This may have been the cause of your issue
        echo $this->createUrl('airlineSearch/roundTripSearch',array('trip' => 'round-trip', 'origin'=>'delhi'));
        // echoes  /services/airline-search/round-trip/delhi
    }

    public function actionLets()
    {

    }


?>