登录后将用户重定向到他们的个人资料

时间:2017-06-12 13:11:29

标签: authentication laravel-5.3

我想将用户重定向到他们的个人资料而不是laravel的默认/主页。我在我的LoginController中试过这个:protected $ redirectTo =' / profile / {id}&#39 ;;但它返回此错误:找不到ID为{id}的用户。

这是我的路线:Route :: get(' / profile / {id}',' ProfileController @ profile');

这是我的个人资料控制器方法:

public function profile(Request $request, $id){
  $User = User::with(['complains'])->find($id);
  if(!$User) return abort(404, 'User with id:'.$id.' not found');

  return view('user.profile')->with(['user' => $User, 'complains' => $User-
  >complains]);
}

6 个答案:

答案 0 :(得分:0)

您需要传递受保护的$redirectTo = '/profile/1';

$id = Auth::user()->id;
`$redirectTo = '/profile/$id';`

如果使用Auth,请在LoginController文件中将其用作Use Auth;

答案 1 :(得分:0)

您可以修改RedirectIfAuthenticated中间件来实现此目的:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::check()) {
            return redirect()->action('ProfileController@profile', Auth::user()->id);
        }
        return $next($request);
    }
}

答案 2 :(得分:0)

这花了我很长时间才能获得帮助。 redirectTo()方法需要一个字符串作为响应,而不需要进一步的重定向,因为它本身就是一个重定向,并且根据laravel文档,该函数的类型为string。因此您可以执行以下操作:

    protected function redirectTo()
 {
    /*
      You can add any other logic needed to be satisfied here or 
      flash some sessional data here,etc.       
    */
    return 'profile/'.auth()->user()->id;
 }

答案 3 :(得分:0)

好的,我知道我来晚了,但是我认为这是个将我为别人做的事情留给同样问题的好地方,所以我做了一条仅在登录Route::get('/home', 'PagesController@index');后使用默认路线的路线, PagesController中的函数:

public function index(){ $user = User::find(auth()->user()->id); return view('pages/profile' ['user'=>$user,]); }

因此,当您登录时,它将用户数据传递到pages / profile.blade.php视图,并且工作正常。

答案 4 :(得分:0)

我认为您不需要在重定向路由上传递ID。 您可以在控制器中获取身份验证用户的ID,所以我建议您

from peachpy import *
from peachpy.x86_64 import *

def decrypt(dword1):
        #Assembler instructions to Transform
        x = Argument(uint64_t)
        with Function("DotProduct", (x,), uint64_t) as asm_function:
            #Load var and Shift right
            LOAD.ARGUMENT(rax, x)
            MOVQ(xmm2, rax)
            MOVLHPS(xmm2, xmm2)
            PSRLDQ(xmm2, 0xf)
            
            MOVQ(rax, xmm1)
            RETURN(rax)

        #Read Assembler Return
        python_function = asm_function.finalize(abi.detect()).encode().load()
        return python_function(dword1)
        
print(decrypt(0x0000000100000001))

我希望这会起作用

答案 5 :(得分:0)

粘贴到你的 homeController

public function index()
{
    return Redirect::to('profile/' .auth()->user()->id);
}