Target class [FrontendController] does not exist - laravel

please help. I got this error "Target class [FrontendController] does not exist."
This is my route :
<?php
use Illuminate\Support\Facades\Route;
Route::get('/','FrontendController#home');
My Controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class FrontendController extends Controller
{
public function home()
{
return view ('index');
}
}
Why this happend? can anyone help me please? :(

You have to refer to the controller, you have 2 options
Laravel 7 and lower
Route::get('/','App\Http\Controllers\FrontendController#home');
If you are using Laravel 8 then you have to change the structure of the route
Route::get('/', [App\Http\Controllers\FrontendController::class, 'home']);

Related

Laravel Target class [App\Http\Controllers\HomeController] does not exist

I was making a small blog site, but got this error. Is there anyone who can help? Currently, my homepage page that I defined in view is not working.
My homepage.blade.php page, by the way, inside the front folder is homepage.blade.
my web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Front\HomeController;
Route::get('/',"HomeController#index");
-----------------------------------------------
here is my controller
<?php
namespace App\Http\Controllers\Front;
use App\Http\Controllers\Front\HomeController;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Category;
class HomeController extends Controller
{
public function index(){
$data['categories']=Category::inRandomOrder()->get();
return view('front.layouts.homepage',$data);
}
}
You have to use like below code
Route::get('/', [HomeController::class, 'index']);
I think you should do like this -
Route::get('/','Front\HomeController#index');
because You have directory like App\Http\Controllers\Front\HomeController.
and you can't call without calling the exact path.
hope, It helps.

I am getting target class does not exist error in laravel 8

The routing process has changed in Laravel version 8. I did as in the internet but it gives an error. Where am I doing wrong?
Route file
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Backend\BackendHomeController;
Route::get('/', function () {
return view('welcome');
});
Route::get('/admin', [BackendHomeController::class, 'index'])->name("index");
Controller file
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public static function index()
{
return view("backend.index");
}
}
BackendHomeController also like this. Everything seems to be correct, but am I doing something wrong with using?
Error says
target class does not exist error
Class is referenced as BackendHomeController but your file is named HomeController. These should align for autoloading to be working.

error vendor\laravel\framework\src\Illuminate\Container\Container.php:833 in laravel

please help me, I'm learning and using Laravel 8.x . I don't know what is my error in this case.
...
vendor\laravel\framework\src\Illuminate\Container\Container.php:833
...
this is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AdminController extends Controller
{
public function index(){
echo'admin';
}
}
and this is my web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/admin','AdminController#index');
Since Laravel 8 default namespace App\Http\Controllers is not registered by default in RouteServiceProvider
So you need to use FQCN for the controller in the routes file other wise you will get the container binding resolution exception as you are getting
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AdminController;
Route::get('/admin', [AdminController::class, 'index']);
This happens from Laravel 8, since the App\Http\Controllers namespace isn't registered by default. You can register it in the corresponding service provider:
# app/Providers/RouteserviceProvider.php
// Uncomment this line
protected $namespace ='App\\Http\\Controllers';

How to Solve Controller Error in Laravel6?

I am getting AdminController error, and i am unable to solve this. I am using namespace in web files. Please let me know where i am mistaking.
My error is this..
Target class [App\Http\Controllers\Admin\AdminController] does not exist.
Here are my web.php file
Route::namespace('Admin')->prefix('admin')->name('admin.')->middleware('can:manage-users')->group(function(){
Route::resource('/users', 'UsersController',['except'=>['show','create','store']]);
Route::resource('/dashboard', 'AdminController');
Route::resource('blog', 'BlogController');
});
Here are my AdminController.php File..
<?php
namespace App\Http\Controllers;
use App\Admin;
use Illuminate\Http\Request;
class AdminController extends Controller
{
public function index()
{
return view('admin.index');
}
}
The error caused by namespace :
Route::namespace('Admin')
// Controllers within the `App\Http\Controllers\Admin` namespace
You need to remove namespace or create AdminController, UsersController, and BlogController under Controllers\Admin folder and namespace.

Laravel : Class controller does not exist

I have created a simple controller and define a function. But when i run this it returns an error that controller does not exist.
In my web.php assign a route.
<?php
Route::get('/', function () { return view('front.welcome'); });
Route::get('plan','PlanController#PlanActivity')->name('plan');
On otherside in my controller my code:
<?php
namespace App\Http\Controllers\Front;
use App\Http\Controllers\Controller as BaseController;
use Illuminate\Http\Request;
class PlanController extends Controller {
public function PlanActivity()
{
dd("hello");
//return view('admin.index');
}
}
This controller created on App\Http\Controllers\Front - on front folder
Error :
ReflectionException (-1)
Class App\Http\Controllers\PlanController does not exist
Add Front part to:
Route::get('plan', 'Front\PlanController#PlanActivity')->name('plan');
Also, change the top of the controller to:
namespace App\Http\Controllers\Front;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
And run composer du.
From the docs:
By default, the RouteServiceProvider includes your route files within a namespace group, allowing you to register controller routes without specifying the full App\Http\Controllers namespace prefix. So, you only need to specify the portion of the namespace that comes after the base App\Http\Controllers namespace.
First when defining route, make sure to use the correct path for the controller. the correct is:
Route::get('plan','Front/PlanController#PlanActivity')->name('plan');
Second you have imported Controller Class as BaseController. so you should extends BaseController not Controller:
class PlanController extends BaseController {
public function PlanActivity()
{
dd("hello");
//return view('admin.index');
}
}

Resources