使用参数laravel 5.3 |重定向Response.php第462行中的InvalidArgumentException

时间:2016-12-05 08:05:13

标签: php redirect routing parameter-passing laravel-5.3

嗨,我是laravel的新手并且卡住了。 在我的应用程序中有三种类型的用户。他们的登录数据保存在单个表中(使用auth生成的用户表),并使用char来定义用户类型。我想要做的是当他们登录时,用url上的user_id(从users表获取)将它们重定向到相关页面。 底线是我想获取网址

  

本地主机:8000 / ESProfile / 15

其中值15是表的user_id。 到目前为止我所做的就是下面的内容。

在AuthenticatesUsers.php中(由artisan auth自动生成)

    protected function authenticated(Request $request, $user)
{
    $userId = $user->id;
    if($user->type === 's') {
        return redirect('ESProfile', $userId);
    }
    else if($user->type === 'p') {
        return redirect('/ePProfile');
    }
    else if($user->type === 'i') {
        return redirect('/ePDashboard');
    }
}

在web.php中

Route::get('ESProfile/{id}', function($id){ return view('ESeeker.details')->with('$id'); });

当我尝试登录时,它会给出

InvalidArgumentException in UrlGenerator.php line 314: Route [ESProfile] not defined.错误。

一起
  

in UrlGenerator.php line 314
at UrlGenerator->route('ESProfile', array('15'), true) in helpers.php line 692
at route('ESProfile', array('15')) in AuthenticatesUsers.php line 123
at LoginController->authenticated(object(Request), object(User)) in AuthenticatesUsers.php line 107
at LoginController->sendLoginResponse(object(Request)) in AuthenticatesUsers.php line 46
at LoginController->login(object(Request))
at call_user_func_array(array(object(LoginController), 'login'), array(object(Request))) in Controller.php line 55
at Controller->callAction('login', array(object(Request))) in ControllerDispatcher.php line 44
at ControllerDispatcher->dispatch(object(Route), object(LoginController), 'login') in Route.php line 190
at Route->runController() in Route.php line 144
at Route->run(object(Request)) in Router.php line 642
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in RedirectIfAuthenticated.php line 24
at RedirectIfAuthenticated->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Router.php line 644
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 618
at Router->dispatchToRoute(object(Request)) in Router.php line 596
at Router->dispatch(object(Request)) in Kernel.php line 267
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 54
at require_once('C:\xampp\htdocs\Project\public\index.php') in server.php line 21

提前致谢。

2 个答案:

答案 0 :(得分:0)

在您看来,您必须使用以下代码

route('ESProfile', ['id' => $id])

显然会因为你没有在任何地方定义ESProfile而失败。

将您的路线更改为以下wrok:

Route::get('ESProfile/{id}', function($id) {
  return view('ESeeker.details')->with('$id');
})->name('ESProfile');

检查named routes的文档。

答案 1 :(得分:0)

经过多次试验后找到了答案。

在RegistersUsers.php(来自php artisan make:auth的自动生成的文件)

    public function register(Request $request){

    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));

    $this->guard()->login($user);
    $user = $request -> user();
        if(($user->type) == 's'){
            return $this->registered($request, $user)?: redirect()-> intended('/addPreferences/ESId='.$user->id);
        }
        if(($user->type) == 'p'){
            return $this->registered($request, $user)?: redirect()-> intended('/ePProfile/epID='.$user->id);
        }
     }

用预期的方法附加所需的路径就可以了。谢谢。 (特别感谢iCode)