本文介绍了如何防止访客用户访问已验证的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的刀片服务器视图中有一个按钮,只有经过身份验证的用户才能访问.单击按钮后,已验证的用户将被重定向到名为 qrcode 的新页面.现在,我想限制访客用户直接访问此页面.
I've a button in my blade View that is only accessible for authenticated users. On button click, verified users are redirected to a new page named qrcode. Now i want to restrict guest users to access this page directly.
我定义了一个中间件,但是不适用于此页面,不知道为什么.请分享您的想法/建议.谢谢
I defined a middleware but that's not working for this page, don't know why. Please share your ideas/suggestion. Thanks
我的页面 qrcode.blade.php 位于 Views/Auth 文件夹中.
My page qrcode.blade.php is located in Views/Auth folder.
视图/产品/Blade.php
@auth
<button type="button" class="buy-button" onclick="window.location='{{route('firstProductQR',['firstQR' => 'qrcode']) }}'" >
Generate Dynamic QR </button>
@endauth
Route.php
Route::group([ 'middleware' => ['web']], function(){
Route::get('/',[
'uses' => 'niceActionController@getActionController'
]);
Route::get('/{firstQR}' , [
'middleware' => 'auth',
'uses' => 'niceActionController@getFirstProductQrPage',
'as'=> 'firstProductQR'
]);
});/*End Web Middleware*/
niceActionController.php
class niceActionController extends Controller
{
public function getFirstProductQrPage($firstQR)
{
return view($firstQR);
}
}
推荐答案
/* ROUTES */
Route::get('/QR/{firstQR}' , [
'uses' => 'niceActionController@getFirstProductQrPage',
'as'=> 'firstProductQR'
]);
*/
use Illuminate\Support\Facades\Auth; // important..
class niceActionController extends Controller
{
public function __construct()
{
$this->middleware('auth:web'); // use this only to make this whole controller accessible to only logged users.
}
public function getFirstProductQrPage($firstQR)
{
// or use this only for this function.
// Auth::user()->id will check if the user is logged in & have an user ID, if not found it will goes to else...
if(Auth::user()->id)
{
return view($firstQR);
} else {
return redirect('/home');
}
}
}
这篇关于如何防止访客用户访问已验证的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!