Last active
August 29, 2015 14:07
-
-
Save thomasbabuj/105f88f3f929d42c1ff3 to your computer and use it in GitHub Desktop.
Laravel 4 Quick start ( With auth ) Guide
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) Install composer locally | |
$ curl -s https://getcomposer.org/installer | php | |
2) Create a new Laravel project | |
// create a new laravel project with latest stable inside of directory "myproject" | |
$ composer create-project laravel/laravel myproject | |
3) Config | |
// Change app/storage folder permission | |
$ chmod -R 0777 app/storage | |
4) Add machine-name to local env ( To setup your enironment - bootstrap/start.php ) | |
// for linux env | |
$ find -name 'bootstrap/start.php' -print -exec sed -i 's/your-machine-name/localhost/g' {} \; | |
// MAC OSC friendly version | |
$ find bootstrap -name 'start.php' -exec sed -i '' -e 's/your-machine-name/localhost/g' {} \; | |
5) Edit config/local/database.php | |
// Edit config/local/databse.php to suit your needs | |
// Dont forget to actually create that DB you specify | |
6) 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 | |
7) Defining which fiels are fillable in our Users model. Edit app/model/User.php and add the "fillable" array | |
protect $fillable = array ('email', 'password'); | |
8) 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(); | |
}); | |
} | |
9) Seeding | |
Edit app/database/seeds/DatabaseSeeder.php | |
public function run() | |
{ | |
Eloquent::unguard(); | |
// Uncomment this line | |
$this->call('UserTableSeeder'); | |
} | |
10) Create User Table seeder file: | |
$ touch app/database/seeds/UserTableSeeder.php | |
11) Edit app/databse/seeds/UserTableSeeder.php | |
class UserTableSeeder extends Seeder { | |
public function run() | |
{ | |
DB::table('users')->delete(); | |
User::create(array( | |
'email' => '[email protected]', | |
'password' => Hash::make('your_password') | |
)); | |
} | |
} | |
12) Create DB and seed it | |
$ php artisan migrate --env=local | |
$ php artisan db:seed --env=local | |
13) 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'); | |
}); | |
14) 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() | |
{ | |
// Dont forget to validate the user inputs | |
// Currently user inputs are not validated | |
Auth::attempt ( array ('email' => Input::get('email') , 'password' => Input::get('password')) ); | |
return Redirect::to('/'); | |
}); | |
15) Create login view: | |
$ touch app/views/login.php | |
Head to your localhost, get redirected to your login form and try loggin in ! | |
Credits : the original author is @fideloper and you can find the article here ( http://fideloper.com/laravel-4-uber-quick-start-with-auth-guide ). | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment