Class App\Http\Controllers\CategoriesController does not exist - laravel

hi m trying to get view of page but it says: Class App\Http\Controllers\CategoriesController does not exist
pic of folder structure https://ibb.co/gMBvwDJ
Route:
Route::match(['get','post'],'/admin/categories/index','CategoriesController#Category');
controller:
public function index()
{
return view('admin.categories.index');
}

First of all you use a wrong definition of the route match, so try this:
Route::match(['get','post'], 'CategoriesController#index');
and second, make sure that in your CategoriesController you use the right namespace, which should be:
namespace App\Http\Controllers;
at the very top of the class.

It gives you that error because the controller couldn't find the class you are calling so in order to fix the problem, in the top of your controller add
use App{ModalName};
for example if your model for this is called categories,
use App\categories;
as well as add
namespace App\Http\Controllers;

Please make sure your controller is placed in below directory structure or else you have to fix the namespace problem

Related

Class "App\Http\Controllers\settingsController\Settings" not found

web.php
<?php
use App\Http\Controllers\settingsController\seoController;
use App\Http\Controllers\settingsController\contactController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\generalController\generalRoutes;
use App\Http\Controllers\settingsController\usersController;
Route::controller(generalRoutes::class)->group(function () {
Route::get('/', 'index')->name('index');
Route::get('/getNews', 'getNews');
});
Route::prefix('settings')->group(function () {
Route::controller(contactController::class)->group(function () {
Route::get('/contact', 'index')->name('contactIndex');
Route::post('/contact/update/general', 'generalContactUpdate')->name('generalSContactUpdate');
Route::post('/contact/update/html', 'staticHtmlUpdate')->name('staticHtmlUpdate');
});
});
contactController
<?php
namespace App\Http\Controllers\settingsController;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class contactController extends Controller
{
public function index(){
$data = Settings::find(1);
return view('settings.contact',compact('data'));
}
I wanted create contactController, I had this problem before
Class "App\Http\Controllers\settingsController\contactController" not found
enter image description here
I solve it but now it gives settingsController error
Class "App\Http\Controllers\settingsController\Settings" not found
Please help me))
I want create contactController but it gives settingsController problem
Add this on top of your web.php
Use App\Http\Controllers\settingsController\contactController.php;
Hey Just for clearing your doubts giving you some hint for this basic question.
If you want to use controller you can use it with the help of namespaces like below:
namespace App\Http\Controllers;
And if you want to fix this error Class "App\Http\Controllers\settingsController\Settings" not found
First, you need to specify the modal from where you want the data like this:
use App\Models\Settings;
After that If you want to retrieve data form that modal you can simply do this:
$data = Settings::find($id);
Hope it will help you for basic understanding the usage of controllers and modals in laravel. If you are still struggling with the issue below doc link will surely help you. Keep Learning.
https://laravel.com/docs/9.x/controllers#basic-controllers

laravel 8 Target class [ColorController] does not exist

I created ColorController Class using
php artisan make:controller ColorController
The class has been created successfully and exists in the App/HTTP/Controller folder.
I defined route like this
Route::get('color/text', [ColorController::class, 'text']);
And a Method in the Contoller Class like this
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ColorController extends Controller
{
public function text()
{
return 'Color Controller';
}
}
I visited the like this
http://localhost:8000/color/text
It shows the error message as below
Illuminate\Contracts\Container\BindingResolutionException
Target class [ColorController] does not exist.
http://localhost:8000/color/text
Where did I make the fault? where to correct the process?
I have found the Solution like this...
I had to add this line above in the web.php file
use App\HTTP\Controllers\ColorController;
Now all works fine.

How to work with subdirectory controllers in CodeIgniter 4?

I need help with using sub directory controllers in CodeIgniter 4.
I just can't make it work for some reason.
This is the URL for example: www.example.com/admin/dashboard
In the controllers folder, I created a folder named Admin, and a file named Dashboard.php.
I used this code in Dashboard.php:
namespace App\Controllers;
class Dashboard extends BaseController
{
public function index()
{
}
}
I tried changing the class name to AdminDashboard, Admin_Dashboard, pretty much every logical name but every time I get a 404 error, saying:
Controller or its method is not found:
App\Controllers\Admin\Dashboard::index
I know the file itself gets loaded successfully, but I think I don't declare the classname correctly and it keeps throwing me those 404 errors.
The documentation of CI4 isn't providing any information about what the classname should be called unfortunately...
UPDATE #1
I managed to make it work by changing few things:
namespace App\Controllers\Admin;
use CodeIgniter\Controller;
class Dashboard extends Controller
{
public function index()
{
}
}
But now it won't extend the BaseController which has some core functions that I built for my app.
Any ideas to how to make it extend BaseController?
I must admit that I don't have much knowledge about namespacing yet, so that might be the reason for my mistakes.
As I imagined, the problem was that I didn't learn about namespacing.
I needed to point the use line at the BaseController location.
namespace App\Controllers\Admin;
use App\Controllers\BaseController;
class Dashboard extends BaseController
{
public function index()
{
}
}
Now www.example.com/admin/dashboard/ goes directly to that index function, as intended.
php spark make:controller /Subfolder/ControllerName
$routes->add('/(.+?)_(.+?)/(.+?)$', 'subdir\\\\$1_$2::$3');
$routes->add('/(.+?)_(.+?)$', 'subdir\\\\$1_$2::index');
I was able to map with this setting.
The route mapping could be as simple as:
$routes->group('admin', static function ($routes) {
$routes->get('dashboard', 'Admin\Dashboard::index');
});

How import custom class in controller Laravel?

I have created a new directory Library in root of Laravel.
Inside I put the file with class:
class My {
//
}
So, in controller Laravel I try to get access to this class:
App\Library\My
But Laravel does not determine this path.
This is my code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use View;
use App\Library\My;
class HomeController extends Controller
{
//
}
A complete and functional example based on the posts here:
1 - Folder and file - Create a folder under app/, in this example we will create a folder called Library.
We will also inside the folder create a file with the name of your class, here we will create a class called My.
So we will have app/Library/My.php
2 - Class and method - Now, for testing, inside the class create a static method called myMethod
<?php
namespace App\Library;
class My
{
public static function myMethod()
{
return 'it\'s work!';
}
}
3 - Controller - Now at the beginning of the Controller, we will declare the namespace of your Class with use:
<?php
namespace App\Http\Controllers;
use App\Library\My;
//rest of the controller code
Finally, to create an instance of the My class, in Controller, a new statement must be used:
//rest of the controller code
public function index()
{
$whatever = new My;
return $whatever::myMethod();
}
As above, make sure it is placed in the App directory and make sure it is properly namespaced e.g.
<?php
$fOne = new \App\library\functions;
$isOk = ($fOne->isOk());
?>
You should create Library folder inside app folder
namespace App\Library\My
app folder is alrdy used psr-4
In your controller
use App\Library\My as My
It's work for me. Hope this answer is helpful
You have to properly namespace your every class.
So you can import your class with use keyword, like so
use App\Library\My;
....
$my = new My();
Or if you've conflicting class name then you can use as keyword to alias the classname while importing
use App\Library\My as MySecond;
....
$my = new MySecond();
And if you want to directly access your class within the method then you can access it like so.
$my = new \App\Library\My();
Note: The leading \ means App was declared in the global scope.

Same name for Laravel controller and model?

I had a model Entry and had no problem with it.
After I created a new Controller with same name, the model stopped working.
When I execute Enrty::where(... a Fatal error occurs:
Call to undefined method entry::where()
Define Controller
<?php namespace Controllers;
class Entry extends \BaseController {}
Define Model
<?php namespace Models;
class Entry extends \Eloquent {}
Now, you can use these classes if you import/alias them
use Controllers\Entry as EntryController;
use Models\Entry as EntryModel;
EntryController::method();
EntryModel::anotherMethod();
Note: this code serves to show the principle.
without changing/renaming namespaces
simply use as keyword
use Models\Car as CarModel;

Resources