How to Create Simple Login And Registration in Laravel

<h3>How to Create Simple Login And Registration in Laravel.</h3>
<h3> Step1:Create Migiration</h3>
create user table using migration and create user table simplay apply this command in your composer.

php artisan migrate:make --create="users" CreateUsersTable

<h3> Step:2 Create Model</h3>
In laravel app->models->User.php but User.php already created.

<h3> Step:3 Create Controllers</h3>
Now Create UsersController.php in App->controllers and insert these code in user controller.

class UsersController extends BaseController {
///////////////////here is method name
}

<h3>Step:4 Create Master Layoutfile</h3>
Laravel user use common layout file for all function so create a master layout file Create a Folder or directory in layouts in app->views->layouts and create main.blade.php and insert following code.
<h4><b>main.blade.php</b></h4>


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{{ HTML::script('js/jquery.js') }}
{{ HTML::script('js/bootstrap.js') }}
{{ HTML::style('css/bootstrap.css') }}
{{ HTML::style('packages/bootstrap/css/bootstrap.min.css') }}
{{ HTML::style('css/main.css')}}
<title>MY First Laravel App</title>
</head>
<body style="background-color:#DBEAF9;">
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<ul class="nav">
@if(!Auth::check())
<li>{{ HTML::link('users/register', 'Register') }}</li>
<li>{{ HTML::link('/', 'Login') }}</li>
@else
<li>{{ HTML::link('users/logout', 'logout') }}</li>
<li>{{ HTML::link('editprofile', 'Edit Profile') }}</li>
@endif
</ul>
</div>
</div>
</div>
<div class="container">
@if(Session::has('message'))
<p class="alert">{{ Session::get('message') }}</p>
@endif
{{ $content }}
</div>
</body>
</html>

Now include this common layout in in UsersController.php and crate a method name index

class UsersController extends BaseController {

protected $layout = "layouts.main";

public function index()
{
/***********get login view***********/
$this->layout->content = View::make('users.login1');
}

}

<h3>Create Login View </h3>
create a folder or directory in app->view  and create users direcory create a file login1.blade.php and insert the following code.
<h4><b>login1.blade.php</b></h4>

<h2><a href="#">Please Login</a></h2>
{{ Form::open(array('url'=>'login', 'class'=>'form-signin')) }}
{{ Form::text('email', null, array('class'=>'input-block-level', 'placeholder'=>'Email Address')) }}<br>
{{ Form::password('password', array('class'=>'input-block-level', 'placeholder'=>'Password')) }}<br>
{{ Form::submit('Login', array('class'=>'btn btn-large btn-primary btn-block'))}}
{{ Form::close() }}

Create another file name register.blade.php
<h4><b>register.blade.php</b></h4>


<h2><a href="#">Register</a></h2>
{{ Form::open(array('', 'class'=>'form-signup','files' => true)) }}
<ul>
@foreach($errors->all() as $error)
<li style="color:#FF0000; font-weight:bold;">{{ $error }}</li>
@endforeach
</ul>
{{ Form::text('firstname', null, array('class'=>'input-block-level', 'placeholder'=>'First Name')) }}<br>
{{ Form::text('lastname', null, array('class'=>'input-block-level', 'placeholder'=>'Last Name')) }}<br>
{{ Form::text('email', null, array('class'=>'input-block-level', 'placeholder'=>'Email Address')) }}<br>
{{ Form::password('password', array('class'=>'input-block-level', 'placeholder'=>'Password')) }}<br>
{{ Form::password('password_confirmation', array('class'=>'input-block-level', 'placeholder'=>'Confirm Password')) }}<br>
{{ Form::submit('Register', array('class'=>'btn btn-large btn-primary btn-block'))}}<br>
{{ Form::close() }}

<h3>Configure you routing file</h3>
Now open app->routes.php

Route::get('/','UsersController@index');////////////////////load login view
Route::post('login','UsersController@postlogin');///////////Post Login data
Route::get('users/register','UsersController@getRegister');//////Get Login Registration view
Route::post('users/register','UsersController@postregister');/////////Post register data

<h3>Now run your Project</h3>
login view

<h3>How Add the following code in UsersController.php</h3>

//////////////get register View
public function getRegister()
{
/***********get Register view***********/
$this->layout->content = View::make('users.register');////////////////////this is used for load view
}
//////////////////////////////////register post code
public function postregister() {
/*****************Post Register Data*************/

$user = new User;
$user->first_name = Input::get('firstname');
$user->last_name = Input::get('lastname');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->remember_token = Input::get('_token');
//print_r($user);die;
$user->save();
return Redirect::to('/')->with('message', 'Thanks for registering Please Login!');
}
}

Now Add the following code for Login and authencation

public function postlogin(){

/***********Post Login and Check Authencation login view***********/
if (Auth::attempt(array('email'=>Input::get('email'), 'password'=>Input::get('password')))) {
return Redirect::to('/logedIn')->with('message', 'You are now logged in!');
} else {
return Redirect::to('/')->with('message', 'Your username/password combination was incorrect')->withInput();
}
}

Now add the New Routes in routes.php add the following route and also add the logout routes.

Route::get('logedIn','UsersController@logedIn');
Route::get('users/logout', function()
{
Auth::logout();
return Redirect::to('/');
});

Now OPen you UsersController.php and insert the following code.

<?php
class UsersController extends BaseController {

protected $layout = "layouts.main";

public function index()
{
/***********get login view***********/
$this->layout->content = View::make('users.login1');
}

public function getRegister()
{
/***********get Register view***********/
$this->layout->content = View::make('users.register');
}

public function postlogin(){

/***********Post Login and Check Authencation login view***********/
if (Auth::attempt(array('email'=>Input::get('email'), 'password'=>Input::get('password')))) {
return Redirect::to('/logedIn')->with('message', 'You are now logged in!');
} else {
return Redirect::to('/')->with('message', 'Your username/password combination was incorrect')->withInput();
}
}

public function postregister() {
/*****************Post Register Data*************/
$validator = Validator::make(Input::all(), User::$rules);
if ($validator->passes()) {
$user = new User;
$user->first_name = Input::get('firstname');
$user->last_name = Input::get('lastname');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->remember_token = Input::get('_token');
//print_r($user);die;
$user->save();
return Redirect::to('/')->with('message', 'Thanks for registering Please Login!');
} else {
return Redirect::to('users/register')->with('message', 'The following errors occurred')->withErrors($validator)->withInput();
}
}

public function logedIn()
{
/************get desboard*************/
$this->layout->content = View::make('users.desboard');
}

public function edit()
{
/////**********Update Profile*********/
$user = new User;
$id= Input::get('id');
$last_name = Input::get('last_name');
$email = Input::get('email');
$firstname =Input::get('firstname');

DB::table('users')
->where('id', $id)
->update(array('last_name' =>$last_name,'first_name' =>$firstname));
return Redirect::to('logedIn')->with('message', 'Profile Successfully Updated!');

}

}

Create a New file in app->view->users->desboard.blade.php and insertthe following code

<h3>Welcome {{ Auth::user()->first_name }}&nbsp;&nbsp;{{ Auth::user()->last_name }}</h3>

Now Add New Routes in routes.php and create new blade file in app->view->users->editprofile.blade.php and insert the following code.
<h4><b>editprofile.blade.php</b></h4>


Route::get('editprofile','UsersController@editprofile');
Route::post('edit','UsersController@edit');

<h2><a href="#">Edit Your Profile</a></h2>
{{ Form::open(array('url'=>'edit', 'class'=>'form-signup')) }}
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
<label>First Name</label>
<input type="text" name="firstname" value="{{ Auth::user()->first_name }}"/><br>
<label>Last Name</label>
<input type="text" name="lastname" value="{{ Auth::user()->last_name }}"/><br>
<label>Email</label>
<input type="text" name="email" value="{{ Auth::user()->email }}"/><br>
{{ Form::submit('Update', array('class'=>'btn btn-large btn-primary btn-block'))}}<br>
{{ Form::close() }}

Create Validation in register.blade.php change the following code

/////////////////////////oldcode/////////////////////////
public function postregister() {
/*****************Post Register Data*************/

$user = new User;
$user->first_name = Input::get('firstname');
$user->last_name = Input::get('lastname');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->remember_token = Input::get('_token');
//print_r($user);die;
$user->save();
return Redirect::to('/')->with('message', 'Thanks for registering Please Login!');
}
}

///////////////////////////////New code////////////////////////////////
public function postregister() {
/*****************Post Register Data*************/
$validator = Validator::make(Input::all(), User::$rules);
if ($validator->passes()) {
$user = new User;
$user->first_name = Input::get('firstname');
$user->last_name = Input::get('lastname');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->remember_token = Input::get('_token');
//print_r($user);die;
$user->save();
return Redirect::to('/')->with('message', 'Thanks for registering Please Login!');
} else {
return Redirect::to('users/register')->with('message', 'The following errors occurred')->withErrors($validator)->withInput();
}
}

And Open the User.php in App->view->User.php and insert the following code
<h2><b>User.php</b></h2>

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

use UserTrait, RemindableTrait;

protected $table = 'users';

protected $hidden = array('password', 'remember_token');

public static $rules = array(
'firstname'=>'required|alpha|min:2',
'lastname'=>'required|alpha|min:2',
'email'=>'required|email|unique:users',
'password'=>'required|alpha_num|between:6,12|confirmed',
'password_confirmation'=>'required|alpha_num|between:6,12'
);

}

register

registersucc

Share Button

About admin

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Author: pramod kumar kumawat Mob: 9269727062
Free WordPress Themes - Download High-quality Templates