Skip to content

Phase I: Authentication

crynobone edited this page May 13, 2012 · 16 revisions

Creating User Table

Laravel has a great migration feature, all you need to do is open up Terminal and run this command.

php artisan migrate:make create_users

This will generate following file (/application/migrations/{timestamp}_create_users.php):

<?php

class Create_Users {

	/**
	 * Make changes to the database.
	 *
	 * @return void
	 */
	public function up()
	{
		//
	}

	/**
	 * Revert the changes to the database.
	 *
	 * @return void
	 */
	public function down()
	{
		//
	}

}

Now we need to update up() and down() method to reflect the database schema changes. During up() we should CREATE TABLE users while during down(), we should DROP TABLE users. To do this we can use Laravel Schema class.

Create_Users::up()

public function up()
{
	// Create a new table `users`
	Schema::create('users', function ($table)
	{
		// create an auto increment field (this also add it as primary key)
		$table->increments('id');
		
		// add all the fields
		$table->string('username', 50);
		$table->string('email', 100);
		$table->string('fullname', 100);
		$table->string('password', 60); // Laravel\Hash by default generate 60 char hash

		// automatically add created_at and updated_at
		$table->timestamps();

		// add UNIQUE or INDEX keys
		$table->unique('username');
		$table->unique('email');
	});
}

Create_Users::down()

public function down()
{
	// Drop table `users`
	Schema::drop('users');
}

In order for your database to have this change run the following command in Terminal:

php artisan migrate

Creating User Model

Laravel Eloquent Orm make it so easy to create Orm, for users table we simply create create /application/models/user.php with following code.

<?php

class User extends Eloquent {

	// indicate that we have created_at and updated_at so it will update it during save()
	public static $timestamps = true;

}

And you're done.

Start The Route

Unlike most other MVC Framework, Laravel does not straight away expose all your Controller. This is to allow you to have multiple extendable Controller without the worrying if someone try to access it when you don't want them to, and Controller in Laravel is optional. You can just define a route without a controller such as

Route::get('/', function ()
{
	// this would get the view from /application/views/home/index.php (or index.blade.php)
	return View::make('home.index');
});

Controller is good to manage huge operation but for showing a static homepage do you really need one? Laravel solved this for you.

You can Route using Restful expression via following methods: Route::get(), Route::post(), Route::put(), Route::delete() or grab all of it using Route::any().

What if I need a controller

There's multiple way to add controllers to route, here how I map /login, /logout and /register.

Route::any('(login|register|logout)', function ($action)
{
	// this would call application/controllers/credential.php Controller.
	return Controller::call("credential@{$action}");
}

Couldn't we just auto-detect all the controllers?

// auto-detecting all controller
Route::controller(Controller::detect());

Controller

Same with route, you can use the normal action based controller or using Restful, here the different.

Action Based

<?php

class Credential_Controller extends Controller
{
	public function action_login() 
	{
		// show login page and handle form
	}
	public function action_logout() 
	{
		// request logout
	}
	public function action_register() 
	{
		// show register page and handle form
	}
}

Restful Based

<?php

class Credential_Controller extends Controller
{
	public $restful = true;

	public function get_login()
	{
		// show login form
	}

	public function post_login()
	{
		// post login form
	}

	public function get_logout()
	{
		// request logout
	}

	public function get_register()
	{
		// show register page
	}

	public function post_register()
	{
		// post register form
	}
}