Answer the question
In order to leave comments, you need to log in
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>
public funtion getSettingsLink()
{
return \Yii::app()->createUrl('users/settings',['userId'=>$this->id]);
}
<a href=<?php echo $user->settingsLink; ?>">settings</a>
Answer the question
In order to leave comments, you need to log in
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));
it is normal to store URIs in models, url must be generated by the router.
In your case, this is most likely not true. If you change the controller, and instead of users/settings
the 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/settings
users/account
$user->settingsLink;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question