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
You can dynamically set the form submission route based on the current endpoint. Here’s an example:
You can try with same route
In web.php
In Authcontroller