Google+
Shineyrock web design & consultancy

Shineyrock

blog

  • like 2 23

    Programming With Yii2: Integrating User Registration

    Final product image
    What You'll Be Creating

    If you're asking, "What's Yii?", check out my earlier tutorial: Introduction to the Yii Framework, which reviews the benefits of Yii and includes an overview of what's new in Yii 2.0, released October 12th, 2014.

    This is part four of a series on Yii2. In Programming With Yii2: Getting Started, we set up Yii2 locally, built a Hello World application, set up a remote server, and used Github to deploy our code. In part two, we learned about Yii's implementation of its Model View Controller architecture and how to build web pages and forms that collect and validate data. In part three, we learned about working with databases and ActiveRecord. In this tutorial, we'll walk you through integrating a popular user registration plugin.

    For these examples, we'll continue building on our hello app available in the Tuts+ repository so that you can follow along.

    User Registration for Your Web Application

    Almost any web application of any usefulness requires some form of user registration. Yii provides a couple of ways to approach user registration, both relatively straightforward. This is one of the things I like about using Yii over vanilla PHP—in moments, I can have a fully featured web application framework ready to build cool stuff on.

    There are few reasons to rebuild the wheel and code user authentication and its many requirements and corollary features from scratch, e.g. sending emails and authentication for registration verification, password recovery, third party social authentication, et al. 

    The Yii2 Advanced Application Template provides built-in user registration; we're using this approach in my Building Your Startup series. However, in this series, we've been building on Yii2's basic application template. The default basic application template includes hard coded user login, which isn't very useful.

    Yii2 Basic Application Template Static Login

    Another approach to user registration is to use third party extensions. For this tutorial, I'll walk you through using Dmitry Erofeev's Yii2-User extension. Documentation for Yii2-User is available here. Erofeev's building other plugins for Yii2 as well.

    Dektriums Yii2-User Documentation

    Installation of Yii2-User

    Let's get started installing the Yii2-User extension. We'll follow the installation instructions.

    Installing Yii2-User With Composer

    First, we need to add Yii2-User to composer's required extensions. Edit the /composer.json file to include Yii2-User:

    Then, when we update composer, you'll see something like this:

    Update the Database

    Next, we run the database migration for Yii2-User. This creates the database tables that the extension requires. These will manage user accounts and credentials.

    Update the Configuration File

    Next, we need to tell Yii to use the Yii2-User component. In /config/web.php, we replace the default User component...

    ... with the Yii2-User component:

    Activate the SwiftMailer

    Since Yii2-User uses email to send out registration confirmations and forgotten passwords, it's time to activate our SwiftMailer configuration. In config/web.php, replace the default Mailer configuration here...

    ... with this—you'll need to include your own SMTP credentials:

    Integration of Yii2-User

    Now, we need to link our navigation bar to the Yii2-User controller paths. In /views/layouts/main.php, we update the navigation bar array definition for the Bootstrap menu. Replace the current navigation bar...

    ... with the following array definition:

    Reload the application and click the Sign Up link in the navigation bar. You should see something like this:

    Yii2-User Sign Up Page

    When you click Sign Up, you'll see the confirmation notification. This tells us an email has been sent that we need to click on to verify our registration.

    Yii2-User Sign Up Confirmation Notice

    You'll receive an email much like this one:

    Yii2-User Confirmation Email

    Click on the link in the email and you'll see something like this:

    Yii2-User Sign Up Confirmation

    Notice the login state maintained by Yii2 and the Yii2-User component—it's displayed in the navigation bar above. Click Logout and let's walk through the Sign In page:

    Yii2-User Sign In Form

    Yii2-User also includes password recovery:

    Yii2-User Forgot Your Password

    Just like that, we have a hugely important core authentication component for our application.

    Yii2-User also has a number of configuration features which you can explore further on your own, e.g. third party social authentication. We'll likely return to them in a later tutorial.

    What's Next?

    I hope you've found this useful for your own Yii2 web application. If you'd like to know when the next Yii2 tutorial arrives, follow me @reifman on Twitter or check my instructor page. My instructor page will include all the articles from this series as soon as they are published.

    Related Links

    martijn broeders

    founder/ strategic creative at shineyrock web design & consultancy
    e-mail: .(JavaScript must be enabled to view this email address)
    phone: 434 210 0245

By - category

    By - date