【问题标题】:Generate a unique link for each name - yii2为每个名字生成一个唯一的链接 - yii2
【发布时间】:2021-06-09 06:41:38
【问题描述】:

我正在开发网络应用程序,用户应该在网络应用程序上注册。
这是我在注册后存储数据的表格。

我想为每个用户提供一个唯一的 URL,该 URL 将存储在保存用户详细信息的同一个表中,以便他们的个人资料 URL 共享他们的社会名称 (society_name)。例如,网站域名为www.example.com,用户的网址为www.example.com/mysociety

我想将生成的唯一 url 保存在我的表格的“url”(#14) 字段中。

我的用户注册控制器看起来像这样

public function actionRegister() {
    $this->layout = 'society';
    
    if (!Yii::$app->user->isGuest) {
        return $this->goHome();
    }

    
    $model = new User();
    $society = new \app\models\Society();
    
    if ($model->load(Yii::$app->request->post())) {
        
    $password= $_POST['User']['password'];
    $password_hash = Yii::$app->security->generatePasswordHash($password);
    $auth_key = Yii::$app->security->generateRandomString();
    
    $mobile=$_POST['User']['mobile'];
    $society = new \app\models\Society();
    $random_number=mt_rand(10000, 99999);
    $society->society_name = $_POST['Society']['society_name'];
    $society->contact_person = $_POST['Society']['contact_person']; 
    $society->address = $_POST['Society']['address'];       
    $society->society_id =$random_number;
    $society->mobile = $mobile;
    $society->status =0;
    $society->save();
    $session = Yii::$app->session;
    return $this->redirect(['regsuccess']);
    }  
    return $this->render('society', [
                'model' => $model,
                'society' => $society,
   ]);}

PS : 英语不是我的母语。我是 yii2 和 stackoverflow 的新手,请原谅我的错误。 谢谢。

【问题讨论】:

  • 你可以把它存储为$model->url = ...然后$model->save() 但是我这会在将来产生问题,如果你更新域名会发生什么?或者将代码迁移到模块,迁移到子域,甚至更新公司名称!看来更新数据结构会更好,然后使用 Yii 的 URL 管理器为您动态生成和解析 URL。
  • 此外,您似乎将society_name 存储在用户表上,这可能不属于那里,尤其是如果您已经在society 表上拥有它。

标签: php database yii yii2


【解决方案1】:

我解决了。

修改了我的控制器

public function actionRegister() {
$this->layout = 'society';
if (!Yii::$app->user->isGuest) {
    return $this->goHome();
}


$model = new User();
$society = new \app\models\Society();
if ($model->load(Yii::$app->request->post())) {
$password= $_POST['User']['password'];
$password_hash = Yii::$app->security->generatePasswordHash($password);
$auth_key = Yii::$app->security->generateRandomString();
$mobile=$_POST['User']['mobile'];
$society = new \app\models\Society();
$random_number=mt_rand(10000, 99999);
$vp_string = trim($_POST['Society']['society_name']);

$vp_string = html_entity_decode($vp_string);

$vp_string = strip_tags($vp_string);

$vp_string = strtolower($vp_string);

$vp_string = preg_replace('[^ a-z0-9_\.]', ' ', $vp_string);

$vp_string = preg_replace('~ ~', '-', $vp_string);
$society->society_name = $_POST['Society']['society_name'];
$society->contact_person = $_POST['Society']['contact_person']; 
$society->address = $_POST['Society']['address'];       
$society->society_id =$random_number;
$society->mobile = $mobile;
$society->url = $vp_string;
$society->status =0;
$society->save();
$session = Yii::$app->session;
return $this->redirect(['regsuccess']);
}  
return $this->render('society', [
            'model' => $model,
            'society' => $society,

]);}

【讨论】:

    猜你喜欢
    • 2012-03-15
    • 1970-01-01
    • 2015-01-30
    • 2013-03-14
    • 2020-06-02
    • 2011-06-06
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    相关资源
    最近更新 更多