Laravel 4 Uber-Quick-Start (With auth!) Guide
Here's how to get started really quickly with Laravel 4.
Here's what we'll accomplish:
- Clone dev branch
- Get Composer (dir=bin)
- Composer install
- File permissions
- Config (Dev environment)
- Database Config (User database)
- Auth
- Routes
- Form
- Form submission
- ...
- Profit
Copy-Pasta Time!
Setup:
// Clone into develop branch, set the remote name of the skeleton app to "laravel", rather than "origin"
$ git clone -b develop -o laravel https://github.com/laravel/laravel.git myproject
$ cd myproject
$ mkdir bin
$ curl -s https://getcomposer.org/installer | php -- --install-dir=bin
$ php bin/composer.phar install
...wait...
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"
// 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($table)
{
$table->increments('id');
$table->string('email')->unique();
$table->string('password');
$table->timestamps();
});
}
Seeding:
Edit app/database/seeds/DatabaseSeeder.php:
public function run()
{
// 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 bin/composer.phar dump-autoload
$ 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( ['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.
