Sie sind auf Seite 1von 10

Laravel

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.

You can install composer using this link: https://getcomposer.org/

IV. Create your first project


After installing composer, you have to install the project.

Open windows CMD using run (windows key in keyboard + R)

In CMD change directory using:

cd C:\wamp64\www

Create the project using:


composer create-project laravel/laravel your-project-name
Composer: means using the Composer.

Create-project: means that we create a project.

laravel / laravel: will search for the files to install for the Laravel Framework.

your-project-name: we put the name we want to give to our project.

Now open the browser and enter your project name using:

Localhost/ your-project-name/public

This command should return the page:

welcome.blade.php
Located at:

C: /wamp64/www / your-project-name /ressources/views

This returned page is considered as the welcome (index) of your website.

V. The structure of folders

1 – Resources:

Views − Views are the HTML files or templates which interact with end users and play a primary role
in MVC architecture.

Lang − this folder includes configuration for localization or internalization.

Assets − the assets folder include files such as LESS and SCSS, that are required for styling the web
application.
2- Public:

It is the root folder which helps in initializing the Laravel application.

It includes the following files and folders:

.htaccess − this file gives the server configuration.

Javascript and css − these files are considered as assets.

index.php − this file is required for the initialization of a web application.

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.

Factories − this folder is used to generate large number of data records.

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].

VI. Create authentication


Laravel framework comes with functionalities for your application that includes Login,
Logout, Register, Forgot Password and remember me functions.

In localhost/phpmyadmin, connect using (root, password =) or (root, password = root) and


create a database called user with following (Note that the database is not optimal):

CREATE TABLE `users` (

`id` int(11) NOT NULL,

`Name` varchar(500) NOT NULL,

`username` varchar(191) NOT NULL,

`email` varchar(500) NOT NULL,

`Password` varchar(500) NOT NULL,

`updated_at` date NOT NULL,


`created_at` date NOT NULL,

`remember_token` varchar(191) DEFAULT NULL,

`msg` varchar(6000) NOT NULL

After creating database, fill the database with some records (values) using insert button:

After filling the form click in run (or executer).

Now open the files .env and .env.example to link the created database to your laravel
project.
Change the following values:

DB_DATABASE =” name of your database “

DB_USERNAME= root (or any username in which you created the database)

DB_PASSWORD= (for my case the password is empty)

Now on your project root directory run following command:

php artisan make:auth

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 :

Public function __construct ()

$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 :

php artisan migrate

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 )

Only those steps are required to create an authentication using laravel.


Now fill the form and connect using login at the top , you will be able to connect and
to see your dashboard ( plz set the default of msg field in sql database as NULL ) :
Now we will login using username instead of email , first alter database to add
username field ( if doesn’t exist ) , In phpmyadmin click in the sql button and run the
command ( Look the first picture ) :

ALTER TABLE `users` ADD `username` VARCHAR


(191) NOT NULL AFTER `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;

class User extends Authenticatable


{
use Notifiable;

/**
* 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',
];
}

Modify the RegisterController.php to accommodate for the username field :


<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller


{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/

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 :

 Laravel crud operations ( creating , updating , destroying tasks )


 Creating databases with 1:1 or 1: n tables (you can also create database
using phpmyadmin and changing .env file to link database to Laravel
project).
 Blade templates and less (to be used with css).

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].

Das könnte Ihnen auch gefallen