returnUrl

hi everyOne

i got a little problem

Please help me

how to get back to previous page from where the request came like php $_SERVER[‘HTTP_REFERER’];

In yii i found in one Post

Yii::app()->user->returnUrl

take me to prevoius page but

when i use, it takes me to index page of site

I m using it in $this->menu

array(‘label’=>‘Back to page’, ‘url’=>array(Yii::app()->user->returnUrl)),

is this rightway to use it here or any other way to get my thing work

Hi,

returnUrl is strictly combined with login process and I’m not sure if you can use it the way you would like to (in a menu for moving back to a previous page).

For the same purpose I’m using simple JavaScript function:


<a href="#" onclick="javascript:history.back()">Go Back</a>

but I don’t know if this will be also that easy to use it in a menu?

basically i dont want to rely on javascript

its good alternative

But i want pure php in my project

Because from every 10 person there is at least 1 who disabled javascript

Sorry, then I can’t help you further here! :[

I’m doing my website with assumption that you can’t build real professional, usable and social-oriented user design, without using JavaScript, AJAX, jQuery or other JS framework. If someone is turning of JavaScript it is his or her choice with all advantages and disadvantages of this decision.

Take a look at this extension by samdark.

But why not simply extend CHttpRequest and add $previousUrl property? You can init that value (with help of $_SERVER[‘HTTP_REFERER’]) within CHttpRequest::init()

Yes! That would be the best solution. But with this you are touching question (not answered, if I’m not mistaken) from some other thread about how to use own classes that extends core Yii classes? I.e. if they are not directly called within code, only executed by other Yii classes or methods? In other words - what should I do or write in my code to force Yii to use my own extension instead of original implementation of let’s say… CHttpRequest or CActiveRecord for example or any other class that isn’t called directly in the code?

You can find the basic core components of CWebApplication here. These components are defined by default, you don’t have to set them in config. BUT, you can set them in config and specify a different class.

So to make $previousUrl work you have to do:




'components' => array(

   ...

   'request' => array(

      'class' => 'application.components.MyHttpRequest',

   ),

   ...

),






class MyHttpRequest extends CHttpRequest

{


   public $previousUrl;


   public function init()

   {

      parent::init();

      $this->previousUrl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;

   }


}



Place the custom request class in the protected/components folder and that’s it.

As you can see, you can define and configure the application components easily.

// For CActiveRecord, you simply create a new class which extends from CActiveRecord (e.g. MyActiveRecord) and then you extend your models from that custom class.

Thank you for your idea!

Sorry, I made a mistake. Extending CActiveRecord to own class is easy. But what, when I want to use original CActiveRecord, but my own COciSchema, tweaked for my Oracle db?

Take a look here.

So… If I get, what you mean… I should extend CDbConnection to my own class (for example MyDbConnection) altering its getSchema() method so it would call my own schema (for example MyOciSchema) for Oracle driver and then change components part of configuration, just the way you shown, to specify that db component should use MyDbConnection as class, instead of original one, right?

This way I can store both MyDbConnection and MyOciSchema along with my code and be able to use original CActiveRecord (and all other db classes that are using CDbConnection) but with my own schema, right?

Did I got what you mean?

Yes, but I just noticed $_schema is private property, so you can’t extend it. Maybe there’s some way by extending CActiveRecord. But it would be perfect if you could use your own schema class in CDbConnection :mellow:

I must be missing something, because I don’t see any problem that $_schema is private, when using my approach.

Since I will not use any other DB than Oracle in this particular project, I’m thinking about extending CDbConnection to as simple class as possible, for example:


/**

 * MyDbConnection is the customized CDbConnection class, that forces all connections

 * to Oracle db to use own class MyOciSchema instead of bulid-in COciSchema.

 */

class MyDbConnection extends CDbConnection

{

    	public function getSchema()

    	{

            	if($this->_schema == NULL)

            	{

                    	if($this->getActive())

                    	{

                            	$driver = $this->getDriverName();

                            	return $this->_schema = new MyOciSchema($this);

                    	}

                    	else throw new CDbException(Yii::t('yii', 'CDbConnection is inactive and cannot perform any DB operations.'));

            	}

            	else return $this->_schema;

    	}

}

and then, as you wrote, in the configuration:


return array

(

    	'class'=>MyDbConnection,

   	...

)

But you seems to be concerned that this will not work?

It doesn’t work because it’s a private property. It would’ve to be a protected or public property.

Yes, you are right. But if I have to introduce some serious changes to COciSchema, then in my opinion it is always better to do this my way and only change one line in original source code of Yii - making $_schema protected instead of private. This way I’ll be able to use my own MyOciSchema instead of changing original one. Am I right?

There is also another option (as my friend told me). Property _$schema is only used in getSchema() and close(). So it is possible to introduce own variable, for example $myschema in extended class and use it instead of original, private one.

Than it is good idea to open bug tracker ticket asking for this, right?

If you set it to protected, you can’t extend from the original class (it’s not possible to change/override the “private” state). But yes you may introduce your own property and replace it in the related methods.

The reason for all those private properties in Yii is backward compatibility. For example, let’s assume it were protected already and in the next version we change the property name for some reason -> then your code would be broken since you still rely on the original property name. This is obviously bad for hacking. I found myself in need many times to have a protected property instead of private. Some of them changed to protected in the latest versions luckily (e.g. CClientScript::$metaTags). So I suggest to open up a ticket and then we see what happens.

I see your point. Ticked opened.

You can go previous page using this also…

<?php CHtml::button(‘Back’,array(‘onClick’=>‘history.go(-1)’)) ?> ::)

Hi,

try this


$this->redirect(Yii::app()->request->urlReferrer)

For going Previous Url it won’t work it seems :rolleyes: