0

I want to use Facebook Api. my web.php file:

?php

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MembersController;
use Laravel\Socialite\Facades\Socialite;
use App\Http\Controllers\Auth\LoginController; 

Route::get('login/facebook', 'App/Http/Controllers/Auth/LoginController@redirectToProvider');
Route::get('login/facebook/callback', 
'App/Http/Controllers/Auth/LoginController@handleProviderCallBack');

my LoginController which is in in App\Http\Controllers\Auth

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Laravel\Socialite\Facades\Socialite;
use Illuminate\Support\Facades\Route;

class LoginController extends Controller
{
use AuthenticatesUsers;


protected $redirectTo = RouteServiceProvider::HOME;

public function __construct()
{
    $this->middleware('guest')->except('logout');
}

public function redirectToProvider() 
{
    return  Socialite::driver('facebook')->redirect();
}

public function handleProviderCallBack()
{
    $user = Socialite::driver('facebook')->user();
}

}

in browser i got error Target class [App/Http/Controllers/Auth/LoginController] does not exist.

can anyone help me? Thanks!

jarlh
  • 42,561
  • 8
  • 45
  • 63
Leo
  • 1
  • 4
  • What version of Laravel is this? If this is Laravel 8, your Routes syntax is wrong, and should be `Route::get('login/facebook', [LoginController::class, 'redirectToProvider']);`: https://laravel.com/docs/8.x/routing#the-default-route-files – Tim Lewis Aug 19 '21 at 14:14
  • @TimLewis you can still use the old route syntax in laravel 8 but namespaces use a `\ ` not a `/` : `App\Http\Controllers\Auth\LoginController` – Gert B. Aug 19 '21 at 14:22
  • 1
    Voted to close as possible duplicate of [Target class controller does not exist - Laravel 8](https://stackoverflow.com/questions/63807930/target-class-controller-does-not-exist-laravel-8). If this is Laravel 8; still need an update from original poster on if that's the case or not. – Tim Lewis Aug 19 '21 at 14:30

1 Answers1

0

You use a forward / instead of a backslash in your route controller namespace:

Route::get('login/facebook/callback', 
'App\Http\Controllers\Auth\LoginController@handleProviderCallBack');

change the slashes for both routes and clear the cache using php artisan route:cache

If you are using Laravel 8 Tim's comment is the right syntax:

use App\Http\Controllers\Auth\LoginController;  
Route::get('login/facebook', [LoginController::class,'redirectToProvider']);
Gert B.
  • 2,282
  • 18
  • 21
  • 1
    @TimLewis working on a 8.0 Laravel app at the moment: `Route::get('/mijn-gegevens', 'AccountController@profile')->name('users.account');` works. and the question does not say laravel 8 – Gert B. Aug 19 '21 at 14:32
  • Ah sorry, you're right. Not sure why I thought it was updated to say that it was; that's my bad. It's possible that's still the issue, apologies for the confusion there. – Tim Lewis Aug 19 '21 at 14:34
  • No problem, Off the record, i"m changing the routes to the new syntax. it is the best way to go. – Gert B. Aug 19 '21 at 14:35
  • Haha yeah, I prefer the new approach too. Clutters the `use ...;` statements at the top a bit, but aside from that, cleaner and easier. Sidenote, you say that `'AccountController@profile'` works, did you update your `->namespace('App\Http\Controllers')` in `RouteServiceProvider.php`? I've got a sandbox version of Laravel 8.12 and that syntax does not work. – Tim Lewis Aug 19 '21 at 14:39
  • Edit: Nevermind, I see it now. Default is `->namespace($this->namespace)`, but since it's commented out (`// protected $namespace = 'App\\Http\\Controllers';`), it's technically `null`. Uncommenting it removes the error. – Tim Lewis Aug 19 '21 at 14:42
  • 1
    @TimLewis yes it's in a packege, with a route group, using a namespace. – Gert B. Aug 19 '21 at 14:43