CreateUrl Yii - should we call it on the controller or doesn't it matter?

Should createUrl be called on the controller or in the views? It does not matter? Or does it matter?

Is there a rule we should follow? Similar methods that extend the ccontroller should be used on controllers, etc.?

+1
source share
3 answers

In view mode, you can use this snippet. You can use this snippet everywhere.

Yii::app()->createUrl(); 

But for me it is better to define the url in the controller action and use just $ some_url var in the view.

 class SomeController extends Controller { public function actionSomeAction() { $params = array( 'key1' => 'value1', 'key2' => 'value2', ); $myUrl = Yii::app()->createUrl('controller/action', $params); $this->render('some_action', array( 'my_url' => $myUrl )); } } 
+6
source

both are good when you are in the $ view, this applies to the current controller. That way you can do $this->createUrl() in any controller or view.

+2
source

You can use url creation in your view, this does not affect performance because it does not use any database queries. Create URL: Yii :: app () → createUrl (); Create an absolute URL: Yii :: app () → createAbsoluteUrl ();

You can use $ this to use these functions in your view, for example $ this-> createUrl ();

+1
source

Source: https://habr.com/ru/post/1501694/


All Articles