欢迎控制器
class WelcomeController extends Controller
{
public function ajaxRequestPost(Request $request){
$request->session()->put('data', $request->all());
return redirect('dashboard');
}
public function readSession(Request $request){
$email = $request->session()->get('data');
return $email;
}
}
路线 (Web.php)
return view('welcome');
});
Route::post('/', [App\Http\Controllers\WelcomeController::class, 'ajaxRequestPost'])->name('ajaxRequest.post');
Route::get('/dashboard/read', [App\Http\Controllers\WelcomeController::class]);
Route::post('/dashboard/read', [App\Http\Controllers\WelcomeController::class, 'readSession']);
Route::get('/dashboard', function () {
$email = session()->has('data');
if(!session()->has('data')){
return redirect('/');
}
return view('dashboard', ['name'=> $email]);
});
Route::get('/logout', function () {
session()->forget('data');
return redirect('/');
});
Welcome.blade用于从远程 API 弹出登录
<div class="hidden fixed top-0 right-0 px-6 py-4 sm:block">
<!-- insert at the bottom part of body -->
<script src="provided.js"></script>
<!-- add button to show login window -->
<button id="btnShowLogin" type="button">Login</button>
<form id="login" action="{{ url('/') }}" method="POST">
@csrf
</form>
<script type='application/javascript'>
//create new instance
var api = new API("Givenapikey");
//initialize api
api.init(function(){
//attach click event to the button
$("#btnShowLogin").click(function(){
//show login window
api.login({type:'student'},function(result){
//check result
if(result.error==0){
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrftoken"]').attr('content')
},
url: "{{ route('ajaxRequest.post') }}",
data: { username: result.details.usr_name} ,
type: 'POST',
async: false,
success: function(result){
console.log(result);
document.getElementById('login').submit();
}
});
}
});
});
});
</script>
dahsboard.blade我的目标刀片,用于显示我的用户名和其他详细信息。来自dashboard.blade 的代码片段。
<span class="info">{{ $name }}</span>
我不知道我做得对不对。路由和控制器我认为我做错了,尤其是在路由方面。dashboard.blade 中的输出是1。我不知道为什么。