Yii2, PHP the easy way

jet_ezra

Tumusiime Ezra Jnr

Posted on March 23, 2022

Yii2, PHP the easy way

Welcome back to my other writing about tech, today we discuss about Yii2, yes, Yii2 also known as Yes it is, for security, fast and effecient.

Whereas most of us confuse the two, but Yii2 is not an extension of yii1, it is a complete re-write of first version which implies yii2 was a complete re-write ground-up, not extension of yii1.

Background
Compared to the most of the powerful frameworks, yii is slightly younger, that is, it first got its official release in December 3rd, 2008 but this was version 1 and it was not super-production-ready and battle-tested to start the competition with the then existing Django, Spring etc. Therefore a new, Version 1.1.2, which was more battle-tested had to be released and that was on March 14th, 2010. And that's how we see the coming up of yii1. But the journey did not stop, this version still had some loops here and there and the developers felt there was more need for further improvement however, they could not tamper with the already existing mature-seeming version 1.1.2, and there was also a tremendous job that was taking place to PHP itself which made PHP run faster and better. Therefore in 2013, May, the first unstable version of yii2 was released which was later followed by the release of the stable version in 2018 October that supported new versions of PHP. Yii2 currently fully supports PHP 8+.
Version 3 of the framework is in progress and can be found here

Getting started with Yii2.

Yii2 gives you two templates of projects you can start from and these are as follows.

  • The basic template (which is most likely what you want)
  • The advanced template.

Requirements.

  1. PHP installed and running. especially PHP7.2+
  2. You will need composer installed. You can quickly install composer from composer official download page.
  3. Apache2 or nginx installed and running.
  4. Database of your choice running e.g. PostgreSQL or MySQL. You can find the list of all databases supported by Yii here

This short tutorial will illustrate the setup of yii2 basic project, you may find the setup of the advanced project here

Installation.
You can quickly set up a yii2 basic project by running the following command.

composer create-project --prefer-dist yiisoft/yii2-app-basic basic
Enter fullscreen mode Exit fullscreen mode
  • composer is the package manager for PHP applications and can be used to perform various action as seen by running composer in your terminal.
  • create-project is the yii2 command that tells yii that you are actually creating a new application.
  • --prefer-dist yiisoft/yii2-app-basic this tells yii2 the template type you would like to start with. In our case we chose the basic template.
  • basic is the application name, this can be any name, like polls, facebook, blog.

Now since you have application, let's navigate to it using and open it with vs code.

cd appname && code .  
Enter fullscreen mode Exit fullscreen mode

Replace appname with your application name you just created.

Project structure

Image description

The boilerplate seems alot but let's break it down in a minute.

Yii2 is in the nature of MVC that is Model View Controller and here, they do exactly how they sound. That is, models are placed in models, Views that is where you place what users see and finally controllers carry the mapping and logic of your entire application.

And we have completed the whole boilerplate! WAIT, HAVE WE?

So what are the rest?

  • assets help you to configure your JS, CSS and other dependencies of your entire application also known as Application Assets and these are done in AppAsset.php.
  • commands these help you to register your new commands with the application. These can later be later accessed and ran via your console.
  • config as the name suggests, this is where the entire application configurations sit. From databases, to configuring modules and many more.
  • controllers, as mentioned above, this is where routing and app logic resides. Logic that ranges from authentication, opening pages, redirecting and many more.
  • mail, this is functional for sending emails.
  • models, the database tables' representation in your code. These help you to interuct with database tables as PHP classes.
  • ... More of these files you may never require them at all.
  • vendor stores your dependencies you have installed, look at this as your node_modules folder, or your virtualenv in python.
  • views contains the pages(HTML) that users finally interuct with.
  • web This is the entry folder. Your application will first get into this folder to access your index.php, which will redirect it accordingly thus running the entire application.

To run our application just get the root directory, make sure you are on the level of yii file. And run the following.

php yii serve
Enter fullscreen mode Exit fullscreen mode

Navigate to http://localhost:8080/

If everything was successful, you should be seeing the page below.

Image description

And that is it!! You have your first PHP Yii2 framework app up and running.

Advantages of YII2

  • Authentication and authorization out of the box. This comes with pre-configured helpers and classes to handle sessions, roles and permissions and many more.
  • Inbuilt ORM(Object Relational Mapper) that supports over 6 databases and supports working with already existing databases with ease!
  • Code generation. In Yii2, you may find it crazy but in most cases you will will find code generation being automated by the gii module. You can find this at localhost:8080/gii. This enables generate modules, models, views, CRUD and many more.
  • Bootstrap 4 out of the box. With Yii2 you don't need to configure bootstrap and jQuery as these two come preconfigured for you to start using without any additional work.

Let me hear from you about the performance of yii2 and if you are a fun too! otherwise, goodluck with your yii journey.

💖 💪 🙅 🚩
jet_ezra
Tumusiime Ezra Jnr

Posted on March 23, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Yii2, PHP the easy way
yii2 Yii2, PHP the easy way

March 23, 2022