Laravel Tutorials
- What is laravel
- Laravel Installation
- Directory Structure
- htaccess
- Remove public from url
- Artisan Command
- Laravel Configuration
- Routing Configuration
- Namespaces
- Request
- Response
- Controller
- Model
- User Authentication
- Multi User Authentication
- Database Seeding
- Database
- Database Query
- ORM
- One-to-One Relationship
- One-to-Many Relationship
- Many to Many Eloquent Relationship
- Has One Through
- Has Many Through
- Querying Relations
- Middleware
- Laravel Views
- Blade Views
- Print data on view page
- Get Site URL
- Get URL Segment
- Get images from Storage folder
- Clear cache
- Form Class not found
- Flash Message in laravel
- Redirections
- path
- CRUD Projerct
- CRUD in Laravel
- CRUD progran
- Laravel Validation
- Jquery Validation
- Cookie
- Session
- Email Send in laravel
- File uploading
- CSRF Protection
- Helper in Laravel
- Helper Functions
- Guzzle Http Client
- Paypal Payment Gatway Integration
- Cron Job in laravel
- Flash message
- path
- Errors Handling
- Date Format
- Date Format Validation
- Display Image on View Page
- Update User Status using toggle button
- Delete Multiple Records using Checkbox in Laravel?
- Confirmation Before Delete Record
- Delete image from storage
- Remove/Trim Empty & Whitespace From Input Requests
- Block IP Addresses from Accessing Website
- How to Disable New User Registration in Laravel
- Redirect HTTP To HTTPS Using Laravel Middleware
- CKEditor
- slug generate unique
- Prevent Browser's Back Button After Logout
- Datatable dunamically
- encrypt & Decript
- Download File
- Rest API
- Shopping Cart
- Shopping Cart Example
- Dynamic Category-Subcategory Menu
- Ajax Search
- Interview Question
- laravel Tutorilal link
- laravel Tutorilal
Important Links
How to user Authenticate in laravel using Auth?
Auth::routes()
Auth::routes() this method is user for user authentication. this mentod is write in web.php file in routing.
The Auth::routes() method includes the routes for login, registration, logout, and password reset.
The Auth::routes() are in the laravel/ui package in the AuthRouteMethods.php file
vendor/laravel/ui/src/AuthRouteMethods.php
File name : AuthRouteMethods.php
public function auth()
{
return function ($options) {
// Authentication Routes...
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');
$this->post('logout', 'Auth\LoginController@logout')->name('logout');
// Registration Routes...
if ($options['register'] ?? true) {
$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
$this->post('register', 'Auth\RegisterController@register');
}
// Password Reset Routes...
if ($options['reset'] ?? true) {
$this->resetPassword();
}
// Password Confirmation Routes...
if ($options['confirm'] ??
class_exists($this->prependGroupNamespace('Auth\ConfirmPasswordController'))) {
$this->confirmPassword();
}
// Email Verification Routes...
if ($options['verify'] ?? false) {
$this->emailVerification();
}
};
}
The authentication controllers are located in the app/Http/Controllers/Auth folder.
In Laravel, you can protect a route using a middelware
Laravel has a builtin auth middleware, which exists in Illuminate\Auth\Middleware\Authenticate.
Open the app/Http/Controllers/HomeController.php file. It has the following code
File name : web.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('home');
}
}
The middleware() method can be either called from the controller or the route definition. So let's remove the call from the controller. Next, open the routes/web.php file and update the home route defintion as follows:
Route::get('/home', 'HomeController@index')->name('home')->middleware('auth');
File name : web.php
How to Disable User Registration?
add a parameter in routes/web.php
File name : web.php
Auth::routes(['register' => false]);
after that you won’t see Register link in top-right corner, and the route /register will show 404 page.
How to enable Enable Email Verification in laravel?
email verification with database field users.email_verified_at. By default disabled.
To enable this function, just pass a parameter in routes/web.php
File name : web.php
Auth::routes(['verify' => true]);
Finally, if you need some routes available only to verified users, use verified Middleware:
Route::get('profile', function () {
// Only verified users may enter...
})->middleware('verified');
How to Disable Reset Password?
File name : web.php
Auth::routes(['reset' => false]);
Note: you can combine registration, verification, and reset in your routes/web.php:
File name : web.php
Auth::routes([
'register' => false,
'verify' => true,
'reset' => false
]);
where user authenticates routes are defined?
User Authentication routes are listed in one method in vendor/laravel/framework/src/Illuminate/Routing/Router.php
File name : router.php
public function auth(array $options = [])
{
// Authentication Routes...
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');
$this->post('logout', 'Auth\LoginController@logout')->name('logout');
// Registration Routes...
if ($options['register'] ?? true) {
$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
$this->post('register', 'Auth\RegisterController@register');
}
// Password Reset Routes...
if ($options['reset'] ?? true) {
$this->resetPassword();
}
// Email Verification Routes...
if ($options['verify'] ?? false) {
$this->emailVerification();
}
}
How to Redirect After Registration?
By default, new registered users are redirected to URL /home. but you want to change it. it’s done in a file app/Http/Controllers/Auth/RegisterController.php
File name : index.php
class RegisterController extends Controller
{
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
...........
}
you want to register to different URLs based on the role of new user. Then you can create a separate method in the same class RegisterController, with name redirectTo():
File name : RegisterController.php
protected function redirectTo()
{
if (auth()->user()->role_id == 1) {
return '/admin';
}
return '/dashboard';
}
How to Change Field Validation Rules?
File name : app/Http/Controllers/Auth/RegisterController.php
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:6', 'confirmed'],
]);
}
How to Disable Auto-Login after User Registration?
default behavior that you may want to change is auto-login immediately after the registration form. You may want to redirect your user to a separate “success” page and expect them to log in manually later.
you need to override register() method of a trait RegistersUsers.
File name : index.php
To disable auto-login, you need to delete this particular line:
$this->guard()->login($user);
File name : index.php
Single User Authentication.
File name : web.php
Route::get('/admin', function () {
return view('auth.login');
});
Auth::routes();
// for email verify
Auth::routes(['verify' => true]);
Login Controller
File name : App\Http\Controllers\Auth\LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Response;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function login(Request $request)
{
$this->validate($request, [
'email' => 'required',
'password' => 'required',
]);
if (\Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
$request->session()->regenerate();
//return response(['success' => true], Response::HTTP_OK);
return redirect('/home')->with('loginmsg', 'Login Successfull');
}
else
{
//return redirect()->route('/admin')->with('error','Email-Address And Password Are Wrong.');
return back()->with('error', 'Sorry! Your Email and Password are Invalid.');
}
}
/*public function redirectTo()
{
// do whatever you want
return '/dashboard';
}*/
public function logout(Request $request)
{
Auth::logout();
$request->session()->flush(); // it remove the all session data
//$request->session()->forget('username'); // it remove the single session data
return redirect('/admin');
}
}
File name : index.php
Route::get('/home', 'HomeController@index')->name('home');
File name : index.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function __construct()
{
//$this->middleware('auth');
$this->middleware(['auth', 'verified']);
}
public function index()
{
return view('admin.dashboard');
}
}
File name : index.php
File name : index.php
File name : index.php