Laravel 4 Uber-Quick-Start (With auth!) Guide
Here's how to get started really quickly with Laravel 4.
Updated on 6/4/2013 since this the official launch of Laravel.
Here's what we'll accomplish:
- Installing Composer locally or globally
- Creating your project with Composer
- File permissions
- Config (Dev environment)
- Database Config (User database)
- Auth
- Routes
- Form
- Form submission
- ...
- Profit
Copy-Pasta Time!
Install composer locally:
$ curl -s https://getcomposer.org/installer | php
Alternatively, install composer globally (so it can be run from anywhere):
$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer
Create a new Laravel project:
// Creates a new laravel project with latest stable inside of directory "myproject"
$ composer create-project laravel/laravel myproject
Config:
$ chmod -R 0777 app/storage
// This is simply setting your environment - bootstrap/start.php
$ find -name 'bootstrap/start.php' -print -exec sed -i 's/your-machine-name/localhost/g' {} \; # add machine-name to local env, for instance "localhost"
// MAC OSX FRIENDLY VERSION: find bootstrap -name 'start.php' -exec sed -i '' -e 's/your-machine-name/localhost/g' {} \;
// Set up a local environment
$ mkdir app/config/local
$ cp app/config/database.php app/config/local/database.php
Edit config/local/database.php :
# Edit config/local/database.php to suit your needs
# Don't forget to actually create that DB you specify :D
Database Migration Setup:
// Create a migration file for table "users", tell Laravel we need logic to create the table
$ php artisan migrate:make create_users_table --table=users --create
Due to new changes, we need to define which fields are fillable in our Users model. Edit app/models/User.php and add the "fillable" array:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password');
// Add to the "fillable" array
protected $fillable = array('email', 'password');
// Rest of class cut-off for brevity
Edit app/database/migrations/SOME_DATE_create_users_table.php:
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('email')->unique();
$table->string('password');
$table->timestamps();
});
}
Seeding:
Edit app/database/seeds/DatabaseSeeder.php:
public function run()
{
Eloquent::unguard();
// Uncomment this line
$this->call('UserTableSeeder');
}
Create User Table seeder file:
$ touch app/database/seeds/UserTableSeeder.php
Edit app/database/seeds/UserTableSeeder.php:
<?php
class UserTableSeeder extends Seeder {
public function run()
{
DB::table('users')->delete();
User::create(array(
'email' => 'your@email.com',
'password' => Hash::make('your_password')
));
}
}
Create DB and seed it:
$ php artisan migrate --env=local
$ php artisan db:seed --env=local
Setup Auth filter and routes:
Edit app/filters.php:
Route::filter('auth', function()
{
// Slight change to redirect to login route
if (Auth::guest()) return Redirect::to('login');
});
Edit app/routes.php:
Route::get('/', array('before' => 'auth' ,function()
{
return 'Hello, '.Auth::user()->email.'!';
}));
Route::get('/login', function()
{
return View::make('login');
});
Route::post('/login', function()
{
// Validation? Not in my quickstart!
// No, but really, I'm a bad person for leaving that out
Auth::attempt( array('email' => Input::get('email'), 'password' => Input::get('password')) );
return Redirect::to('/');
});
Create login view:
$ touch app/views/login.php
Edit app/views/login.php: Grab that here.
And you're pretty much done. Head to your localhost, get redirected to your login form, and try logging in!
You should see:
'Hello, whatever@email.you.used.com!'
If you get an error try running:
$ chmod -R 0777 app/storage
again.