Target class does not exist. Routing Prefix Laravel 9 - php

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');
});

Related

problems with routes laravel

After data from a form is saved i wanted to get back to the admin page.
I checked the database and the new data was there but I got an error:
"Route [pages.admin] not defined."
My Admin Controller code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Models\Admin;
class AdminController extends Controller
public function store(Request $request)
{
// Validation code
// Saveing code
return redirect()->route('pages.admin')
->with('success', 'Admins created successfully.');
}
My Page Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controllerpublic
function admin(){
return view('pages.admin');
}
Routes:
Route::get('/admin', 'PagesController#admin');
Route::post('admin_form', 'AdminController#store');
Would appreciate the help.
I looked in online sources but it didn't help
You are confusing the name of a view with the name of a route. Your view has the name pages.admin because there is a admin.blade.php view in the pages folder within the views folder of your application.
For route('pages.admin') to work, you need to assign a name to a route. You may do this by using name() when defining your route.
Route::get('/admin', 'PagesController#admin')->name('pages.admin');
It is a good practise to always name routes. For example: it allows you to just change the url without having to worry about your redirects breaking, since they use the name that hasn't changed.
I found a video and changed my code in the controller to
return redirect('admin');
and it worked.

Target class [Api\UserController] does not exist [duplicate]

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

Class App\Http\Controllers\ does not exist

This is my Route:
Route::get('/hello', '#HomeController#index');
This is my HomeController
namespace App\Http\Controllers;
use app\Requests;
use Illuminate\Http\Requests;
use Spatie\Activitylog\Models\Activity;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
class HomeController extends Controller {
public function index() {
$lastActivity = Spatie\Activitylog\Models\Activity::all();
return view('activity'), compact('lastActivity'));
}
}
But I keep on getting an error message:
ReflectionException in Route.php line 280:
Class App\Http\Controllers\ does not exist
What can I do? Thanks.
At the first of controller you do not need to put #, its just for method of controller.
Route::get('/hello', 'HomeController#index');
You have an extra # in your method call.
'#HomeController#index'
should be
'HomeController#index'
Whenever errors of type ReflectionException occur, you should check the routes in the routes/api.php and routes/web.php files to correct them ok understand do it carefully next time.

Class App\Http\Controllers\homeController does not exist

i'm using laravel 5 , in rutes.php i have this code :
Route::get('about',"homeController#about");
and in App\Http\Controllers\ i have file homeController.php that contains :
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller as BaseController;
class homeController extends BaseController{
public function about(){
return view::make('about');
}
}
but it throws this error : Class App\Http\Controllers\homeController does not exist .
how can i fix it ?
here is structure of the project and controllers :
First, check if you write the name of the controller correctly.
If it is, there are 3 methods:
Route::get('/about', 'App\Http\Controllers\homeController#about');
Write all the paths where your controller there is.
Route::get('/about', [HomeController::class, 'about');
Go to file RouteServiceProvider.php in Providers folder
Search for //protected $namespace = 'App\\Http\\Controllers';
( You will find it in comment)
Remove // of comment and save.
with this method, you will able to use the name of the controller directly without
writing all the paths.
Change all
homeController
To
HomeController
For HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function welcome()
{
return view('welcome');
}
}
For web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'welcome']);
Its work for me
change your routing like below:
use App\Http\Controllers\HomeController;
Route::get('/about', [HomeController::class, 'index'])->name('home');
for more info look at this page:
https://laravel.com/docs/8.x/routing
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller as BaseController;
class homeController extends BaseController{
public function about(){
return view::make('about');
}
}
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller as BaseController;
class homeController extends BaseController{
public function about(){
return view::make('about');
}
}
Should works perfectly. Are you sure that name of file is homeController.php ?
I faced a similar issue with the Home controller, in my case I saw the route was configured as following:
Route::get('/home',"homeController#index");
when I changed the above code to the following then it worked.
Route::get('/home',"App\Http\Controllers\HomeController#about");
In your case first, check the spelling first, whether it should be HomeController or homeController.
You can change your route code to the following
so you can try to change the following code
Route::get('about',"HomeController#about");
to
Route::get('about', 'App\Http\Controllers\HomeController#about');
or
Route::get('/about', 'App\Http\Controllers\HomeController#about');
Hope this will work.
It can happen because of these two causes:
Typo Error
Laravel Cache
1) Type Error
For this check, please check your HomeController file name and the class name in that file. Both should be same with case sensitivity.
HomeController.php
class HomeController extends
2) Laravel Cache
Laravel stores file caches with previous configurations. To refresh the cache, do this commands in the command window and then try again
php artisan cache:clear
php artisan view:clear
php artisan optimize
Hope it solved!.
Yes. That is right because you are not giving right path to the action in the routes. Either you update core files for path or provide manually in the route. e.g
you have
Route::get('about',"homeController#about");
try this route
Route::get('/about', [App\Http\Controllers\homeController::class,'about']);
or you can type route as
Route::get('/about', 'App\Http\Controllers\homeController#about');
furthermore you can check if you have right code in the controller function.
You are trying Laravel 7.x and before routing schema. Refer to 8.x documentation.
New syntaxis using as [HomeController::class, 'index'] or you need to add namespace before Controller name like App\Http\Controllers\HomeController.
Update the path to your route by adding .
Route::get('/home', [\App\Http\Controllers\HomeController::class, 'index'])->name('home');
Please update your route
use App\Http\Controllers\HomeController;
Route::get('/about', [HomeController::class, 'about']);

Laravel 5 registering a controller to map all methods

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(){
// ...
}

Categories