Laravel 5 Namespace issue fatal error exception - php

Im having this fatal error exception that i cant seem to figure out:
Class 'App\Http\Controllers\Admin\Controller' not found
For some reason im not sure why it is tacking controller at the end of that error. My namespace for the controller:
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Announcement;
use App\Http\Requests;
class AnnouncementController extends Controller
{
...
}
and my routes:
Route::group(['prefix' => 'admin','namespace'=>'Admin', 'middleware'=>'auth'], function () {
Route::resource('announcements','AnnouncementController');
});
But when i navigate to the /admin/announcements route i get that fatal exception with the Controller tacked on at the end..
This controller is in the App\Http\Controllers\Admin directory so i m not sure why i m getting this error. Am i name spacing wrong?

Try this
Route
Route::group(['prefix' => 'admin', 'middleware'=>'auth'], function () {
Route::resource('announcements','Admin\\AnnouncementController');
});
Controller
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Announcement;
use App\Http\Requests;
class AnnouncementController extends Controller
{
...
}
if this dosent work, check if you have a Controller called Controller in app/Http/Controllers/

Related

Target class does not exist. Routing Prefix Laravel 9

I am learning Laravel 9, and I got a problem Target class [Admin\DashboardController] does not exist. when using prefix routing. Is someone can help me to fix it?
this code on routes/web:
Route::prefix('admin')->namespace('Admin')->group(function(){
Route::get('/','DashboardController#index')->name('dashboard');
});
this code on App\Http\Controllers\Admin\DashboardController:
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index(Request $request){
return view('pages.admin.dashboard');
}
}
You've specified an incorrect namespace in your route. As per the error message:
Target class [Admin\DashboardController] does not exist
Laravel expects to find a DashboardController in the Admin namespace, however, you've defined your DashboardController with the namespace App\Http\Controllers\Admin.
Update the namespace on your route.
Route::prefix('admin')->namespace('App\Http\Controllers\Admin')->group(function(){
Route::get('/','DashboardController#index')->name('dashboard');
});
If you read the documentation, you will see that you must use another way:
Route::prefix('admin')->namespace('Admin')->group(function(){
Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
});

Model Class not found error

Using Laravel 5.4, I am getting this error on view whenever I call my method it shows on screen
(1/1) FatalThrowableError
Class 'App\Models\Chat\User' not found
Hierarchy of my project :
Controllers
- ChatMessageController
Models
-Chat
-message.php
-User.php
and here's my controller class code :
namespace App\Http\Controllers\Chat;
use App\Http\Controllers\Controller;
use App\Models\Chat\Message;
use App\Models\User;
use Illuminate\Http\Request;
class ChatMessageController extends Controller
{
public function index()
{
$messages = message::with(['user'])->latest()->limit(100)->get();
return response()->json($messages,200);
}
}
In message.php, probably you forgot to add: use App\Models\User;.
So, it is trying to find User in the wrong space.

Fatal error: Class 'App\Http\Controllers\Controller' not found

I am getting the error below even though my Controller class is present in my Controllers directory;
Fatal error: Class 'App\Http\Controllers\Controller' not found
Below is the content of my UserController controller (in the same directory, Controllers):
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
public function SignIn(Request $request)
{
if (Auth::attempt(['usernameEmail'=>$request['usernameEmail'],'password'=>$request['password']])) {
return redirect()->route('dashboard');
}
return redirect()->back();
}
public function getDashBoard()
{
return view('dashboard');
}
}
What am I doing wrong and how can I resolve it?
You get the Fatal error: Class 'App\Http\Controllers\Controller' not found
error because you have no such a class in your project at the derived location.
Instead of
use App\Http\Controllers\Controller; // this is irrelevant and causing the error you get
you should have
use Illuminate\Http\Request; // this is necessary for your SingIn method
as in:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
// your methods here
}
because one of your SignIn method does make use of it.
It you ran composer dump-autoload and it didn't fixed your problem you placed your controller in wrong folder. It should be inside app\Http\Controllers dir.
If it won't fix your issue, look at storage\logs\laravel.log where you can find more informations about the problem, and where your problem occured. Maybe you are defining routes using wrong classname or something like that.

Class App\Http\Controllers\ does not exist

This is my Route:
Route::get('/hello', '#HomeController#index');
This is my HomeController
namespace App\Http\Controllers;
use app\Requests;
use Illuminate\Http\Requests;
use Spatie\Activitylog\Models\Activity;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
class HomeController extends Controller {
public function index() {
$lastActivity = Spatie\Activitylog\Models\Activity::all();
return view('activity'), compact('lastActivity'));
}
}
But I keep on getting an error message:
ReflectionException in Route.php line 280:
Class App\Http\Controllers\ does not exist
What can I do? Thanks.
At the first of controller you do not need to put #, its just for method of controller.
Route::get('/hello', 'HomeController#index');
You have an extra # in your method call.
'#HomeController#index'
should be
'HomeController#index'
Whenever errors of type ReflectionException occur, you should check the routes in the routes/api.php and routes/web.php files to correct them ok understand do it carefully next time.

Laravel 5 registering a controller to map all methods

I am new to Laravel 5 coming from CodeIgniter background. I have habit to not play with routes.php. CodeIgniter automatically maps methods like controllerName/MethodName. But in Laravel 5 I am trying to do same by registering a controlller by writing this at top of app/http/sroutes.php:
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
When I run php artisan route:list it show that controller is registered. But when I see URL /public/admin/user/addRole it show addRole method not exist while I have created a method in AdminUserController.
Admin/AdminUserController.php
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getaddRole(){
echo "adding Roles";
}
}
Routes.php
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getAddRole(){
echo "adding Roles";
}
}
NB: Notice getAddRole() not getaddRole(), use camelCase
If your controller action contains multiple words, you may access the action using "dash" syntax in the URI like this:
public/admin/user/add-role
It's hard to tell because I don't see your controller code but I assume you missed adding a HTTP verb to the method name. Like:
public function getAddRole(){
// ...
}
If you want the method to match any request method, use any:
public function anyAddRole(){
// ...
}

Categories