This question already has answers here:
Error “Target class controller does not exist” when using Laravel 8
(27 answers)
Closed 2 years ago.
After installing fresh version of laravel I was getting the above mentioned error. I have done 'composer dump-autoload' and cross checked the paths and everything seems to be correct. I am sure that everyone is going to face the exact issue. So please check my answer below, it will help you.
Thank you
The issue is very well explained in the upgrade guide. But for your quick reference I will post it here.
In previous releases of Laravel, the RouteServiceProvider class contained a $namespace property with a value of App\Http\Controllers. This value of this property was used to automatically prefix controller route declarations controller route URL generation such as when calling the action helper.
In Laravel 8, this property is set to null by default. This allows your controller route declarations to use the standard PHP callable syntax, which provides better support for jumping to the controller class in many IDEs:
use App\Http\Controllers\UserController;
// Using PHP callable syntax...
Route::get('/users', [UserController::class, 'index']);
// Using string syntax...
Route::get('/users', 'App\Http\Controllers\UserController#index');
In most cases this won't impact applications that are being upgraded because your RouteServiceProvider will still contain the $namespace property with its previous value. However, if you upgrade your application by creating a brand new Laravel project, you may encounter this as a breaking change.
Related
I'm trying to extend an existing application without modifying its source code.
The application has a named route called wizard-add.
Is there a way to register \MyPackage\MyMiddleware with the existing route?
I tried attaching it via Route::getRoutes()->getByName('wizard-add')->middleware(\MyPackage\MyMiddleware::class); but since packages are registered before the routes are read, Route::getRoutes() returns an empty collection.
Thank you!
Since I didn't find a way to solve this, I extended the app's controllers and put my logic inside.
namespace MyPackage\Controllers;
use App\Controllers\WizardController;
class MyPackageWizardController extends WizardController { ... }
and then in my service provider's register() method:
$this->app->bind(WizardController::class, MyPackageWizardController::class);
So everytime the app attempts to instantiate WizardController, it instantiates MyPackageWizardController instead. I don't know if there are any drawbacks but so far this has been working perfectly for me.
I started with Laravel 7 a few weeks ago. It happened to me multiple times that after reading about a topic on the Laravel website, I wanted to check the details of a function, for example:
Illuminate\Support\Facades\Route::group()
So I went to the Laravel API, and could find the Route facade, but not the group function.
What am I doing wrong? Where do you check for example the exact signature of a function?
Thanks!
The method group in Route::group() is inherited from another class, RegistrarGroup.
See the docblock method in the source file, vendor/laravel/framework/src/Illuminate/Support/Facades/Route.php:
#method static \Illuminate\Routing\Router|\Illuminate\Routing\RouteRegistrar group(\Closure|string|array $attributes, \Closure|string $routes)
so, this is what you look for in the API documentation:
https://laravel.com/api/7.x/Illuminate/Contracts/Routing/Registrar.html#method_group
That is because a Facade, by definition, is only an 'interface' to the methods exponed by another object, so you will not find the actual methods available by visiting the facade code.
Usually you can find the actual class that a facade resolves to (when not mocked) by checking the docblock in the source code and navigate to that class.
A very useful tool to overcome this problem and provide autocompletion (and inspection) for facades on your IDE is the package https://github.com/barryvdh/laravel-ide-helper
I am trying to get api versioning in place for an API I am working on, I found this post that explained how to do it using middleware and replacing a string in the route itself. Basically specifying routes like this.
Route::group(['middleware' => ['api-version']], function() {
Route::get('/endoint', ['uses' => '{api-namespace}\EndpointController#endpoint']);
});
However, when I attempt this I get the following error
Class App\Http\Controllers\{api-namespace}\EndpointController does not exist
It would appear that the container is verifying the existence of route controller files prior to running the middleware which does the replacing. I have added the middleware to the $routeMiddleware in the Http Kernel file.
How can I accomplish this before it checks for the existence of the file?
I thought about adding this to the applications global middleware but I do not want this to run on web only on api calls
Create a different file for next version of API has some downside.
You have to create all the routes from version 1
and in my case version 2 was just some changes to 3 requests. that was the time I felt we need a fallback for this kind of operation.
then I created a simple Laravel package to support Laravel API versioning it adds fallback functionality to routes. I personally needed this long ago but didn't realize it will be achieved with such a tiny package.
https://github.com/mbpcoder/laravel-api-versioning
The problem is that uses actually tries to retrieve a class and then call the method inside, you shouldn't be encouraged to put any parameters there so restrain from doing so, instead try grouping your api routes under certain prefix and middleware like so:
Route::prefix('XXXXXXX')->group(['middleware' => ['api-version']], function() {
Route::get('/endoint', 'EndpointController#endpoint');
});
Note: My above assumption is made out of that you didn't handle changing {api-namespace} inside of your middleware class properly.
Stepping through the code allowed me to see that this is already handled by Laravel and all i needed to do was create a routes/api/v2.php file with the routes for version 2. The only problem I see is having to duplicate all routes which did not change from version 1 to version 2. I may look into modifying my RouteServiceProvider to actually inherit previous versions if they are not overridden in the requested api version rather than duplicating the routes code for every api version.
I came across this code $app['files] in a Laravel package.
I am just wondering what it does in Laravel.
$app['files'] refers to \Illuminate\Filesystem\Filesystem as you can see in the doc here.
It's the same as accessing the File facade but in some places it's better to not use facades and access them with there bindings in the $app var
.
I am trying to use Laravel and have been following the official Laravel Eloquent documentation and multiple tutorials at credible sources tuts plus
I created a model inside app/models called Stack with a table in the database called stacks with a primary key column called id, as corresponding to Laravels defaults.
<?php (Stack.php)
class Stack extends Eloquent
{
}
$stacks = Stack::all();
However when I run this model I get the following error message.
Fatal error: Class 'Eloquent' not found in C:\www\laravelproject\app\models\Stack.php on line 4
Including the official documentation and the reputable tutorials, I have also watched 2 youtube tutorials and it seems like there is no additional autoloading/including/requiring required to be declared in any new defined model's, so I am assuming something else here maybe wrong.
Do I have to manually find all classes I must autoload? If so, why is this not written in the official documentation?
I downloaded the latest laravel.phar file directly from laravel and used a .bat file to call it. (Not via composer)
Some things I have checked/tried to fix the problem.
Eloquent directory does exist at vendor\laravel\framework\src\Illuminate\Database\Eloquent
Eloquent alias set in app/config/app.php. Default 'Eloquent' => 'Illuminate\Database\Eloquent\Model'
Directly extending class like \Illuminate\Database\Eloquent\Model, error message the same but with \Illuminate\Database\Eloquent\Model instead of just Eloquent
Tried to directly extend through all variations by navigating down the entire Laravel directory structure \vendor\laravel\framework\src\Illuminate\Database\Eloquent, then \laravel\framework\src\Illuminate\Database\Eloquent etc... etc...
Bit the bullet and decided to try the second official method, I installed composer and ran the command composer create-project laravel/laravel --prefer-dist, the command screen alerted me it was downloading files which was then all successful at 100%, then alerted me that a generated application key was set successfully. I then navigate to the new directory model/User.php and receive the exact same error message as when I did it with the previuos method(laravel.phar direct download).
Thanks in advance.
Make sure you are accessing the application from the correct 'entrance'.
Thus, accessing it from app/public/index.php.
The app/public/index.php file loads the autoloader.
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* #package Laravel
* #author Taylor Otwell <taylorotwell#gmail.com>
*/
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
require __DIR__.'/../bootstrap/autoload.php';
It's possible your is namespacing. Try adding the backslash before the class being extended.
class Stack extends \Eloquent
{
}
Make sure your are setting the Eloquent alias in the app config. (app/config/app.php)
Alternatively use the class directly. I believe it's: Illuminate\Database\Eloquent\Model
class Stack extends \Illuminate\Database\Eloquent\Model {}