Class Controller not found in laravel 4 - php

I'm beginner in Laravel and when I'm trying to use Controller I'm getting this error
Illuminate\Contracts\Container\BindingResolutionException
Target class [PostsController] does not exist.
http://127.0.0.1:8000/posts
routes directory contain web.php to route and there I'm using
Route::get('/posts', 'PostsController#index');
to redirect to the class PostsController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function index() {
echo "asdas";
}
}

Before diving into your problem, you should really not use Laravel 4. We are almost at version 9.
Laravel 4 lacks features, security fixes and current PHP versions support (yes, at the time of this writing, all versions < 7 shouldn't be used).
That being said, you probably have a good reason, like working on a legacy project that's hard to update.
What you could do is to use the fully qualified namespace inside your route file.
Route::get('/posts', '\App\Http\Controllers\PostsController#index');
Also, triple check that you are using Laravel 4. The problem you have could also be caused by one of the latest Laravel update (version 8) where the default namespace was removed from the App\Providers\RouteServiceProvider file.
If you made a mistake and you are on Laravel 8, then just use the following notation:
Route::get('/posts', [\App\Http\Controllers\PostsController::class,'index']);
[EDIT]
As mentioned in the comments, you could also uncomment // protected $namespace = 'App\\Http\\Controllers'; in your route file. Of course, this apply to Laravel 8 so if you are on Laravel 4, just ignore this edit.

Related

Laravel ReflectionException : Class CustomerAccountController does not exist

I have this project on Laravel 5.7 and Voyager on WAMP with 2 issues, the first one is when I run php artisan route:list and the result is:
ReflectionException : Class CustomerAccountController does not exist
at C:\wamp64\www\cell_marketplace\vendor\laravel\framework\src\Illuminate\Container\Container.php:779
And actually the class exists and I'm using its functions on another processes and it's working, I've checked namespace, ran composer dump-autoload with no results.
The second one, I've created a BREAD on Voyager, and I got the model class and controller class, but when I go to the index of that resource again got this:
ReflectionException: Class DropOffController does not exist in \vendor\laravel\framework\src\Illuminate\Container\Container.php:779
And the controller exists and has a function that it's actually working, so I think that's related with the first one but if anybody can help I'd really appreciate it
The CustomerAccountController class:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Voyager\VoyagerBaseController;
use App\Models\CustomerAccount;
use App\Models\CustomerAccountTransaction;
use App\Models\Provider;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Stripe\Charge;
use Stripe\Stripe;
use TCG\Voyager\Facades\Voyager;
class CustomerAccountController extends VoyagerBaseController
{
[...]
I found the issue, it needed the absolute path to the controller, not the relative when configuring the BREAD for that resource, the project is in Laravel Voyager.

Using controller::class in Route::resource is causing namespace to be included two times resulting in Controller Not Found

I have a Laravel application where I'm getting exception controller does not exist due to wrong namespace. The namespace is getting repeated two times causing this issue. I don't know why it's repeating and I've been searching on internet but nothing found related to this.
Target class [App\Http\Controllers\App\Http\Controllers\customerController] does not exist.
The namespace here is being repeated which is causing the error. The namespace set in RouteServiceProvider is below:
protected $namespace = 'App\\Http\\Controllers';
My routes in routes/web.php file are:
use App\Http\Controllers\customerController;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
Route::resource("customers", customerController::class);
Solution
If I use old Laravel syntax then the error goes away:
Route:resource("customers", "customerController");
But I want to know why the above approach is behaving strangely. I'm using Laravel 8 in this project.
Update
I was doing one thing wrong. We don't have to specify namespace in RouteServiceProvider if we want to use classes as routes. I added namespace in routes services provider, that's why when I was using class syntax, it included namespace twice causing error. But when I used string based controller name then namespace resolved correctly.
use App\Http\Controllers\CustomerController;
use capital letter for starting of controller name
then
Route::resource('customers', CustomerController::class);

Laravel upgrade broke model paths

I've performed a long-overdue update on a Laravel project from v5.7 to v9 and ran into a Target class does not exist error. I found this guide and used the first method to resolve the error (adding the namespace into the RoutesServiceProvider.php boot function). This resolved that error but now, everything is giving me Class "App\Whatever" not found.
I did notice that models are now stored in a Models directory within the app directory rather than directly in app, so have moved them to Models. I figured that might be breaking my use App\Whatever; lines at the top of my controllers, so I've tried use App\Models\Whatever and also use app\Models\Whatever (since the "a" is lowercase in the directory name) but no effect.
I should note I don't really have a good grasp of namespaces, MVC frameworks etc. so ELI5 etc :-)
Some of my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Thing;
use App\AnotherThing;
...
public function thing_summary($id) // show the summary view of the thing
{
if(Auth::check()) {
$thing = Thing::find($id);
...
Laravel 7/8/9 sticks with strict namespacing. When you move the models to a new directory, you need to update the namespace in the models themselves (if specified at all) and any file that has a use for the model. If the models move from app/ to app/models, the namespace must be changed to App/Models

PHP Laravel - Controller does not exist

I am new to PHP and Laravel. I am creating my first Laravel API where I am working with Post object.
In order to do so I use a PostController. I have created this controller using the php artisan command: php artisan make:controller PostController --api. This created a PostController class inside the app\Http\Controllers.
When I want to append this controller to the api routes using the Route::resource function I came across something strange. The course I followed told me to declare the controller like so: Route::resource('posts', 'PostController');. When testing the api php artisan route:list this gave the following error:Target class [PostController] does not exist.
Then I did some research and found an other way to declare the controller inside the api routes using:
use App\Http\Controllers\PostController;
Route::resource('posts', PostController::class);
This worked for me but I don't have any clue why the first declaration failed. I have looked inside my PostController class to see any typo's. But I couldn't find any:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
...
}
The reason for this is just like the exception says: it could not find the PostController in your declared namespace.
The reason for that highly depends on the Laravel version you are using. Laravel 8 removed the default namespacing to App\Http\Controllers, so if you are using Laravel 8 but have followed a tutorial for an earlier Laravel version, that might be the reason.
However, using the declaration using class imports is the better way to do it anyways, so I would stick with that.
Since Laravel 8, there were some changes to the automatic controller namespace prefixing.
https://laravel.com/docs/8.x/upgrade#routing
You can continue to use the original auto-prefixed controller routing, see the 4th paragraph on the upgrade documentation. But the new way is recommended.
You should tell Route::resource method the controller's namespace. As you did in your second try.
If you wanna do it with your first attempt, you can tell it the required namespace
Route::resource('posts', 'App\Http\Controllers\PostController');
You can also set the namespace in your App\Providers\RouteServiceProvider:
public function boot()
{
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace('App\Http\Controllers\Api')
->group(base_path('routes/api.php'));
});
}

Route resource not working in Laravel 8.x

I have a problem with the Route::resource() method in Laravel 8.x.
The error it returns is:
Target class [Admin\App\Http\Controllers\Admin\ProfileController] does
not exist.
Here is my code in routes/web.php:
Route::prefix('admin')->namespace('Admin')->group(static function() {
Route::middleware('auth')->group(static function () {
//...
Route::resource('profile', ProfileController::class);
});
});
I could not find where the problem is.
Finally, I found out the answer in laravel 8.x upgarade guide. I have texted the controller name with full namespace, instead of importing it.
Route::prefix('admin')->namespace('Admin')->group(static function() {
Route::middleware('auth')->group(static function () {
//...
Route::resource('profile', '\App\Http\Controllers\Admin\ProfileController');
});
});
Run following step for clear route cache
php artisan route:clear
Route::resource('invoice','\App\Http\Controllers\InvoiceController');
Make sure you followed the upgrade guide. There have been quite a few things that changed from v7 to v8.
To App/Providers/RouteServiceProvider.php add $namespace
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';
}
You also find more answers here: https://stackoverflow.com/a/63808132/799176
I also faced same issue with Laravel 7 latest version. See how I solved it:
First include this directory on the page enter image description here
use \App\Http\Controllers\Admin\ProfileController
Then call the full version of the route including the className like this
Route::resource('profile', '\App\Http\Controllers\Admin\ProfileController');
This will automatically create different routes for all methods defined in the ProfileController class. See an example in the image attached using TodoController.
Running php artisan route:list , I was having the same problem.
Target class [App\Http\Controllers\App\Http\Controllers\CourseController] does not exist.
In my case , what worked was:
Changing the resource from : Route::resource('courses', CourseController::class); to Route::resource('courses', 'CourseController');
I didn't changed any namespace and I'm using laravel 7.2.
So the reason this is an issue is because Laravel 8 removed the default namespace on the RouteServiceProvider.
If you want to use the ProfileController::class functionality you need to reset the protected $namespace to be null in the RouteProvider to match the base code.
Otherwise it will prepend the $namespace to whichever class you pass.
so change
protected $namespace = null
Then remove the ->namespace('Admin') from your routes.php file
In Laravel 8, First, you have to import the controller like,
use App\Http\Controllers\ProfileController;
And then use the resource,
Route::resource('profile', 'App\Http\Controllers\ProfileController');
if you used it in class,
Route::resource('profile',[ProfileController::class, 'classname']);
Please use below code, it seem that you are using class without importing so first you need to import profilecontroller in web.php file.
use App\Http\Controllers\Admin\ProfileController; ## insert path profilecontroller
Route::prefix('admin')->namespace('Admin')->group(static function() {
Route::middleware('auth')->group(static function () {
//...
Route::resource('profile', ProfileController::class);
});
});

Categories