To create a simple login page using Blade in Laravel, you can use the following steps:
- Create a new Blade template file for the login page. You can do this by creating a new file in the
resources/views
directory of your Laravel project, and giving it a name such aslogin.blade.php
. - Add the HTML code for the login form to the template file. This should include an
<form>
element with input fields for the user’s email address and password, as well as a submit button. You can also add any additional HTML elements or styling that you want to include on the page.
Here is an example of what the login form might look like in your Blade template:
<form method="POST" action="/login"> @csrf <label for="email">Email address:</label><br> <input type="email" id="email" name="email"><br> <label for="password">Password:</label><br> <input type="password" id="password" name="password"><br><br> <button type="submit">Log in</button> </form>
- Define a route for the login page. In your Laravel project’s
routes/web.php
file, define a route that points to the login page. For example:
Route::get('/login', function () { return view('login'); });
This route will handle GET requests to the /login
URL and render the login page using the Blade template you created.
- Handle the login form submission. To handle the form submission and perform the login action, you can define a separate route that listens for POST requests to the
/login
URL. In this route, you can use the Laravel authentication system to validate the user’s credentials and log them in.
Here is an example of how you might handle the login form submission:
Route::post('/login', function (Request $request) { $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { // The user is authenticated and can be redirected to the dashboard or another protected route } else { // The user's credentials are invalid and they should be redirected back to the login page } });
I hope this helps give you an idea of how to create a simple login page using Blade in Laravel. Let me know if you have any questions or need further assistance.