Get current URL/URI without some of $_GET variables

后端 未结 15 1915
被撕碎了的回忆
被撕碎了的回忆 2021-01-30 09:10

How, in Yii, to get the current page\'s URL. For example:

http://www.yoursite.com/your_yii_application/?lg=pl&id=15

but excluding the

相关标签:
15条回答
  • 2021-01-30 09:11

    Try to use this variant:

    <?php echo Yii::app()->createAbsoluteUrl('your_yii_application/?lg=pl', array('id'=>$model->id));?>
    

    It is the easiest way, I guess.

    0 讨论(0)
  • 2021-01-30 09:12

    Yii 1

    Most of the other answers are wrong. The poster is asking for the url WITHOUT (some) $_GET-parameters.

    Here is a complete breakdown (creating url for the currently active controller, modules or not):

    // without $_GET-parameters
    Yii::app()->controller->createUrl(Yii::app()->controller->action->id);
    
    // with $_GET-parameters, HAVING ONLY supplied keys
    Yii::app()->controller->createUrl(Yii::app()->controller->action->id,
        array_intersect_key($_GET, array_flip(['id']))); // include 'id'
    
    // with all $_GET-parameters, EXCEPT supplied keys
    Yii::app()->controller->createUrl(Yii::app()->controller->action->id,
        array_diff_key($_GET, array_flip(['lg']))); // exclude 'lg'
    
    // with ALL $_GET-parameters (as mensioned in other answers)
    Yii::app()->controller->createUrl(Yii::app()->controller->action->id, $_GET);
    Yii::app()->request->url;
    

    When you don't have the same active controller, you have to specify the full path like this:

    Yii::app()->createUrl('/controller/action');
    Yii::app()->createUrl('/module/controller/action');
    

    Check out the Yii guide for building url's in general: http://www.yiiframework.com/doc/guide/1.1/en/topics.url#creating-urls

    0 讨论(0)
  • 2021-01-30 09:12

    You are definitely searching for this

    Yii::app()->request->pathInfo
    
    0 讨论(0)
  • 2021-01-30 09:13

    So, you may use

    Yii::app()->getBaseUrl(true)
    

    to get an Absolute webroot url, and strip the http[s]://

    0 讨论(0)
  • 2021-01-30 09:13
    $validar= Yii::app()->request->getParam('id');
    
    0 讨论(0)
  • 2021-01-30 09:17

    To get the absolute current request url (exactly as seen in the address bar, with GET params and http://) I found that the following works well:

    Yii::app()->request->hostInfo . Yii::app()->request->url
    
    0 讨论(0)
提交回复
热议问题