This question already has answers here:
Error “Target class controller does not exist” when using Laravel 8
(27 answers)
Closed 1 year ago.
im getting this error in Laravel 8 after using php artisan route:list:
Illuminate\Contracts\Container\BindingResolutionException
Target class [Api\UserController] does not exist.
I have a controller in App\Http\Controllers\Api:
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index()
{
$users = User::all();
return responde()->json($users);
}
}
routes/api.php:
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\UserController;
Route::get('users', 'Api\\UserController#index');
Im new at Laravel
Since you are importing the use App\Http\Controllers\Api\UserController namespace statement you can use the ::class notation for controller class. It will also have added benefits as IDE will be able to navigate to the path unlike string
Since Laravel 8, I guess the default namespace is no longer defined to be App\Http\Controllers in the RouteServiceProvider, rather it is left upto the developer to define the default namespace by setting $namespace property with the desired value.
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\UserController;
Route::get('users', [UserController::class, 'index']);
On Larvel 8 you can't wright route as we used to before, the next syntax is this
Controller:
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
user App\Models\User;
class UserController extends Controller
{
public function index()
{
$users = User::all();
return responde()->json($users);
}
}
api.php:
<?php
use Illuminate\Support\Facades\Route;
Route::get('users', '\App\Http\Controllers\Api\UserController#index');
Or
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\UserController;
Route::get('/Pages', [UserController::class, 'index']);
Docs for routing
Related
I am learning Laravel 9, and I got a problem Target class [Admin\DashboardController] does not exist. when using prefix routing. Is someone can help me to fix it?
this code on routes/web:
Route::prefix('admin')->namespace('Admin')->group(function(){
Route::get('/','DashboardController#index')->name('dashboard');
});
this code on App\Http\Controllers\Admin\DashboardController:
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index(Request $request){
return view('pages.admin.dashboard');
}
}
You've specified an incorrect namespace in your route. As per the error message:
Target class [Admin\DashboardController] does not exist
Laravel expects to find a DashboardController in the Admin namespace, however, you've defined your DashboardController with the namespace App\Http\Controllers\Admin.
Update the namespace on your route.
Route::prefix('admin')->namespace('App\Http\Controllers\Admin')->group(function(){
Route::get('/','DashboardController#index')->name('dashboard');
});
If you read the documentation, you will see that you must use another way:
Route::prefix('admin')->namespace('Admin')->group(function(){
Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
});
This question already has answers here:
Error “Target class controller does not exist” when using Laravel 8
(27 answers)
Closed 10 months ago.
Am new to Laravel and am using Laravel 8.83.5. I created a new controller called Posts PostsController using php artisan and I also created a new directory called posts in the layouts directory but when I try accessing that file using http://127.0.0.1:8000/p I get an error that TIlluminate\Contracts\Container\BindingResolutionException
Target class [PostsController] does not exist.
here is my web.php file
<?php
// use Illuminate\Support\Facades\Auth;
// use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
// use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Route;
Route::get('/',function(){
return view('welcome');
});
Auth::routes();
Route::get('/p','PostsController#create');
Route::get('/profile/{user}','ProfilesController#index')->name('profile.show');
here is the file called create.blade.php i want to display when I run http://127.0.0.1:8000/p
#extends('layouts.app')
#section('content')
#endsection
and here is my postsController file
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function create(){
return view('posts.create');
}
}
<?php
//in web.php namespace your Posts Controller
use App\Http\Controllers\PostsController;
Route::get('/posts',[PostsController::class, 'create'])->name('create');
try this out if works
This question already has answers here:
Error “Target class controller does not exist” when using Laravel 8
(27 answers)
Closed 10 months ago.
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!
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']);
I am trying to use the inbuilt Auth for login in laravel and register. These automatically generated pages work so well, now I use Auth::guest() to check if the user is authorized to return a view: index else to login page.
But it shows:
Class 'App\Http\Controllers\Auth' not found".
Here's the code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
class StepsController extends Controller
{
public function step1()
{
if (Auth::guest())
{
return redirect('login');
}else {
return view('index');
}
}
}
You need to "import" the definition of the facade of Auth and remove the use App\Http\Controllers\Auth because does not exists.
Just add at the top (just before the class declaration):
use Auth;
And remove the other:
Having this:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
class StepsController extends Controller
{
....
Hope it helps.
PS: If you want to learn Laravel with a good guide, step-by-step, please check here: Learn Laravel
I am new to Laravel 5 coming from CodeIgniter background. I have habit to not play with routes.php. CodeIgniter automatically maps methods like controllerName/MethodName. But in Laravel 5 I am trying to do same by registering a controlller by writing this at top of app/http/sroutes.php:
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
When I run php artisan route:list it show that controller is registered. But when I see URL /public/admin/user/addRole it show addRole method not exist while I have created a method in AdminUserController.
Admin/AdminUserController.php
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getaddRole(){
echo "adding Roles";
}
}
Routes.php
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getAddRole(){
echo "adding Roles";
}
}
NB: Notice getAddRole() not getaddRole(), use camelCase
If your controller action contains multiple words, you may access the action using "dash" syntax in the URI like this:
public/admin/user/add-role
It's hard to tell because I don't see your controller code but I assume you missed adding a HTTP verb to the method name. Like:
public function getAddRole(){
// ...
}
If you want the method to match any request method, use any:
public function anyAddRole(){
// ...
}