Webman实现iframe框架下登录的详细步骤与代码实例
Webman是一个基于ThinkPHP5的轻量级Web应用开发框架,它提供了丰富的功能和灵活的配置方式,使得开发者能够快速构建高质量的Web应用。本文将详细介绍如何使用Webman实现iframe框架下的登录功能,并提供相关的代码实例。
1. 创建登录页面
首先,我们需要创建一个登录页面,该页面包含用户名、密码输入框以及登录按钮。我们可以使用HTML和CSS来设计页面的样式。以下是一个简单的登录页面示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登录</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f2f5;
}
.login-container {
width: 300px;
padding: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.login-container h2 {
text-align: center;
margin-bottom: 20px;
}
.login-container input {
display: block;
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ddd;
border-radius: 3px;
}
.login-container button {
width: 100%;
padding: 10px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="login-container">
<h2>登录</h2>
<form action="/login" method="post">
<input type="text" name="username" placeholder="用户名" required>
<input type="password" name="password" placeholder="密码" required>
<button type="submit">登录</button>
</form>
</div>
</body>
</html>
2. 创建登录控制器
接下来,我们需要创建一个登录控制器,用于处理用户提交的登录请求。在这个控制器中,我们将验证用户的用户名和密码是否正确,并根据验证结果返回相应的响应。以下是一个简单的登录控制器示例:
<?php
namespace app\controller;
use think\Controller;
use think\Request;
use think\Db;
class Login extends Controller
{
public function index()
{
return $this->fetch();
}
public function login(Request $request)
{
$username = $request->post('username');
$password = $request->post('password');
$user = Db::name('users')->where('username', $username)->find();
if ($user && password_verify($password, $user['password'])) {
// 登录成功,跳转到主页面
session('user_id', $user['id']);
return redirect('/');
} else {
// 登录失败,返回错误信息
return '用户名或密码错误';
}
}
}
3. 配置路由
在route/app.php
文件中,我们需要添加一个路由规则,将登录页面的URL映射到登录控制器的index
方法。同时,我们还需要添加一个路由规则,将登录表单的提交URL映射到登录控制器的login
方法。以下是一个简单的路由配置示例:
<?php
use think\facade\Route;
Route::get('login', 'Login/index');
Route::post('login', 'Login/login');
4. 创建主页面
最后,我们需要创建一个主页面,用于显示登录成功后的内容。在这个页面中,我们将使用iframe框架来嵌入登录后的页面。以下是一个简单的主页面示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>主页面</title>
</head>
<body>
<h1>欢迎来到主页面!</h1>
<iframe src="/main" width="100%" height="600px"></iframe>
</body>
</html>
5. 创建主页面控制器
接下来,我们需要创建一个主页面控制器,用于处理用户访问主页面时的逻辑。在这个控制器中,我们将获取当前登录用户的ID,并将其存储到session中。以下是一个简单的主页面控制器示例:
<?php
namespace app\controller;
use think\Controller;
use think\Session;
use think\Db;
class Main extends Controller
{
public function index()
{
$user_id = Session::get('user_id');
if (!$user_id) {
// 用户未登录,重定向到登录页面
return redirect('/login');
} else {
// 用户已登录,显示主页面内容
return '欢迎回来,' . Db::name('users')->where('id', $user_id)->value('username');
}
}
}
6. 配置路由(续)
在route/app.php
文件中,我们需要添加一个路由规则,将主页面的URL映射到主页面控制器的index
方法。以下是一个简单的路由配置示例:
<?php
use think\facade\Route;
Route::get('main', 'Main/index');
至此,我们已经完成了使用Webman实现iframe框架下登录的功能。用户可以在登录页面输入用户名和密码进行登录,登录成功后将被重定向到主页面。在主页面中,我们使用了iframe框架来嵌入登录后的页面。