skip to Main Content

So I’m new to Laravel and I’ve only did one project where the registration had a .blade file and login had another .blade file.

now I’m working on a project where there’s only one .blade file that has 2 forms a normal username password form (login) and some js that makes the registration form appear when i click on a button so I’m having a hard time with making data appear on my database and with the routing and stuff cause there’s no /login and /registration routes cause its just 1 page.

i didn’t really try much cause i don’t understand much but i made the login and registration functions return the same view which is the login.blade file and i gave the registration and registrations functions /register routes and the login and loginPost functions /login routes but now the registration form does not respond to the data inserted doesn’t even do anything not refresh no clearing the data entered no nothing.

.blade code

    <form action="{{ route('register_post') }}" method="POST" id="form" class="sign-up-form">
            @csrf
          <h2 class="title">Sign Up</h2>
          <div class="input-field">
            <i class="fas fa-user"></i>
            <input id="username" type="text" name="username" placeholder="Username" />
            <div class="error"></div>
          </div>
          <div class="input-field">
            <i class="fas fa-envelope"></i>
            <input id="email" type="email" name="email" placeholder="Email" />
            <div class="error"></div>
          </div>
          <div class="input-field">
            <i class="fas fa-lock"></i>
            <input id="password" type="password" name="password" placeholder="Password" />
            <div class="error"></div>
          </div>
          <div class="input-field">
            <i class="fas fa-lock"></i>
            <input id="confirmPassword" type="password" name="confirmPassword" placeholder="confirm Password" />
            <div class="error"></div>
          </div>
          <input type="submit" value="Sign Up" class="btn solid 
        </form>

web.php

<?php

use IlluminateSupportFacadesRoute;
use AppHttpControllersAuthController;

Route::get('/login', [AuthController::Class,'login'])->name('login');
Route::get('/register', [AuthController::Class,'register'])->name('register');
Route::post('/login', [AuthController::Class,'loginPost'])->name('login_post');
Route::post('/register', [AuthController::Class,'registrationPost'])->name('register_post');
Route::get('/logout', [AuthController::Class,'logout'])->name('logout');

authcontroller

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppModelsUser;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesHash;
use IlluminateSupportFacadesSession;

class AuthController extends Controller
{
    function login(){
        return view('login');
    }
    function register(){
        return view('login');
    }
    function loginPost(Request $request){
$request->validate([
    'username' => 'required',   
    'password' => 'required']);

    $credentials = $request->only('username','password');
    if (Auth::attempt($credentials)){
return redirect()->intended(route('home'));
    }  
return redirect(route('login'))->with('error','Failed to login');
}

function registrationPost(request $request){
    $request->validate([
        'username' => 'required',   
        'confirmPassword' => 'required',   
        'email' => 'required|email|unique:users',   
        'password' => 'required']);

$data['username'] = $request -> username;
$data['email'] = $request -> email;
$data['password'] = Hash::make($request -> password);
$data['confirmPassword'] = $request -> confirmPassword;
$user = User::create($data);    
if(!$user){
    return redirect(route('register_post'))->with('error','Failed to Register');
}
return redirect(route('login'))->with('success','Youve been registered successfully');

function logout(){
    Session::flush();
    Auth::logout();
    return redirect(route('login'));
};
}

}

sorry if its too much

2

Answers


  1. You can dynamically set the form submission route based on the current endpoint. Here’s an example:

     <form action="{{ request()->is('register') ? route('register_post') : route('login_post') }}" method="POST" id="form" class="sign-up-form">
    
    Login or Signup to reply.
  2. You can try with same route

    In web.php

        <?php
    
    use IlluminateSupportFacadesRoute;
    use AppHttpControllersAuthController;
    
    Route::get('/register', [AuthController::Class,'register'])->name('register');
    Route::post('/register', [AuthController::Class,'registrationPost'])->name('register_post');
    Route::get('/logout', [AuthController::Class,'logout'])->name('logout');
    

    In Authcontroller

    <?php
    
    namespace AppHttpControllers;
    
    use IlluminateHttpRequest;
    use AppModelsUser;
    use IlluminateSupportFacadesAuth;
    use IlluminateSupportFacadesHash;
    use IlluminateSupportFacadesSession;
    
    class AuthController extends Controller
    {
        
        function register(){
            return view('login');
        }
    
      function registrationPost(request $request){
        $request->validate([
            'username' => 'required',   
            'confirmPassword' => 'required',   
            'email' => 'required|email|unique:users',   
            'password' => 'required']);
    
    $check = User::where('username', $request->username)->check();
    
    if($check){ 
    
    $credentials = $request->only('username','password');
            if (Auth::attempt($credentials)){
        return redirect()->intended(route('home'));
            }  
        return redirect(route('register'))->with('error','Failed to login');
    }
    
        
    $data['username'] = $request -> username;
    $data['email'] = $request -> email;
    $data['password'] = Hash::make($request -> password);
    $data['confirmPassword'] = $request -> confirmPassword;
    $user = User::create($data);    
    if(!$user){
        return redirect(route('register_post'))->with('error','Failed to Register');
    }
    return redirect(route('register'))->with('success','Youve been registered successfully');
    
    function logout(){
        Session::flush();
        Auth::logout();
        return redirect(route('register'));
    };
    }
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search