关于页面跳转 session 丢失问题
LoginController.php
namespace App\Http\Controllers\Manager;
use Config;
use DB;
use App\Http\Requests;
use http\Env\Response;
use Request;
class LoginController
{
//登录成功跳转页面
protected $redirectTo = '/';
public function login_do(){
$name = Request::post('username');
$pass = Request::post('password');
if(!$name || !$pass){
alert('参数有误!','/login.html');
exit();
}
$info = DB::table('sys_manager')
->where([
['deleted','=','2'],
['group_id','=','1'],
['state','=','1'],
['user_name','=',''.$name.''],
['user_pass','=',''.md5crypt($pass).''],
])
->select('id','user_name','user_pass','roles')
->first();
if($info){
$user_info['id'] = $info->id;
$user_info['user_name'] = $info->user_name;
$user_info['user_pass'] = $info->user_pass;
$user_info['roles'] = $info->roles;
}else{
$user_info='';
}
if ($user_info) {
//获取左侧菜单
// 存储数据到session...
session(['sidebar' => $this->getSidebar($user_info)]);
session(['uid' => $user_info['id']]);
session(['roles' => $user_info['roles']]);
if($user_info['roles']=='SUPER'){
$roleName = '超级管理员';
}
else{
$role = DB::table('sys_manager_role')
->where('deleted','=','2')
->whereIn('id', [$user_info['roles']])
->get();
$roleName='';
if($role){
$rolesArr=array();
foreach ($role as $key => $item) {
$rolesArr[]=$item->role_name;
}
$roleName=($rolesArr)?implode(',', $rolesArr):'';
}
}
session(['username' => $name]);
session(['role_name' => $roleName]);
//此处可以打印session['username'];
return redirect('/');
} else {
return redirect('login.html');
}
}
Controller.php
<?php
namespace App\Http\Controllers\Manager;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use App\Http\Requests;
use Request;
use View;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
function __construct() {
$this->middleware(function ($request, $next) {
//未登录的处理
$this->dosomething();
return $next($request);
});
}
public function dosomething(){
$a = session()->all();
printr($a);
if(session('uid')==''){
header("location:/login.html");
exit;
}
$sidebar = session('sidebar');
View::share('sidebar', $sidebar);
}
public function keep_token($token){
$url = './other/';
$file_name = 'token.txt';
if(is_dir($url)){
//将整个文件内容读入到一个字符串中
$json_info = file_get_contents($url.$file_name);
$token_str = json_decode($json_info ,true);
if($token_str['hi']){
$token_str['hi'] = $token;
$info = $token_str;
}else{
$info = array('hi'=>$token);
$info = array_merge($token_str,$info);
}
$info_json = json_encode($info);
//写入TXT文件内容
$result = file_put_contents($url.$file_name,$info_json);
}else{
//创建目录
mkdir('./other','0777');
//写入TXT文件内容
$result = file_put_contents($url.$file_name,$token);
}
return $result;
}
}
此时打印的结果是
Array ( [_token] => v1F8ohzOMORklpOEXzNCT4GT1SfRBv8euavi1uLo )
2 Comments
抱歉这个问题解决了 打扰大家了 不用回答了
那是什么原因,我的也是只有_token?