Append $_GET variable to the end of all URLs

I’m pretty sure I’m looking in the right direction but cannot find any documentation on how to accomplish this:

When a user goes to a page and has $_GET[‘t’] set, is there a way to append that to all URLs in the system after that(i.e. page.php?t=5, where t=5 is automatically added to every URL)? Since the pages are private, I do not need clean URLs.

I think it has something to do with this? http://www.yiiframework.com/doc/api/CUrlManager

You will have to override the createUrl method in CUrlManager and check if $_GET[‘t’] is set, if it is then set it in the parameters by default.

That’s where I’m stuck after looking through the documentation on that. How do you override it?

Sorry to bump this back up, but I have still not figured out how to extend createUrl. Any help would be greatly appreciated!

hi skyer2000,

take a look at the langurlManager here. It is implementing exactly what you want.





class LangCUrlManager extends CUrlManager

{	


    public function createUrl($route,$params=array(),$ampersand='&')

    {

        if (!isset($params['lang']) ) 

        {

// add here the 'lang' param to each url created by yii


        	$params['lang']=Yii::app()->GetLanguage();

       	

        }

        return parent::createUrl($route, $params, $ampersand);

    }

}




hope this helps

8)

1 Like

Just curious why you need to do this? What are you trying to achieve, do you need a value available at all points in the application? I mean is the value of t changing or is it like a session var?

Just wondering

doodle

Thanks Raoul, that was very helpful!!! Here is the slightly modified code I ended up using:




<?php

class TopidUrlManager extends CUrlManager

{

    public function createUrl($route,$params=array(),$ampersand='&')

    {

        if (isset($_GET['t']) )

        {

            $params['t'] = $_GET['t'];

        }

        return parent::createUrl($route, $params, $ampersand);

    }

}

?>



The reason I need this to happen is that users can have access to several different projects at the same time (the ‘t’ variable). Within each project, there are documents, other users assigned to that particular project, etc etc. So depending on which project they initially selected, the ‘t’ variable gets verified, then access checks are performed. Once that is completed, all queries run using that ‘t’ variable to find which documents are available within the project.

Hope that explains it! The permissions on this are extremely complex, and the program has already been written procedural style from scratch. Porting it has been difficult, but as I’m understanding more and more of Yii and getting help from the forums its starting to go faster.

Hi skyer2000,

you’re welcome ;)

… the need you’re describing could maybe be handled by states variables which are stored at the session level and os are persistant betwenne to requests. This way you don’t need to add parameter to the query.

It looks like this :




//set a state 

Yii::app()->user->setState('myVariable','myValue');




// get state

$myVariable = Yii::app()->user->getState('myVariable');



You can find more info on the Documentation and of course the API Guide

On the other hand, if you’ve already solved your problem, it’s good to know anyway (for next time ;))

ciao

8)

Hi all,

I can’t seem to get this working. I have created my cusstom class manageUrl:


class manageUrl extends CUrlManager

{

    public function createUrl($route,$params=array(),$ampersand='&')

    {

        if (isset($_GET['tour']) )

        {

            $params['tour']=$_GET['tour'];

        }


        return parent::createUrl($route, $params, $ampersand);

        

    }

}

I have declared my default class in my init() in my module:


$this->setComponents(

                        array(

                            'urlManager'=>array(

                                'class'=>'modulename.components.manageUrl'

                            )

                        )

                 );

Than when I click on a link that triggers the ‘tour’ get param the application does not automatically append the $_GET[‘tour’] on all other links.

What am I doing wrong?

Thanks in advance,

bettor

Try declaring it like this in main.php:




'urlManager'=>array(

     'class'=>'application.extensions.manageUrl',

 ),



Hi skyer2000,

Your suggestion has resolved the issue. However, one concern that I have is that I would like to define a custom url manager for a module only and now this is across the whole application. At least your suggestion has identified that the custom url manager class works fine and the issue lies with declaring it on a module level. I will have to have a deeper look on how to declare this at a module level only.

Any thoughts would be welcome too.

Cheers,

bettor