Installation and configuration of Yii framework

Yii is a well-known high performance PHP framework, especially oriented to web applications. In this post we will tell you how to install it in our hosting plans, in addition to pointing out some of its advantages.

Still don't know Yii?

I recommend that you take a look at his official website or check the definitive guide in spanish. As they are defined:

Yii is a high-performance, component-based PHP framework for developing large-scale Web applications. It allows for maximum reusability in web programming and can speed up the development process. The name Yii (pronounced /i:/) stands for easy, efficient, and extensible.

The advantages are many, since a framework, in short, is a compendium of tools created by programming professionals (in most cases). This for us is a huge advantage, since these tools have been tested and fixed until exhaustion. In addition, since a framework (as I mentioned before) are tools, they consolidate us with an excellent base. We can go directly to development, and focus on what is important about our website, instead of wasting time on log-in/out forms or similar tasks.

To be a bit more specific, YII has tools like:

  • MVC design pattern (controller view model): This is a very advantageous tool, since it allows us to keep our workspace extremely tidy. Ideal for working with a project that hosts a group of programmers (although it is also a great practice to use when we are alone, so that when people enter our work, we already have everything in order).
  • object oriented: YII framework is fully object oriented, which is an excellent programming practice: it keeps the code tidy, clean (although this depends a lot on the programmer), and easy to manipulate/maintain.
  • Validators for forms: This is an excellent tool that provides us with a way to validate the data that is entered in a form.
    public function rules() { return array( array('title, body, file, categoryId', 'required') ); }

    Where we say that the fields title, body, file Y categoryId are required (the form is not sent if they are empty). Also, if we need our own validation, we can do something like:

    public function rules()
    {
        return array(
           array('password', 'passwordStrength', 'strength' => 'weak'),
        );
    }
    
    public function passwordStrength($attribute, $params)
    {
        if ($params['strength'] === 'weak') {
            $pattern = '/^(?=.*[a-zA-Z0-9]).{5,}$/';  
        } elseif ($params['strength'] === 'strong') {
            $pattern = '/^(?=.*d(?=.*d))(?=.*[a-zA-Z](?=.*[a-zA-Z])).{5,}$/';  
        }
     
        if(!preg_match($pattern, $this->$attribute)) {
          $this->addError($attribute, 'tu password no es muy segura!');
        }
    }
  • Complete authentication and authorization system: Which, by the way, we can configure to work with SESSION or COOKIE (although by default, SESSION is used).
    $identity = new UserIdentity($username, $password); if ($identity->authenticate()) { // Let's log in the user Yii::app()->user->login($identity); } else { // There was an error, so we show it echo $identity->errorMessage; } // Log out the user Yii::app()->user->logout();

    And now we move on to the UserIdentity class:

    class UserIdentity extends CUserIdentity
    {
        public function authenticate()
        {
            // Buscamos al usuario en la base de datos
            $record = User::model()->findByAttributes(array('username' => $this->username));
            if ($record === null) {
                // Usuario inexistente
                $this->errorCode = self::ERROR_USERNAME_INVALID;
            } else if ($record->password !== md5($this->password)) {
                // Error en la contraseña
                $this->errorCode = self::ERROR_PASSWORD_INVALID;
            } else {
                // Si llegamos a este punto, entonces decimos que no hay error
                $this->errorCode = self::ERROR_NONE;
            }
    
            return !$this->errorCode;
        }
    }
    
  • Translation of messages in a very simple way: Easy and with excellent results, this tool allows us to create multi-language sites in a short time. All we have to do is create the texts in the different languages that we want to implement on the site, and YII will take care of doing the replacements with the Yii::t() method.
  • And the most important thing, from my point of view, is that YII is very secure and robust. You can see the detail of each of the functionalities here.

Installing YII in Duplika

This is an extremely easy process. The first thing we must do is connect to the FTP with our credentials. Once there, we go to the public_html folder and proceed to dump our entire project. Once the upload is complete, you'll be glad to know that's it.

If you use databases, you just have to modify the file main.php of the folder protected/config/ by the following way:

'db' => array( 'connectionString' => 'mysql:host=localhost;dbname=DATABASE NAME', 'emulatePrepare' => true, 'username' => 'USERNAME', 'password' => 'PASSWORD', 'charset' => 'utf8' ),

We are Duplika

Give your site the hosting it deserves

no comments on Installation and configuration of Yii framework

back to blog

Duplika

Duplika

We are online, we are not a bot :)

I will be back soon

Duplika
Hi 👋
Select the prefered contact method to get in touch.
Connect via:
chat