Beruflich Dokumente
Kultur Dokumente
I. Introduction
Laravel is a royalty-free PHP Framework that was introduced in 2011. It may be young compared to
others of its kind, but it stands out for its ease, elegant syntax, and all its documentation available to
all. In addition, Laravel uses the latest versions of PHP and frequently has patches available with new
elements and updates that fix the problems, which proves that it is constantly evolving and
improving. At the moment, it is based on "Composer", the best dependency tool that manages
projects in PHP until now.
A framework can be seen as a set of tools available to achieve repetitive tasks.
II. MVC
Laravel allows clarity of the folders architecture and greatly simplifies the task for developers.
o A model contains the data to display.
o A view contains the presentation of the graphical interface.
o A controller contains the logic concerning the actions performed by the user.
The model interacts with the database, groups, processes and manages the data. The view is
mainly about showing what the model returns. Then it takes care of receiving any user
interaction (hover, mouse click, text entry, etc.). These are the actions that the controller
manages. This one supports to synchronize the model and the view. It captures all the
activities of the user and, depending on, it activates the changes to be made on the site.
III. Composer
Composer finds the PHP files needed in a project. He will pick them up and install them in the right
place. To use Laravel, you should install composer.
cd C:\wamp64\www
laravel / laravel: will search for the files to install for the Laravel Framework.
Now open the browser and enter your project name using:
Localhost/ your-project-name/public
welcome.blade.php
Located at:
1 – Resources:
Views − Views are the HTML files or templates which interact with end users and play a primary role
in MVC architecture.
Assets − the assets folder include files such as LESS and SCSS, that are required for styling the web
application.
2- Public:
3- Database
As the name suggests, this directory includes various parameters for database functionalities.
It includes three sub-directories as given below:
Seeds − this contains the classes used for unit testing database.
Migrations − this folder helps in queries for migrating the database used in the web
application.
Knowing those folders is the minimum required to use laravel. You can read about other
folders in [https://www.tutorialspoint.com/laravel/laravel_application_structure.htm].
After creating database, fill the database with some records (values) using insert button:
Now open the files .env and .env.example to link the created database to your laravel
project.
Change the following values:
DB_USERNAME= root (or any username in which you created the database)
This command will generate the required Controller files, views and routes that are
required for the Authentication.
If you open your route file (resources > routes > web.php) you should see the Route
generated
This command will create a HomeController.php file under app > Http > Controllers :
this file will contain the following code :
$this->middleware ('auth');
}
Next, we need to run the migrate command to create the tables required for
authentication. Run the following command on project Root in your terminal :
Now open /register page in your laravel application and you should see the following
page : ( open http://localhost/laravel5/public/register where laravel5 is the
project_name )
Now go to app-> user. Php and change the code to accommodate for the new
username column in user table :
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name','username','email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'username' => 'required|string|max:255|unique:users',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'username' => $data['username'],
Modify view file register.blade.php file to include username field along with name, email and
password
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Register</div>
<div class="panel-body">
<form class="form-horizontal" method="POST" action="{{ route('register') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>
@if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('username') ? ' has-error' : '' }}">
<label for="username" class="col-md-4 control-label">Username</label>
<div class="col-md-6">
<input id="username" type="text" class="form-control" name="username" value="{{ old('username') }}" required>
@if ($errors->has('username'))
<span class="help-block">
<strong>{{ $errors->first('username') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required>
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Register
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Modify file login.blade.php to login via username instead of email.
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Login</div>
<div class="panel-body">
<form class="form-horizontal" method="POST" action="{{ route('login') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="username" class="col-md-4 control-label">Username</label>
<div class="col-md-6">
<input id="username" type="text" class="form-control" name="username" value="{{
old('username') }}" required autofocus>
@if ($errors->has('username'))
<span class="help-block">
<strong>{{ $errors->first('username') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}>
Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>
<a class="btn btn-link" href="{{ route('password.request') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Now test using http://localhost/laravel5/public/register and register using
http://localhost/laravel5/public/login , you should note that you can login using the
username instead of the email.
Laravel can do a lot more but this is only a tutorial to help you get started with Laravel.
I advise anyone who wants to learn Laravel to search trainings about :
References:
https://www.5balloons.info/laravel-authentication-tutorial-login-logout-register-
forgot-password-remember-me-functionality/
Beaulne-Bélisle, L. (2014). Tutoriel sur Laravel.
tgugnani, (2017). [online] Available at: https://www.5balloons.info/laravel-authentication-
tutorial-login-logout-register-forgot-password-remember-me-functionality/ [Accessed 28
Feb. 2019].