Tuesday, December 14, 2010

Symfony Guard and myUser

Last time we left off with the registration and login pages, but our application would die because we didn't have our database set up. So this time around we are going to add our account class, put together all the database tables and register our first user.

Editing the myUser class

So before we begin, we need to first update our myUser. So we will begin in NetBeans.

  1. Open apps\frontend\lib\myUser.class.php
  2. Update extends to be extends sfGuardSecurityUser
Simple enough. Now we need to update our settings to state that we can use the User plugin.

  1. Open apps\frontend\config\settings.yml
  2. Your .all section should have an enabled_modules. Within the [] add sfGuardAuth, sfGuardRegister, sfGuardForgotPassword
  3. under all of .settings, at the end of the file add the following:
    .actions:
      login_module:    sfGuardAuth
      login_action:    signin


      secure_module:   sfGuardAuth
      secure_action:   secure
YAML files are sensitive to spacing, so make sure the .actions is lined up with .settings and that the modules/actions are spaced in to the same distance.


Creating the Account Table

The biggest reason I use symfony can be demonstrated here with the creation of tables. You modify one file and run a command and you get tables, objects, and accessors for those objects. It complements the DRY principle I strive to uphold. This same functionality lends itself well to the [GameName]Entities project as we will see after we finish this session.

So in NetBeans in the [GameName] project we want to open the config\doctrine\schema.yml for now we will add a single table, again, mind the spacing in the code submitted as it greatly affects how your tables will turn out.

[GameName]UserProfile:
  actAs: { Timestampable: ~ }
  columns:
    user_id: {type: integer, notnull: true }
    character_slots: {type: integer, default: 1}
  relations:
    sfGuardUser:
      type:           one
      foreignType:    one
      class:          sfGuardUser
      local:          user_id
      foreign:        id
      onDelete:       cascade
      foreignAlias:   Profile
Be sure you update [GameName] to be your game name. In reality if you are forming a company and plan on having many games on your site you may want to update [GameName] to be your [SiteName] or [CompanyName]. The character_slots is just additional information that will be sent to our client in another tutorial when we cover messages to the client.


Save all the edited files if you havn't already.

First we will want to create our database, create a user, then tell symfony what the configuration is. So open the XAMPP control panel and click admin next to MySQL. Make sure Apache and MySQL are running before doing this. It will open a browser window to phpMyAdmin using the default login/pw root with no password.

In the new browser page you will see a box to Create new Database. Enter a name into the field, leave the collation where it is and click Create. It will take you to a new page with a success message. Click the button that looks like a house in the upper left to return to the beginning screen. Click the tab marked Privileges. Click the link that says Add a new User. Enter a username, leave the host blank, then enter a password in the next 2 fields. Don't change any other values, just click Go. This should show another success message. Scroll down to Database Specific Privileges and select the table you just created. Check the boxes SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, and DROP and click Go.

This will give you a user with permission to do everything symfony needs.

Open a command window and go to [XAMPPInstallDir]\[GameName] and enter the following command:


symfony configure:database "mysql:host=localhost;dbname=[DatabaseName]" [DBUser] [DBPassword]


symfony doctrine:build --all


this will display a confirmation to rebuild the databases and after you say y should go into creating all the objects and tables and get you ready for what is next.


Load up the page again and go into the registration form and create your first user. If you do this and then go into the phpMyAdmin you can click your database and see a list of the tables. Your sf_guard_user table should now have a single record, if you click on that, you will see your newly created user.


After registration you probably received an error in your browser. That is because you don't have a module called game with an action of index. Lets take care of that real quick.

  1. Create a new folder under apps\frontend\modules called game.
  2. Under game create 3 folders called actions, config, templates
  3. In the config folder create a new file called security.yml with the following code
default:
  is_secure: true
This will secure this module so the user must be logged in to access it. If they aren't, they will be redirected to the login page.

  1. In the actions folder create a file called actions.class.php and put in the following code:




<?php


/**
 * index actions.
 *
 * @package    AegisBorn
 * @subpackage index
 * @author     Your name here
 */
class gameActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
      $characters = $this->getUser()->getGuardUser()->getCharacters();
      if($characters->count() < 1)
      {
          $this->redirect('new_character');
      }
      $this->current_character = $this->getUser()->getGuardUser()->getCurrentCharacter();
  }
}

  1. Lastly we will create indexSuccess.php under templates:
<H1>[GameName]</H1&tg;


Logged in. Home page.


This completes this tutorial part. We can now create a user and log them in.

No comments: