I'm quite new to Laravel and when I am going through a tutorial when I encountered this error. This is my code in 'testController.php'.
<?php
namespace app\Http\Controllers;
use app\Http\Controllers\Controller;
class testController extends \app\Http\Controllers\Controller {
public function getAbout()
{
return view('Learning.about') ;
}
public function getHome()
{
return view('Learning.index');
}
}
And this is my 'routes.php'.
<?php
Route::get('test', [
'as' => 'test',
'uses' => 'testController#getHome',
]);
Route::get('about', [
'as' => 'about',
'uses' => 'testController#getAbout',
]);
I am getting this error:
Class 'app\Http\Controllers\Controller' not found
How can I fix this error?
Let's go through this step by step.
1. Check autoload directive on composer.json
Open composer.json file on your project root directory. Locate the the autoload section. It should be looking like this:
{
...
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
}
Make sure you have this configuration under the psr-4 option:
"App\\": "app/"
This configuration tells the composer that we want to autoload all classes inside the app directory using psr-4 convention and places it under the App namespace.
2. Update your controller
First, your controller file name should be in CamelCase style. So we have to renamed it to TestController.php. Make sure that it's saved under app/Http/Controllers directory.
Now open your TestController.php file, we have to capitalize the namespace and class name like so:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class TestController extends Controller {
public function getAbout()
{
return view('Learning.about') ;
}
public function getHome()
{
return view('Learning.index');
}
}
Note that we also turn this line:
class testController extends \app\Http\Controllers\Controller
Into:
class TestController extends Controller
Since we already import the base Controller class, we don't have to specify the fully qualified name. We imported the Controller class using the use keyword:
use App\Http\Controllers\Controller;
Save your TestController.php file.
3. Update your routes file
Now we have to update our app\Http\routes.php file. We just need to capitalize the controller name:
<?php
Route::get('test', ['uses' => 'TestController#getHome', 'as' => 'test']);
Route::get('about', ['uses' => 'TestController#getAbout', 'as' => 'about']);
4 Update your autoloader
Now the last thing to do. Open your terminal / command prompt. Go to your project directory and run the following command:
composer dump-autoload
This command will update the autoloader file (Read more here).
Now if you open up your browser and hit /test route, you should see the content from resources/views/Learning/index.blade.
Use correct namespace:
namespace App\Http\Controllers;
// Remove: use app\Http\Controllers\Controller;
class testController extends Controller {
According to my experience in Laravel projects, the namespaces starts with the capital A of App used in namespace, you should try to change your code to this:
namespace App\Http\Controllers;
class testController extends Controller { }
Also check if the controller - App\Http\Controllers\Controller lies in the same namespace as mentioned in your code.
Include this at the top of your Controller file. This fixed it for me.
namespace App\Http\Controllers;
In some cases the problem is that the framework is not able to instantiate your given controller class. This can happen for example if you are using a sub-folder under Controllers and that when you are extending the Controller.php class, you did not provide the use statement to that definition*. Other run-time errors may also cause this.
*Which is now required since your own controller is not at the root of the Controller folder anymore.
Related
I am using Laravel 5.5. I have added a custom directory inside App folder in my workspace. So, the folder structure is:
Inside App\Bishwa\Transformers there are two PHP files:
Transformer.php
LessonTransformer.php
Those files look like follows:
Transformer.php
<?php
namespace Bishwa;
abstract class Transformer {
public function transformCollection(array $items){
return array_map([$this, 'transform'], $items);
}
public abstract function transform($item);
}
LessonTransformer.php
<?php
namespace Bishwa;
class LessonTransformer extends Transformer {
public function transform($lesson){
return [
'title' => $lesson['title'],
'body' => $lesson['body'],
'active' => (boolean)$lesson['some_bool']
];
}
}
Then Inside LessonsController.php I have the following:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use App\Lesson;
use Bishwa\LessonTransformer;
class LessonsController extends Controller
{
protected $lessonTransformer;
function __construct(LessonTransformer $lessonTransformer){
dd('ok');
}
While running action of the controller, It gave me an error message saying:
Reflection Exception: Class Bishwa\LessonTransformer does not exist
I have tried composer dump-autoload, restarting the server again but none of them helped. Am I doing wrong while Namespacing or What?
Change your namespace of the files in your custom directory to App\Bishwa.
Well thanks to Jerodev and Jack. Since, both of them were write I decided to write myself a combined solution to this problem.
1st Solution:
In case of custom namespaces and custom classes I have to include the path to classname in Composer.json file in the following portion:
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/Bishwa/Transformers"
],
"psr-4": {
"App\\": "app/"
}
2nd Solution:
Changing NameSpace of my files to custom directory App\Bishwa .
Namespace of transformer.php and LessonTransformer.php now becomes:
namespace App\Bishwa\Transformers;
While using in LessonsController:
use App\Bishwa\Transformers\LessonTransformer;
Once again, big thanks to Jerodev and Jack. Its my silly mistake, that I couldn't figure that out.
I created a Panel directory inside Controller directory .
there is a login function inside AdminController.php
class AdminController extends Controller
{
//
public function login()
{
return 'test';
}
}
in routes.php I wrote a route like this:
Route::get('/cp/login','Panel\AdminController#login');
but when I run below url I got some errors that there isn't exist this controller :
http://localhost:8000/cp/login
ReflectionException in Route.php line 280: Class
App\Http\Controllers\Panel\AdminController does not exist
Try adding the appropriate namespace to the top of the AdminController file, you will also need to specify the namespace for the Controller class that it extends, as they are under different sub-namespaces.
You can read more about PSR-4 autoloading here http://www.php-fig.org/psr/psr-4/.
Based on the directory structure that you have there it should read
<?php
namespace App\Http\Controllers\Panel
use App\Http\Controllers\Controller;
class AdminController extends Controller {
//..
}
you should add the namespace to the contorller
Change you namespace to
namespace App\Http\Controllers\Panel;
Laravel will resolve controllers based on your name spacing, not on your directory structure.
I've created a custom new file, app/Http/Helpers.php and added:
<?php
namespace app\Http;
class ConnectionsHelper {
public static function organisation($id) {
return 'ID:'.$id;
}
}
In Composer.json, in the autoload-block I've added:
"files": [
"app/Http/Helpers.php"
]
And then I ran "composer dump-autoload".
My controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class myController extends Controller
{
public function index()
{
echo ConnectionsHelper::organisation(2);
}
}
And get in return:
FatalErrorException in OrganisationsController.php:
Class 'App\Http\Controllers\ConnectionsHelper' not found
You need to provide a namespace alias in your controller.
use App\Http\ConnectionsHelper
Autoloading a file does not mean that the classes in that file are required/included in all other scripts in the app. It just means that you are making those files available to your app. In this case, your helper file is already inside the App namespace which is autoloaded by default, so you can remove the files bit of your composer.json completely.
I have create Helpers folder inside app, then I have created php file amchelpers.php ---> app/Helpers/amchelpers.php
amchelpers.php code:
<?php namespace App;
class AmcHelper {
static function displayString($string){
return $string;
}
}
then added these lines to composer.json
"files": [
"app/Helpers/amchelpers.php"
]
then run this command:
composer dump-autoload
then added 'Helper' => app_path() . '\Helpers\AmcHelper' to aliases array in config/app.php file.
in my controller I have below action (this action defined in route.php):
use Helper;
class UserController extends Controller {
public function displayMyString(){
echo Helper::displayString('Hello');
}
}
when run the page http://localhost:8080/easy_marketing/public/displayMyString
I Got:
ErrorException in compiled.php line 6367: Class 'C:\wamp\www\easy_marketing\app\Helpers\AmcHelper' not found
you have written user Helper instead of use Helper
or
another way to achieve this is
Laravel 5 App directory is autoloaded by default with its folder, what you have to take care is add namespace followed by directory name,
so directory structure is App --> Helpers
so your name space must include App\Helpers
try following code
<?php namespace App\Helpers;
class AmcHelper {
static function displayString($string){
return $string;
}
}
and when you are using this class in another class write this after namespace declaration
use App\Helpers\AmcHelper as Helper;
class UserController extends Controller {
public function displayMyString(){
echo Helper::displayString('Hello');
}
}
Is it possible to call a control that is nested within a sub folder in Laravel 4?
My Controllers are as follows
- Controllers
- admin
* AdminController.php
* HomeController.php
* BaseController.php
* ArticleController.php
Below is the code from my AdminController class:
<?php
class LoginController extends BaseController {
public function showLogin()
{
return View::make('partials.admin.login');
}
}
In my Routes.php file I am doing the following:
Route::get('/admin', 'admin.LoginController#showLogin');
But I'm getting a Class not found error. Is there anything I'm missing as I can't seem to find out how to solve this problem from the Laravel 4 documentation.
As long as you don't change the namespace of the controller you should be able to access it from the global namespace even if it is in a subfolder.
So just change:
Route::get('/admin', 'admin.LoginController#showLogin');
to:
Route::get('/admin', 'LoginController#showLogin');
The filename also needs to match the class name so change 'AdminController.php' to 'LoginController.php' or change the class name from 'LoginController' to 'AdminController'.
And make sure you do composer dump-autoload
You just need to add namespace in your AdminController.php file and change name of the class from LoginController to AdminController
AdminController.php will then be:
<?php
namespace Admin;
use BaseController;
class LoginController extends BaseController {
public function showLogin()
{
return View::make('partials.admin.login');
}
}
and change your routes.php to :
Route::get('/admin', 'admin\LoginController#showLogin');
I experienced a problem when I stored my admin controller in a subdirectory of the controllers directory app/controllers/admin
I had to add this directory to the list of autoload classmaps in my composer.json file
Then run composer dump-autoload
Adding a trailing slash to "app/controllers" in composer.json worked for me:
"autoload": {
"classmap": [
"app/commands",
"app/controllers/",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
Then run composer dump-autoload
may be its too late but one of the possible ways is to use namespaces.
here is my sample:
routes.php :
Route::group(array('prefix' => 'admin' , 'before' => 'admin' ), function()
{
Route::controller('contacts' , '\backend\ContactController');
...
}
and on top of your backend controllers add add these lines :
namespace backend;
use \view as view;
and also add these lines to your composers.json in classmap directive :
"app/controllers/backend",
"app/controllers/front",