I have laravel 8 project and I am facing issue with controller which is in subfolder.
I have DashboardController located in /app/Http/Controllers/Dashboard. In my web.php I have:
use App\Http\Controllers\Dashboard\DashboardController;
Route::get('dashboard', [DashboardController::class, 'dashboardView']);
DashboardController has this namespace:
namespace App\Http\Controllers\Dashboard;
I have tried uncomment $namespace variable in RouteServiceProvider.php. Also I have added:
->namespace($this->namespace);
in boot() method. But with no luck. I got this error:
Class 'App\Http\Controllers\Dashboard\Controller' not found"
When I have DashboardController in laravel controller folder, everything works well. Also interesting is LoginController. It is in Auth subfolder (Controllers/Auth) and this controller works from subfolder.
Reason why I want to move controller into subfolder(s) is better organisation of files.
Is anybody here, who can help me figure out this issue? Thank you very much in advance.
In that class file you are referencing Controller; most likely extends Controller on the class definition. There is no class named Controller in that namespace you have declared, App\Http\Controllers\Dashboard. You are most likely trying to reference App\Http\Controllers\Controller which means you will need a use statement for that or referencing it by its FQCN, Fully Qualified Class Name.
Related
I'm new to learning Laravel but I'm having trouble routing to controller, I have a controller named "App" and I have a function named index in it, it says it can't find it in "App" controller even though I set it in the route
Error
Error
Call to undefined method Illuminate\Support\Facades\App::index()
http://localhost:8000/anasayfa
App.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class App extends Controller
{
public function index(){
return "anasayfa";
}
}
web.php
Route::get('/anasayfa', 'App#index');
What is the reason for this error?
A class with the name App already exists in Laravel, defined in namespace Illuminate\Support\Facades\App
if you want to use your class make sure to add
use App\Http\Controllers\App
in your web.php
It is recommended to use a different name. You should follow the conventions and name it AppController.
I'm solved.
I deleted the controller named "App" and created a controller named "AppController". But this caused a new error, Laravel could not find class "AppController". For this I updated web.php as follows;
use App\Http\Controllers\AppController;
Route::get('/anasayfa', [AppController::class, 'index']);
Hi there I am having trouble using a model within a class there error being shown is Error
Class 'App\Models\RegisteredUsers' not found.
I have made sure that the namespaces match what is being used but I repeatedly get the same error.
Model code
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class RegisteredUsers extends Model
{
//
}
Controller code
<?php
namespace App\Http\Controllers;
use App\Models\RegisteredUsers;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class RegisterUser extends Controller
{
$UserObj = new RegisteredUsers();
}
The directory
App/Http/Controllers/RegisterUser - controller
App/Http/Models/RegisteredUser - model
I created the model and the controller using the command line with PHP artisan.
I have tried the solutions from
laravel model class not found
as well as Model not found in Laravel
and a few laracast questions but i still get the error.
May need to rebuild the classmap
composer dump-autoload
With App\Models namespace in your RegsiteredUser model, the RegisteredUser.php model file must be in the app/Models/RegisteredUser.php directory. Try to move the Models folder outside the Http folder. And from now, you should never put the Models folder in the Http folder again.
Your error is App/Http/Models/RegisteredUser and use App\Models\RegisteredUsers;, did you place your Models in Http?. that is not correct, the Models should be in App root directory, so you are calling the RegisteredUser in the wrong place
i recive this error
"\Controllers\ProductController does not exist "
but indeed i have this controller in my app/http/controller
class ProductController extends AdminController {
public function index() {
$products=Product::latest()->paginate(20);
return view('admin.product.index',compact('products'));
}
}
.....
Route::get('/admin/product','Productcontroller#index');
maybe check your namespace and see if changing that directory suited with the error will help.
namespace App\Http\Controllers\admin
Directs to the Admin Folder in the Controllers Folder
use App\Http\Controllers\Controller
Uses the main Controller.php file
After all this you can check your routes if they work at this point.
If you want to extend another controller instead of the main controller like in your case, I think you should check the directory of the AdminController.php and use the Use command again if the directory isn't in your namespace directory.
namespace App\Http\Controllers
use App\Http\Controllers\admin\AdminController
Atleast that's where i think the extent of your problem is.
This line:
Route::get('/admin/product','Productcontroller#index');
should probably be (watch the case):
Route::get('/admin/product','ProductController#index');
Also make sure that you import the right namespace: Is your controller really living in App\Http\Controllers namespace ?
I am trying to create an API directory in my Laravel project and I'm receiving the following error...
Class 'App\Http\Controllers\Controller' not found
I have tried using use App\Http\Controllers\Controller; on top of my API controllers but no luck.
My API classes are defined like...
class SyncController extends Controller {...}
class TimecardController extends Controller {...}
I'm pretty sure the error is coming from the extends Controller portion.
in App\Http\Controllers\Controller I have Controller.php. One way I have pushed past this is to duplicate the Controller.php into App\Http\Controllers\Api\v2\ and change the namespace of that controller to match where it is located (namespace App\Http\Controllers\Api\v2;)
I don't believe this is correct, as there should be a way to reference the Controller.php from the controllers in the API subdirectory.
../Controllers/Controller.php and API is a subdirectory, ../Controllers/Api/v2/SyncController.php
Any help would be much appreciated.
Thanks
-----------Edit------------
my routes for the api look like so
Route::group(['prefix' => 'api/v2'], function () {
Route::get('sync', 'Api\v2\SyncController#sync')->middleware('auth:api');
Route::post('timecard', 'Api\v2\TimecardController#create')->middleware('auth:api');
});
The Controller class cannot be found because the API controllers are not in the default Laravel controller directory. You need to add the controller class as a use statement. Then the autoloader will be able to find it.
namespace App\Http\Controllers\Api\v2;
use App\Http\Controllers\Controller;
class SyncController extends Controller {...}
And while your at it you might also want to add the auth:api middleware to the entire group. Much safer and efficient.
Route::group(['prefix' => 'api/v2', 'middleware' => 'auth:api', 'namespace' => 'Api\v2'], function () {
Route::get('sync', 'SyncController#sync');
Route::post('timecard', 'TimecardController#create');
});
I'm trying to use a controller inside a folder in the controllers directory like
route
Route::get('/','site\HomeController#index');
but seem's does not work like it gives me this error
Class App\Http\Controllers\site\HomeController does not exist
Note: I have also a HomeController.php in the controllers folder. I'm trying to organize my controllers by putting them unto their specific folders.
Any help, ideas please?
You should use proper namespace, like:
namespace App\Http\Controllers\Site;
And add this line:
use App\Http\Controllers\Controller;
Then this route will work:
Route::get('/','Site\HomeController#index');
The namespace of the class HomeController should be as:
namespace App\Http\Controllers\Site;
And in your route file you can use it as:
Route::get('/','Site\HomeController#index');
Remember to add following line of code in HomeController class as:
use App\Http\Controllers\Controller;