use a controller inside a folder in the controllers folder - php

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;

Related

Laravel 8 issue with controller inside subfolder

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.

how to fix 'Class App\Http\Controllers\Productcontroller does not exist'

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 ?

Class Not Found - Laravel 5.1

I have some issue with my controller. After I move the controller to sub folder it always tell me
Class 'Horsefly\Request' not found
I don't know what it is the problem. I did the same in admin sub folder and it worked fine.
Controller:
<?php
namespace Horsefly\Http\Controllers\includes;
use Illuminate\Http\Request;
use Horsefly\Http\Requests;
use Horsefly\Ebooks;
use Horsefly\Settings;
use Horsefly\Http\Controllers\Controller;
class EbooksController extends Controller {
}
Routes
get('/navpage', 'includes\EbooksController#navpage');
get('/content', 'includes\EbooksController#content');
get('/openModel', 'includes\EbooksController#openModel');
thanks for the help.
You declared use Horsefly\Http\Requests;
Just change it to
use Horsefly\Requests;
or use Request;

Laravel - Class/Model not found

Initially, this code worked, but it doesn't work anymore. I don't know what is causing the issue.
The error is :
FatalErrorException in AdminController.php line 64:
Class 'App\Category' not found
AdminController code is:
<?php
namespace App\Http\Controllers;
use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Category;
use View;
class AdminController extends Controller
{
public function article_get()
{
$categories = Category::latest()->get();
return View::make('create.article')->with('categories',$categories);
}
}
My model Category is located at App/Models/Category.php.
What I've tried:
Change from use App\Category; to use Category, to use \Category, to use App\Models\Category.
composer dump-autoload.
A few hours ago I had a working project, but now my models are not found.
Because Laravel uses PSR-4 autoloading, you need to make sure your namespaces match the real directory structure.
You say that your Category model is located at App/Models/Category.php so its namespace should be App\Models. Then, in your controllers you would use App\Models\Category.
I had also faced the same error when Models are called inside Controllers or Seeders.
The best solution is to import your Model inside the Controllers.
Add the below line at the top of your AdminController.
use App\Models\Category
This is applicable for all Classes where you try to call your models.
Do it like this and it must work:
use App\Models\Category
and make sure that the model Category is inside a folder named Models.
Your code seems fine.
May be it has an issue with namespace in Category model.
if you use this code for Category Model in your AdminController controller:
use App\Models\Category;
then your Category model itself has this namespace
namespace App\Models;
Check if your model namespace is still there or not.
Thanks
App\Models\Category
PLease Check This twoThings
1.Importing namespace App\Http\Controllers;
2,Check Controller Letter is in Upper Or LowerCase
like this
use App\models\Category----->❌❌❌
use App\Models\Category----->✅✅✅
use App\Models\Category
//check your models is capital or small letter sometimes issue like that is a big problem

phpcs - class must be in a namespace of at least one level - how to fix?

I am using laravel 4.2.
Lets say there is such class:
class BShopsController extends ShopsController
To fix this, I try to use name space lets say this:
namespace app\controllers;
and then it does not find ShopsController
so I add
use \ShopsController;
Then I get error:
Class BShopsController does not exist
What namespace should I use first of all so it would not break anything?
Edit:
BShopsController and ShopsController are in folder Shops
As your files are inside the Shops folder and I believe that the Shops folder is inside the app folder you should namespace your class the following way.
<?php namespace Shops;
class BShopsController extends ShopsController{}
Similarly,
<?php namespace Shops;
class ShopsController{}
So with the help of Shhetri and this Using namespaces in Laravel 4
I did this way:
namespace App\Controllers\Shops;
class BShopsController extends ShopsController{}
Also in routes.php then need to change to this:
Route::controller('shops', 'App\Controllers\Shops\ShopsController');
And where calling action() method - also need to use namespace.
Also needed to run
composer dump-autoload -o
otherwise were errors.
Also in ShopsContrller needed to to this:
use \App\Controllers\BaseController;
Because Shops controller was in another namespace than BaseController and cannot find it. But is extending from BaseController, so need it.

Categories