【问题标题】:How to prevent guest user to access verified page如何防止访客用户访问已验证的页面
【发布时间】:2020-04-13 11:12:03
【问题描述】:

我的刀片视图中有一个按钮,只有经过身份验证的用户才能访问。单击按钮后,经过验证的用户将被重定向到名为 qrcode 的新页面。现在我想限制访客用户直接访问此页面。

我定义了一个中间件,但这不适用于此页面。这是为什么呢?

我的页面 qrcode.blade.php 位于 Views/Auth 文件夹中。

视图/产品/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);
}

}

【问题讨论】:

    标签: php laravel


    【解决方案1】:
    /* 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');
          }
    
        }
    
        }
    

    【讨论】:

    • 我无法为完整的控制器定义中间件,因为可以通过此控制器访问其他网页。无论如何,我使用了你的第二段代码,我仍然在访问“qrcode”页面..
    • 你确定你已经退出了吗??因为代码是绝对正确的。
    • okk,你用过 Auth Facades 吗??如果没有,则添加使用 Illuminate\Support\Facades\Auth;
    • 刚刚添加到我的控制器“使用 Illuminate\Support\Facades\Auth;”.. 什么也没发生 :( .. 我什至使用 composer dump-autoload 清除我的缓存。还是同样的问题
    • 令人惊讶的是,在 Views/Auth 文件夹下,只有这一页不接受中间件。其他工作正常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多