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

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.

Related

Laravel, Route not defined. yet it is

I'm developing an eccomerce site. and im getting this error on checkout. please help
here are the defined routes:
//payfast payment
Route::get('payment', 'PaymentController#confirmpayment')->name('confirmPayment');
Route::get('/payfast/success','PaymentController#success')->name('payment.success');
Route::get('/payfast/cancel','PaymentController#cancel')->name('payment.cancel');
and here is the Controller (destination route):
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use NunoMaduro\Collision\Provider;
use App\Models\Cart;
use App\Models\Product;
use DB;
use Billow\Contracts\PaymentProcessor;
Class PaymentController extends Controller
{
public function confirmpayment(PaymentProcessor $payfast)
{
$cart = Cart::where('user_id',auth()->user()->id)->where('order_id',null)->get()->toArray();
$data = [];
When you use Laravel Named Route name(), you have to use it.
Route::get('payment', 'PaymentController#confirmpayment')->name('confirmPayment');
Instead of using
payment
replace it with
confirmPayment
change your HTTP method from get to post
like this:
Route::post('payment', 'PaymentController#confirmpayment')->name('confirmPayment')
it seems you don't have route named payment you only have
confirmPayment
payment.success
payment.cancel

Target class [FrontendController] does not exist

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

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.

Laravel Controller not found

I'm learning laravel and I've tried to create a Controller.
I'm really new, so, please, elaborate.
I've used the following command to create the Controller
php artisan make:controller Api/EstadoController
So, EstadoController is under Controllers/Api
I also created a route at api.php
Route::namespace('API')->name('api.')->group(function() {
Route::get('/estados', 'EstadoController#index')->name('estados');
});
EstadoController has index function and correct namespace:
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class EstadoController extends Controller
{
public function index(){
return Estado::all();
}
}
Here is the error page:
Changing
API
to
Api
in my route resolved, like route creation.
Route:
Route::namespace('Api')->name('api.')->group(function() {
Route::get('/estados', 'EstadoController#index')->name('estados');
});
Please try to make route like this:
Route::get('/estados', 'Api\EstadoController#index');

Routing in Laravel is not working

I am using laravel 5.0
I am trying to route the following. But it is not working
Route::post('accesscontrols/permissions', 'AccescontrolsController#permission');
I don't know what error in this.
It does not access permissions function in AccesscontrolsController
I have a function in AccesscontrolsController
public function permission()
{
$roles = DB::table('roles')->get();
$permissions = DB::table('permissions')->get();
return view('accesscontrols.permission', compact('roles', 'permissions'));
}
What I have did wrong?
Your route declaration should be made in app/Http/routes.php.
Also, make sure that your controller is within the App\Http\Controllers namespace and that it extends App\Http\Controllers\Controller.
Ex:
<?php
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
public function permission()
{
...
Also, if you whish to test it in the browser (by typing "accesscontrols/permissions" in the address bar), you route should answer to the GET verb. Try to declare it using Route::get( instead.
You are returning a view in your method and you are not working with any POST data, which is strange. Are you sure you want POST request and not GET?

Resources