0

I have a named route with a parameter which looks like this...

Route::get('/profile/{user_id}', [ProfileController::class, 'profile'])->name('profile');

Now in one of my controller,

I have a function that calls this route like this

public function _myFunction($some_data) {
    return redirect()->route('profile', [$some_data->user_id]);
}

and in my ProfileController's profile() function, I have this.

public function profile() {

    return view('modules.profile.profile');

}

I've followed the documentation and some guides I found in SO, but I got the same error,

"Route [profile] not defined."

can somebody enlighten me on where I went wrong?

Here's what my routes/web.php looks like...

Route::middleware(['auth:web'])->group(function ($router) {

    Route::get('/profile/{user_id}', [ProfileController::class, 'profile'])->name('profile');

});
modi
  • 297
  • 2
  • 3
  • 15
  • did you cache your routes? is this route you are showing in a route group up the line that defines a 'name'/'as'? – lagbox Jan 19 '21 at 16:58
  • @lagbox Yes, I already did. BTW, I noticed something strange, I tried removing the parameter in the routes and call it normally like `redirect()->route('profile')`, I noticed that it has a hash at the end of the URL. it looked like this : `mysite.com/profile#` – modi Jan 19 '21 at 17:02
  • Are you sure your user is logged in? Your route needs authentication. – Farid Vatani Jan 19 '21 at 17:07
  • @FaridVatani Yes I'm logged in, I tried using `dd` on all calls, and I noticed that it did not go through the `ProfileController's` `profile()` function – modi Jan 19 '21 at 17:15
  • OK, Where is `$router` parameter used in routes group? If it is not used anywhere, clean it. – Farid Vatani Jan 19 '21 at 17:19

2 Answers2

0

When calling the route, you should pass the name of the attribute along with the value (as key vaue pairs). In this case, your route is expecting user_id so your route generation should look like this:

return redirect()->route('profile', ['user_id' => $some_data->user_id]);

Read more on Generating URLs To Named Routes in Laravel.

ByWaleed
  • 395
  • 4
  • 18
0

I solved the issue, and its really my bad for not providing a more specific case information and made you guys confused.

I was using socialite and called _myFunction() inside the third party's callback..

After all, the problem was the socialite's google callback, instead of placing the return redirect()->route('profile', [$user->id]) inside _myFunction(), what I did was transfer it to the callback function.

So it looked like this now...

private $to_pass_user_id;
public function handleGoogleCallback()
{
    try {

        $user = Socialite::driver('google')->user();

        $this->_myFunction($user);

        return redirect()->route('profile', [$this->to_pass_user_id]);

    } catch (Exception $e) {

        dd($e->getMessage());

    }
}

public function _myFunction($some_data) {
 
    ... my logic here
    $this->to_pass_user_id = $some_value_from_the_logic
}
modi
  • 297
  • 2
  • 3
  • 15