tp5 设置需要会员登陆页
时间: 2023-11-12 14:03:42 浏览: 28
在ThinkPHP 5中,可以通过中间件来实现需要会员登录才能访问的页面。以下是实现步骤:
1. 创建中间件文件
在app/middleware目录下创建一个名为CheckLogin.php的中间件文件,文件内容如下:
```php
<?php
namespace app\middleware;
use think\facade\Session;
class CheckLogin
{
public function handle($request, \Closure $next)
{
if (!Session::has('user')) {
return redirect('user/login');
}
return $next($request);
}
}
```
该中间件的作用是判断用户是否已登录,如果未登录则跳转到用户登录页面。
2. 注册中间件
在app/middleware.php文件中注册中间件:
```php
<?php
return [
app\middleware\CheckLogin::class,
];
```
3. 制定需要登录的页面
在路由中使用中间件来制定需要登录的页面,例如:
```php
Route::rule('user/profile', 'user/profile', 'get')->middleware('CheckLogin');
```
以上路由规则表示访问/user/profile页面时,需要先通过CheckLogin中间件的验证。
通过以上步骤,就可以在ThinkPHP 5中实现需要会员登录才能访问的页面了。