【问题标题】:Laravel 5.1 - Redirecting specific users on every pageLaravel 5.1 - 在每个页面上重定向特定用户
【发布时间】:2016-02-24 15:44:01
【问题描述】:

我正在创建一个网站,用户可以在它启动前注册。但是,当具有特定电子邮件地址的人注册时,他们已经可以访问整个网站,其他人将被列入等待名单。

假设每个拥有 gmail 地址的人都可以访问,而我希望将其余的人重定向到页面/等待列表。


例子:

Gmail 用户:

Route::get('favorites', 'FavoriteController@index');

Hotmail 用户:

Route::get('favorites', 'FavoriteController@index')

-> 让这个重定向到

Route::get('waitlist', 'WaitlistController@index')

是否有一种简单、快捷的方法可以为每条路线执行此操作,例如使用 if,像这样?

if(substr($user->email, -9)!= "@gmail.com")
{
    always redirect my routes to "Waitlist"
}

【问题讨论】:

  • 我会在您的登录功能中执行此逻辑。 :)
  • 如果您需要帮助,请告诉我。发布您的登录功能。我会帮你的。
  • @ihue 但是如果用户手动输入网址怎么办?
  • 哦。不,他们不能。发布您的登录功能。
  • 您在他们提交表单时检查它。

标签: routes laravel-5.1 url-redirection


【解决方案1】:

好的,感谢@ihue 用户,我知道解决方案。

我必须创建一个中间件,在用户每次输入 URL 时检查他们的电子邮件。

我是这样做的


我使用生成的中间件

php artisan make:middleware EmailRedirectMiddleware

然后我在中间件中编写了我的 if 循环。

<?php

namespace App\Http\Middleware;

use Auth;
use Closure;

class EmailRedirectMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $user = Auth::User();
        if(substr($user->email, -9) != "@gmail.com")
        {
            return redirect('waitinglist');
        }

        return $next($request);
    }
}

然后只需在您的routes.php 中,为您想要的每条路线使用中间件,就像这样。

Route::group(array('middleware' => 'auth'), function(){
    Route::group(array('middleware' => 'App\Http\Middleware\EmailRedirectMiddleware'), function() {
        Route::get('work', 'WorkController@index');
    });
});

第一行检查用户是否登录,第二行检查登录用户有哪些电子邮件。 如果用户没有使用 Gmail 地址,他将被重定向到 waitinglist,就像我在中间件中编码的那样

【讨论】:

    【解决方案2】:

    您可以在以下功能中更改多用户的用户重定向。 但是如果 laravel 版本更新,您在此处的更改可能会被取消。

    转到文件路径并更改功能

    vendor\laravel\framework\src\illuminate\Foundation\Auth\AuthenticatesUser.php

    protected function handleUserWasAuthenticated(Request $request, $throttles)
        {
            if ($throttles) {
                $this->clearLoginAttempts($request);
            }
    
            if (method_exists($this, 'authenticated')) {
                return $this->authenticated($request, Auth::user());
            }
    
            $user = Auth::user();
            $type = $user->Rtype;
    
            if($type==1){
                return redirect()->intended('/admin/dashboard');
            }elseif($type==2){
                return redirect()->intended('/admin/dashboard');
            }elseif($type==3){
                return redirect()->intended('/');
            }else{
            return redirect()->intended($this->redirectPath());
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-31
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2021-11-19
      • 2018-06-15
      相关资源
      最近更新 更多