H
H
HaruAtari2014-01-27 07:39:26
Yii
HaruAtari, 2014-01-27 07:39:26

Is it ok in Yii to store urls in models?

Good morning.
There is a system on Yii. Template engines are not used for layout. I do it in the standard Yii way: through \CController::render()the file with html markup is connected, into which data is inserted through <?php echo ''; ?>
the AR model is passed to the view, whose properties are substituted. It is more convenient than in the controller to form an array of parameters and pass it. Models are built in such a way that none of its methods, except for heteros, are called in the view.
The system uses CNC. Therefore, link addresses are not entered directly, but are entered through \Yii::app()->createUrl();. But in this case, when changing the address of the page, you need to look for this link in all views and change it.
I'm thinking of making methods in the model that will return the corresponding links. And then instead of this code in the view:

<a href="<?php echo \Yii::app()->createUrl('users/Settings',['userId'=>$user->id]); ?>">settings</a>

I describe the method in the model:
public funtion getSettingsLink() 
{
  return \Yii::app()->createUrl('users/settings',['userId'=>$this->id]); 
}

and in the view:
<a href=<?php echo $user->settingsLink; ?>">settings</a>

This option seems logical to me. link generation is encapsulated within an object. But the fact that the model contains url-dependent methods is confusing (roughly speaking, it works with the view).
Please let me know if this approach is worth using. Or are there other ways to solve this problem?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
enchikiben, 2014-01-27
@EnChikiben

In yii, the CNC can be configured in the config, and if you want a different address, then change it there, but leave the controller alone, and in the view you can write like this:

echo CHtml::link('settings',array('users/settings','userId'=>$user->id));

S
Sergey, 2014-01-27
Protko @Fesor

it is normal to store URIs in models, url must be generated by the router.

G
goodnickoff, 2014-01-27
@goodnickoff

In your case, this is most likely not true. If you change the controller, and instead of users/settingsthe link turns into member/settings, then getting a link for membrs from the users model does not look pretty. The same goes for action. If instead the link changes to then getting it through the same does not look logical. I would advise you to pay more attention to the design so as not to radically change controllers and actions later. users/settingsusers/account$user->settingsLink;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question