Laravel: ReflectionException - Class App\Http\Controllers\XXXX does not exist - php

I am trying to run test page using Laravel.
When I use Controller, every time I got message:
Laravel: ReflectionException - Class App\Http\Controllers\XXXX does not exist
Does somebody knows where problem is?
This is my routes/web.php:
Route::get('/hi', 'HiController#index');
HiController.php (it is in correct folder structure: app/Http/Controllers/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HiController extends Controller
{
public function index(){
return "test";
}
}
RouteServiceProvider.php
namespace App\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* #var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* #return void
*/
public function boot()
{
//
parent::boot();
}
/**
* Define the routes for the application.
*
* #return void
*/
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
//
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* #return void
*/
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* #return void
*/
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
}
Also, when I use this code in routes/web.php it works:
Route::get('/hi', function (){
return "hi";
});
It's clean Laravel 5.6 installation, on Windows, wamp64.
I tried also with "composer dump auto-load" and "php artisan config:clear" but nothing works.
Thank you in advance.

Run the following (assuming your app name is app):
php artisan app:name app
Then use the following namespace also in your controller:
app\Http\Controllers\Controller

Q: why XXXX if it's HiController? About the issue, if it returns the hi, technically, it has to work if it's vanilla.
Since its on windows and wamp64, I don't think file permissions are a thing so let's skip that.
Make sure the name of the file is indeed correct
Make sure the file indeed exists in the correct directory
Make sure the namespce is namespace App\Http\Controllers;
Add this use, just in case: use App\Http\Controllers\Controller;
Just to be sure, make sure in your composer.json you've got this (you should have since it's default):
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
If none work, try creating a new controller php artisan make:controller SomeController and try it with this one.

tried every option, the only that worked for me was restarting the vm, ngingx or laravel had something cached or something similar that throw this error.

Related

Laravel API controller not found - "Target class **** does not exist"

I know this was a bit of a breaking change in later versions and I have tried all of the methods I have found so far to rectify through stackoverflow and online and looked for spelling errors, but I am a little stuck here if anyone can help.
Laravel v8.6 - Fresh install
My api.php and the routes I've tried (Not at the same time obviously)
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use app\Http\Controllers\API\AssetTrackController;
//Route::get('/assettracking', [AssetTrackController::class, 'index']);
//Route::get('/assettracking', 'AssetTrackController#index');
//Route::resource('/assettracking', [AssetTrackController::class, 'index']);
Route::get('/assettracking', [AssetTrackController::class, 'index']);
Route::resource('/assettracking', AssetTrackController::class)->only([
'index'
]);
My controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\Models\Assettrack;
class AssetTrackController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return DB::table('assettrack')->whereRaw('ID < 420000000')->get();
}
I've tried adding to the RouteServiceProvider
protected $namespace = 'App\\Http\\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* #return void
*/
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
}
Controller path : HeatMAP/app/Http/Controllers/API/AssetTrackController.php
api.php path: HeatMAP/routes/api.php
I've loooked and can't seem to work this out based on the solutions I've already found. I feel like this one is staring me right in the face. Any help is appreciated. Thanks.
Your controller namespace is wrong.
Assuming your controller is in this order of directories:
app\Http\Controllers\Api\AssetTrackController.php
you should fix it to:
namespace App\Http\Controllers\Api;

Target class [PagesController] does not exist in laravel 8

This error keeps showing up on the browser even though I had tried a few solutions to fix it.
This is the code for PagesController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controller
{
//
public function index()
{
return view("pages.index");
}
}
Code for routes/web.php:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PagesController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
// Route::get('/', function () {
// return view('welcome');
// });
Route::get("/", [PagesController::class, 'index']);
Code for RouteServiceProvider.php:
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* #var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* The path to the "home" route for your application.
*
* #var string
*/
public const HOME = '/home';
/**
* Define your route model bindings, pattern filters, etc.
*
* #return void
*/
public function boot()
{
//
parent::boot();
}
/**
* Define the routes for the application.
*
* #return void
*/
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
//
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* #return void
*/
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
/**
* Define the "API" routes for the application.
*
* These routes are typically stateless.
*
* #return void
*/
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
}
Everything just seems working fine to me, I did add the use statement in my web.php but it still shows PagesController and the default namespace in my RouteServiceProvider.php is 'App\Http\Controllers'. The file structure is also correct the PagesController.php is under the Controllers folder. I also tried to use commands to fix it like php artisan config:cache and composer dump-autoload but it did not work. Does anyone detect the mistake that I am making here? Thanks in advance.
In the new version of laravel, it works like this
Add This code
use App\Http\Controllers\PagesController;
and use the route as
Route::get('/', [PagesController::class,'index']);
First check PagesController is not a resource controller. if PagesController is resource controller so you can add bellow code.
php artisan route:list
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
// Route::get('/', function () {
// return view('welcome');
// });
// And use resource route
Route::resource('/', PagesController::class);
use resource route don't added PagesController controller in top. and if not solve please send message me on https://devnote.in I will help you.
If Everything is in the correct folder, I think there is some problem with you route file. Use this statement and check if your Controller can be find:
Route::get('/', 'PagesController#index');
And if it does not work use this one:
Route::get('/', 'App\Http\Controllers\PagesController#index');
it seems that you are upgrading from a previous Laravel version, isn't it?
Check RouteServiceProvider.php from source:
https://github.com/laravel/laravel/blob/master/app/Providers/RouteServiceProvider.php
And make sure to make a comparison to your entire directory with:
https://github.com/laravel/laravel/compare/7.x...master
You could try putting:
use App\Http\Controllers\Controller;
In your PagesController.php as it is now needed in Laravel 8 (https://laravel.com/docs/8.x/controllers#defining-controllers)
I had a similar issue and this is how I solved it.
I included the following code in PagesController:
use App\Http\Controllers\Controller;
Then included the following code in routes (web.php)
use App\Http\Controllers\PagesController;
Route::get('/', [PagesController::class,'index']);
Laravel 8 is not a namespace prefix for your route groups where your routes are loaded.
"In previous releases of Laravel, the RouteServiceProvider contained a $namespace property. This property's value would automatically be prefixed onto controller route definitions and calls to the action helper / URL::action method. In Laravel 8.x, this property is null by default. This means that no automatic namespace prefixing will be done by Laravel."
When the namespace prefix is ​​not used, you must use the fully qualified class name for your controllers when referring to them in your ways.
use App\Http\Controllers\UserController;
Route::get('/users', [UserController::class, 'index']); // or
Route::get('/users', 'App\Http\Controllers\UserController#index'); If
you prefer the old way: App\Providers\RouteServiceProvider:
public function boot() {
...
Route::prefix('api')
->middleware('api')
->namespace('App\Http\Controllers') // <---------
->group(base_path('routes/api.php'));
... }
Do this for any route groups you want a declared namespace for.
The $namespace property:
Release Notes refer to the $namespace property to be set on your RouteServiceProvider in the Release notes and commented in your RouteServiceProvider this does not have any effect on your routes. It is currently only for adding a namespace prefix for generating URLs to actions. So you can set this variable, but it does not add these namespace prefixes, you need to make sure that you are using this variable when adding namespace in the way groups.
information is now an update guide
Laravel 8.x Docs - Upgrade Guide - Routing
With what the Upgrade Guide is showing the important part is that you are defining a namespace on your routes groups. Setting the $namespace variable by itself only helps in generating URLs to actions.
Again, and I can't stress this enough, the important part is setting the namespace for the route groups, which they just happen to be doing by referencing the member variable $namespace directly in the example.
uncomment:
you can uncomment the protected $namespace member variable in the RouteServiceProvider to go back to the old way, as the route groups are setup to use this member variable for the namespace for the groups.
// protected $namespace = 'App\\Http\\Controllers';
The only reason uncommenting that would add the namespace prefix to the Controllers assigned to the routes is because the route groups are setup to use this variable as the namespace:
<?php
namespace App\Providers;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* The path to the "home" route for your application.
*
* This is used by Laravel authentication to redirect users after login.
*
* #var string
*/
public const HOME = '/home';
/**
* The controller namespace for the application.
*
* When present, controller route declarations will automatically be prefixed with this namespace.
*
* #var string|null
*/
**protected $namespace = 'App\\Http\\Controllers';**
/**
* Define your route model bindings, pattern filters, etc.
*
* #return void
*/
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
}
/**
* Configure the rate limiters for the application.
*
* #return void
*/
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
});
}
}

Routes on laravel 8.1.0

Hi everyone I have a problem with laravel 8.1.0.
At the moment of wanting to create the routes in the web.php file
with the following code I get an error stating that the controller does not exist
web.php file
Route::get('/home/','HomeController#index');
I get the error "Illuminate\Contracts\Container\BindingResolutionException
Target class [App\Http\Controllers\HomeController] does not exist."
I have seen in the laravel documentation that version 8 has different calls for the controllers than the previous versions, I have applied the changes suggested by the documentation, however everything remains the same
I have added the following code in the file routeserviceprovider.php in app\providers
protected $namespace = 'App\Http\Controllers';
->namespace($this->namespace) //inside $this-> routes (function ()
->namespace($this->namespace) //inside Route::prefix('api')
According to the laravel documentation this should work, however I keep getting the same error.
"Illuminate\Contracts\Container\BindingResolutionException
Target class [App\Http\Controllers\HomeController] does not exist."
I have tried adding use App\Http\Controllers; in web.php file, but I have the same error message
I have tried using Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home'); and Route::get('/home', [HomeController::class, 'index']); but I have the same message error
web.php complete file
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
//Route::get('/home', [HomeController::class, 'index']);
Route::get('/persona/','PersonaController#index')->name('per','persona');
Auth::routes();
//Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/home/','HomeController#index');
routeservicesprovider.php complete file
<?php
namespace sisVentas\Providers;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* The path to the "home" route for your application.
*
* This is used by Laravel authentication to redirect users after login.
*
* #var string
*/
public const HOME = '/home';
/**
* If specified, this namespace is automatically applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* #var string
*/
protected $namespace = 'App\Http\Controllers'; //agregado
/**
* Define your route model bindings, pattern filters, etc.
*
* #return void
*/
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::middleware('web')
->namespace($this->namespace) //agregado
->group(base_path('routes/web.php'));
Route::prefix('api')
->middleware('api')
->namespace($this->namespace) //agregado
->group(base_path('routes/api.php'));
});
}
/**
* Configure the rate limiters for the application.
*
* #return void
*/
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60);
});
}
}
My homecontroller file
<?php
namespace sisVentas\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
}
The problem is the same with all the routes that I try to access.
I have lost a whole day trying to repair the routes, but I get the error of not finding them, I would like to know how to do it, thank you very much for reading this far :)
Your namespace is wrong.The app is searching for this controller in App\Http\Controller but your controllers namespace is sisVentas\Http\Controllers.
Depending on your directory structure if your root folder is sisVentas
you should use
protected $namespace = 'sisVentas\Http\Controllers';
Or if your root is App you should change your controller and providers namespace to
namespace App\Http\Controllers;
not require such kind of function in routeservicesprovider.php
protected $namespace = 'App\Http\Controllers'; x
please try this definitely will work..
Route::get('/home','App\Http\Controllers\Frontend\homeController#index');
write complete path.
and no need write to call top lavel

How to use controller outside folder App\Http\Controllers in Laravel?

I have a controller WebController at folder App\Web\Controllers\WebController.
WebController
<?php namespace App\Web\Controllers;
use Illuminate\Http\Request;
class WebController extends Controller
{
public index(){
return view('layouts.master');
}
}
Route
Route::get('home_page','WebController#index');
When i call this route, the following error appears:
Class App\Http\Controllers\WebController does not exist
It has to do with the routing system.
in App\Providers\RouteServiceProvider.php you will find the map() method:
/**
* Define the routes for the application.
*
* #return void
*/
public function map()
{
$this->mapWebRoutes();
$this->mapApiRoutes();
}
Down that class you will find the definition for the namespace of "Web Routes"
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* #return void
*/
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace) // 'App\Http\Controllers'
->group(base_path('routes/web.php'));
}
Now you just need to change the namespace() attribute to your liking
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace('App\Web\Controllers')
->group(base_path('routes/web.php'));
}
class WebController extends **Controller**
There is a problem with this relationship. You have to import controller into this file. You are missing
use App\Http\Controllers\Controller
Also, you route is wrong. Try this
Route::namespace('your namespace here')->group(function () {
Route::get('/', 'your-controller-here#your-method');
});
Do not change the configuration of the routeserviceprovider
You change default namespace in RouteServiceProvider.
protected $namespace = 'App\Http\Controllers';
App\Http\Controllers is default namespace you can change it to
protected $namespace = 'App\Web\Controllers';
and then your code should work
I would suggest either the above code from Devetoka by using the namespace, or for only 1 or two controllers (in my case), you can simply inject the full namespace in. This is tested and works.
Route::get('home_page','\App\Web\Controllers\WebController#index');

Laravel 5.7 Class App\Http\Controllers\Auth\SendsPasswordResetEmails does not exist

I'm trying to implement the reset password function using the built-in function from Laravel 5.7 as i have defined my routes in my web.php. I tried running php artisan route:list , It gave me an exception
UPDATE
Sorry for the lack of information given. I have already ran the command php artisan make:auth previously and the Auth::routes() has already been defined in web.php I am trying to access function resets in ResetPasswords traits through my ResetPasswordControllerbut it gave an exception
Class App\Http\Controllers\ResetPasswordController does not exist
I am using the pre-defined controller that is located at App\Http\Controllers\Auth\ResetPasswor.php
ResetPasswordController
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
class ResetPasswordController extends Controller
{
use ResetsPasswords;
public function reset(Request $request){
$reset = $this->reset($request);
}
/**
* Where to redirect users after resetting their password.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
}
web.php
Auth::routes();
Route::post('password/reset','ResetPasswordController#reset');
SOLUTION
I have figured out where did i do wrong i had to add a Auth\ in my routes
Route::post('password/reset','Auth\ResetPasswordController#reset');

Categories