【问题标题】:createUrl is not creating URL in the path formatcreateUrl 未以路径格式创建 URL
【发布时间】:2014-02-07 13:06:50
【问题描述】:

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

这是 ma​​in.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
问题 :- 我怎样才能以路径格式获得它?

【问题讨论】:

    标签: url yii yii-url-manager


    【解决方案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>',
                    ),
    

    我刚刚从

    中删除了

    'airlineSearch/roundTripSearch/&lt;origin:\w+&gt;'=&gt;'airlineSearch/roundTripSearch/&lt;origin&gt;',

    【讨论】:

    • 不错的答案。问答 +1 :-)
    【解决方案2】:

    我总是建议删除默认的 Yii URL 规则并添加您自己的特定规则。也尝试使用useStrictParsing。这两种方法都有助于更密切地控制您的 URL,并将在适当时生成 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()
        {
    
        }
    
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-06
      • 1970-01-01
      • 1970-01-01
      • 2023-02-01
      • 2012-04-19
      • 1970-01-01
      • 2011-07-02
      相关资源
      最近更新 更多