Search code examples
phpdatabaseyiiyii2

Generate a unique link for each name - yii2


I am working on development of web app, Users are supposed to register on the web app.
This is my table where data is being stored post registration. My table

I would like to give every user a unique url which would be stored in the same table where details of the users is being saved so that their profile url shares their society name (society_name). For example, the website domain would be www.example.com and the users' url would be www.example.com/mysociety

I would like to save the unique generated url in in the field "url"(#14) of my table.

My User Register Controller looks like this

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 : English is not my native language. I am newbie to yii2 and stackoverflow, please excuse me for the mistakes. Thanks.


Solution

  • I solved it.

    Modified my Controller

    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,
    

    ]);}