Tuesday, December 14, 2010

Symfony Registration and Login

So we have an initial setup for a working dev environment for people wishing to combine Symfony, Unity3d, and SmartFoxServer. Next we need some data and the best thing for that is a login and registration system for users.

Hopefully by this point you have read Chapter 3 from the Symfony book. It would also be helpful to skim through Chapter 13. We are going to use SfDoctrineGuard to lock down our site and provide us with user login capabilities. This post may get a little lengthy as it takes a bit of work to get through.

First there are 2 methods to get a hold of SfDoctrineGuard. I will describe both, but they are both run through Chapter 13.

Downloading and installing the plugin manually

  1. You need to go to this site and download the package. 
  2. Extract the package to [XAMPPInstallDir]\[GameName]\plugins
  3. rename SfDoctrineGuardPlugin-5.0.0 to SfDoctrineGuardPlugin
  4. In NetBeans in the [GameName] project go to open up config\ProjectConfiguration.class.php
  5. Add the following to the setup() above the setWebDir call:
  6. $this->enablePlugins('sfDoctrinePlugin');
  7. $this->enablePlugins('sfDoctrineGuardPlugin');
Using Symfony to install it for you

  1. Open a command prompt to [XAMPPInstallDir]\[GameName]
  2. type symfony plugin:install SfDoctrineGuardPlugin
Configuring our modules

So now we need to configure our modules. We are going to start with just the intro module. It'll be our web application's front end that includes user registration and login. Chapter 3 covers generation of modules but they generally contain way more stuff than we need.

  1. In NetBeans in the [GameName] project we need to go into apps\frontend\modules and create a folder called intro.
  2. Under the intro folder we need to create 2 more folders: actions and templates
  3. in the actions folder create a new php file actions.class.php
  4. Open actions.class.php and add the following code:

/**
* intro actions.
*
* @package [GameName]
* @subpackage intro
* @author Your name here
*/
class introActions extends sfActions
{
  /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $user = $this->getUser();
    if ($user->isAuthenticated())
    {
      return $this->redirect('@game_index');
    }


    $this->login_form = new sfGuardFormSignin();


    $this->registration_form = new sfGuardRegisterForm();
  }

}

This class gets the user, if the user is already logged in it redirects to game_index which we will define in a moment. If they aren't logged in the registration form and login form are loaded into the template and are shown to the user.

Now we need to define @game_index so open up apps\frontend\config\routing.yml and we will be changing a few things. 

#homepage
homepage:
  url:   /
  param: { module: intro, action: index }
#game_index
game_index:
  url:   /home
  param: { module: game, action: index }
#SFGuard
sf_guard_signin:
  url:   /login
  param: { module: sfGuardAuth, action: signin }

sf_guard_signout:
  url:   /logout
  param: { module: sfGuardAuth, action: signout }

sf_guard_register:
  url:   /register
  param: { module: sfGuardRegister, action: index }

sf_guard_password:
  url:   /request_password
  param: { module: sfGuardAuth, action: password }

Updating homepage forces the web application to load to that module/action when you first go to the page.

Now we need the template to display when the user opens that page. Create a new file called indexSuccess.php under apps\frontend\modules\intro\templates. This file should contain the following:



<?php use_helper('I18N') ?>
<div class="login_form">
    <form action="<?php echo url_for('@sf_guard_signin') ?>" method="post">
        <table>
            <tbody>
                <?php echo $login_form ?>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="2">
                        <input type="submit" value="<?php echo __('Signin', null, 'sf_guard') ?>" />


                        <?php $routes = $sf_context->getRouting()->getRoutes() ?>
                        <?php if (isset($routes['sf_guard_forgot_password'])): ?>
                            <a href="<?php echo url_for('@sf_guard_forgot_password') ?>"><?php echo __('Forgot your password?', null, 'sf_guard') ?></a>
                        <?php endif; ?>
                    </td>
                </tr>
            </tfoot>
        </table>
    </form>
</div>
<div class="register_form">
    <form action="<?php echo url_for('@sf_guard_register') ?>" method="post">
        <table>
            <?php echo $registration_form ?>
            <tfoot>
                <tr>
                    <td colspan="2">
                        <input type="submit" name="register" value="<?php echo __('Register', null, 'sf_guard') ?>" />
                    </td>
                </tr>
            </tfoot>
        </table>
    </form>
</div>

This takes the forms we passed in, login and register, and creates 2 forms that will redirect the user to the SfDoctrineGuardPlugin when you click submit. If you reload your browser you should now see 2 forms that look totally unformatted but function very well. You can continue to play with the styles and html or you can read Chapter 4 about the controller and view.

Now, you probably tried to create a user and it died horribly. Thats because we now need to set up the database. Which I will cover in Part 6.

No comments: