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

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.

Related

Importing Laravel Avored Package Controller in Laravel Controller

I am trying to extend the package controller in my base laravel controller. Tried importing class using below code which shows error as class not found.
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Controllers\ProductController as ControllersProductController;
use App\Imports\ProductsImport;
use AvoRed\Framework\AvoRedProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;
use Avored\Framework\Catalog\Controllers\ProductController;
class ProductControllers extends Controller
{
private $avored_product;
public function __construct(ProductController $p) {
$this->avored_product = $p;
}
public function index() {
echo $this->avored_product;
}
Tried multiple options by researching on it couldn't find the same. Request all to please guide me with same.
Could you share with us the exact error message? From your code snippet I can't see which class couldn't be found.
And which Avored package are you referring to? I guess avored-laravel-ecommerce?
If you want to extend the ProductController-from the package, you have properly extend from that controller.
<?php
namespace App\Http\Controllers\Admin;
use Avored\Framework\Catalog\Controllers\ProductController as AvoredProductController;
class ProductControllers extends AvoredProductController
{
public function index() {
// Do your thing in here
}
}
You can now override the controller methods to your liking.

Fatal Error Class 'App\Http\Controllers\Controller' not found Laravel 5.4

I tried to route with using name .
I already tried to solve with using Composer update , Clear Cache , Generate App key.
But it's showing fatal error .
Error View Message :
(1/1) FatalErrorException
Class 'App\Http\Controllers\Controller' not found
in ClassesController.php line 13
My Route Code Below :
<?php
Route::prefix('Classes')->group(function(){
Route::get('/add','ClassesController#index')->name('AddNewClass');
});
My Controller Code Below :
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ClassesController extends Controller{
public function index()
{
return "Method Access";
}
In this event, I would first ensure that the file containing the class Controller exists in the same directory with the same namespace as the ClassesController, as implied in your code.
If the Controller does not exist, you can either create the class or simply remove the extends Controller and the line use App\Http\Controllers\Controller; from your code.
its worked for me.
usercontroller
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* Show the profile for the given user.
*/
/* http://localhost:8080/laravelapps/blog/public/staff */
public function showProfile()
{
return 'new';
}
}
web.php
Route::get('/staff', 'UserController#showProfile');
http://localhost:8080/laravelapps/blog/public/staff
Check whether Controller.php file exists or not in your Controllers folder if not exists download or put from laravel project.
After that run following command php artisan app:name newproject
Make sure in the above line i have updated text 'newproject' that should be first line of your Controller.php file. For example: namespace newproject\Http\Controllers;

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.

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.

How do I call a model in Laravel 5?

I'm trying to get the hang of Laravel 5 and have a question which is probably very simple to solve but I'm struggling.
I have a controller named TestController and it resides in \app\Http\Controllers
This is the controller:
<?php
namespace App\Http\Controllers;
class TestController extends Controller {
public function test()
{
$diamonds = diamonds::find(1);
var_dump($diamonds);
}
}
Then I have a model I created that resides in /app:
<?php
namespace App;
class diamonds extends Model {
}
Put all other mistakes aside which I'm sure there are, my problem is that laravel throws an error:
FatalErrorException in TestController.php line 10: Class
'App\Http\Controllers\diamonds' not found
So, how do I get the controller to understand I'm pointing to a model and not to a controller?
Thanks in advance...
You have to import your model in the Controller by using namespaces.
E.g.
use App\Customer;
class DashboardController extends Controller {
public function index() {
$customers = Customer::all();
return view('my/customer/template')->with('customers', $customers);
}
}
In your case, you could use the model directly App\diamonds::find(1); or import it first with use App\diamonds; and use it like you already did.
Further, it is recommended to use UpperCamelCase class names. So Diamonds instead of diamonds. You also could use dd() (dump and die) instead of var_dump to see a nicely formatted dump of your variable.
//Your model file
<?php
namespace App\Models;
class diamonds extends Model {
}
// And in your controller
<?php
namespace App\Http\Controllers;
use App\Models\
class TestController extends Controller {
public function test()
{
$diamonds = diamonds::find(1);
var_dump($diamonds);
}
}
Try adding the following lines above your class decleration in your controller file...
use App\Diamonds;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
(this assumes your model file is called Diamonds.php)
Ultimately, there were a few issues here and all of you helped, however I was missing the following on the model page:
use Illuminate\Database\Eloquent\Model;

Categories