I have the following error while trying to run my controller
Controller class not found
I have this code in my routes.php file
Route::get('cms/create-page', 'AdminCMSController#create_page');
Route::post('cms/createpage','AdminCMSController#createpage');
Route::controller('cms','AdminCMSController');
And this is the code in my Controller
class AdminCMSController extends BaseController {
public function create_page() {
}
public function createpage() {
}
}
How can I fix it?
If you didn't move the controllers directory from the original location (which is «project_root»/app/controllers/, you must guarantee that:
Laravel's autoload has the controller directory. Navigate to «project_root»/app/start/global.php. You need to have something like this:
(...)
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
));
(...)
Take notice to this line app_path().'/controllers'. It must exist.
Also, open your composer.json file and verify that the following lines exist:
(...)
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
(...)
Make sure that you have the line with app/controllers
If you have this lines and you still get the same message, go to your project root and run the following command from the command-line composer dumpautoload -o.
Laravel works with Composer, which is a dependency management tool for PHP. It also prepares an autoload file for all your project classes (see composer docs). When you run the composer dumpautoload command, it will create some files within «project_root»/vendor/composer.
Make sure that you can find the class AdminCMSController in the file «project_root»/vendor/composer/autoload_classmap.php. You should see something like this:
'AdminCMSController' => $baseDir . '/app/controllers/AdminCMSController.php',
If you have changed the default location of your controllers directory, you have to do either one of the following steps. However, since you are not defining a namespace in your class, it doesnt seem likely that this is your problem:
Use PSR-0 for autoloading classes. Imagine that you have the following folder structure:
/app
/commands
/config
/database
/Acme
/controllers
You have to specify the Acme folder in your composer.json, like this:
"autoload": {
"classmap": [
"app/commands",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-0": {
"Acme": "app/"
}
},
After this you need to update you composer autoload files with the command composer dumpautoload.
If you do not want to use the PSR-0 for autoloading, you need to change your routes file from this
Route::controller('cms','AdminCMSController');
to this:
Route::controller('cms','Acme\controllers\AdminCMSController');
IF you use PSR-0, you need to namespace your classes like this:
<?php namespace Acme\controllers;
class AdminCMSController extends BaseController {
(...)
}
Curious about the Acme reference? I was too. Refer to the wikipedia.
Related
I am struggling with PHP Namespaces and Auto loading in Laravel 5.4. I actually decided to put the models that I need to use in a definite and named folder as app/Models:
php artisan make:model Models/Customer
I have set Customer model's namespace to namespace Models;
Then, I created a route as follows:
Route::get('customer', function () {
$customer = \App\Models\Cutomer::find(1);
echo $customer;
});
To do auto loading work I opened the composer.json file located in the very root folder of the Laravel project and made the autoload to be as follows:
"autoload": {
"classmap": [
"database",
"app/Models"
],
"psr-4": {
"App\\": "app/",
"App\\Models\\": "App/Models"
}
},
After all of this I checked if my composer is an update version and ran dump-autoload:
composer self-update
composer dump-autoload -o
There is also worth of notice the contents of vendor/composer/autoload_classmap.php:
'App\\Models\\Customer' => $baseDir . '/app/Models/Customer.php',
'App\\Models\\Test\\Test' => $baseDir . '/app/Models/Test.php',
My problem is that whenever I execute the url: http://127.0.0.1:8000/customer I will encounter the following error output:
(1/1) FatalThrowableError
Class 'App\Models\Cutomer' not found
Would you please help me understand where of my work has been incorrect, and how to fix it? Thank you very much in advance.
The namespace in the model should be namespace App\Model; (hence why you call the class as \App\Models\Cutomer)
When you use php artisan make:model Models/Customer it should have set the namespace correctly then.
Also, you don't need to edit your composer.json to do this. Remove the additions you've made to your composer.json file for the autoloading and then run composer dumpautoload from the command line.
Hope this helps!
First, you have an error in your PSR-4 setup.
"App\\Models\\": "App/Models"
This does not map to a valid directory. Secondly, and more importantly, there is no need to autoload a nested namespace of one that is already declared. By autoloading App you will already autoload any nested namespaces as long as they conform to PSR-4 standards i.e namespace as directory name and filename as class name.
Composer setup only needs to be the following:
"psr-4": {
"App\\": "app/"
}
I have the following error while trying to run my controller
Controller class not found
I have this code in my routes.php file
Route::get('cms/create-page', 'AdminCMSController#create_page');
Route::post('cms/createpage','AdminCMSController#createpage');
Route::controller('cms','AdminCMSController');
And this is the code in my Controller
class AdminCMSController extends BaseController {
public function create_page() {
}
public function createpage() {
}
}
How can I fix it?
If you didn't move the controllers directory from the original location (which is «project_root»/app/controllers/, you must guarantee that:
Laravel's autoload has the controller directory. Navigate to «project_root»/app/start/global.php. You need to have something like this:
(...)
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
));
(...)
Take notice to this line app_path().'/controllers'. It must exist.
Also, open your composer.json file and verify that the following lines exist:
(...)
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
(...)
Make sure that you have the line with app/controllers
If you have this lines and you still get the same message, go to your project root and run the following command from the command-line composer dumpautoload -o.
Laravel works with Composer, which is a dependency management tool for PHP. It also prepares an autoload file for all your project classes (see composer docs). When you run the composer dumpautoload command, it will create some files within «project_root»/vendor/composer.
Make sure that you can find the class AdminCMSController in the file «project_root»/vendor/composer/autoload_classmap.php. You should see something like this:
'AdminCMSController' => $baseDir . '/app/controllers/AdminCMSController.php',
If you have changed the default location of your controllers directory, you have to do either one of the following steps. However, since you are not defining a namespace in your class, it doesnt seem likely that this is your problem:
Use PSR-0 for autoloading classes. Imagine that you have the following folder structure:
/app
/commands
/config
/database
/Acme
/controllers
You have to specify the Acme folder in your composer.json, like this:
"autoload": {
"classmap": [
"app/commands",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-0": {
"Acme": "app/"
}
},
After this you need to update you composer autoload files with the command composer dumpautoload.
If you do not want to use the PSR-0 for autoloading, you need to change your routes file from this
Route::controller('cms','AdminCMSController');
to this:
Route::controller('cms','Acme\controllers\AdminCMSController');
IF you use PSR-0, you need to namespace your classes like this:
<?php namespace Acme\controllers;
class AdminCMSController extends BaseController {
(...)
}
Curious about the Acme reference? I was too. Refer to the wikipedia.
in my Laravel app I have created Acme\fooDir directory inside app directory
i am trying to call a model -"barModel"- from within the fooDir
//app/Acme/fooDir/test.php
$barM = new barModel;
but i am getting this error
Class 'Acme\fooDir\barModel' not found
here is my app structure
app
-app/Acme
--app/Acme/fooDir
---app/Acme/fooDir/test.php (this is the file that could load the barModel)
-app/models
--app/model/barModel.php (this is the model i am trying to use)
I have added autoload in composer.json
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-0":
{
"Acme": "app/"
}
and
I have ran the command
composer dump-autoload -o
and
php artisan dump-autoload
but the problem not solved and i am still getting the same error
Class 'Acme\fooDir\barModel' not found
any idea?
When using namespace all your classes are called within that namespace.
If you want to use barModel in Acme/fooDir/test.php your will need to use use barModel; just after your namespace row.
<?php
namespace Acme\fooDir;
use barModel;
class test {
}
Two things.
1, Have you namespaced your classes ie,
<?php namespace Acme\FooDir;
class Test ...
2, Are you using use in your class
<?php namespace Acme\FooDir;
use \BarModel
class Test ...
?
Would this do the trick for you.
$barM = new \barModel;
PHP namespaces and importing
Lets say I have a class MyClass in app/repository/MyClass.php in the Repository namespace:
namespace Repository;
class MyClass { ... }
I can bind this using the ioc container:
App::bind('SomeClass', 'Repository\MyClass');
Same thing with model dependencies:
SomeClass extends Eloquent{
public function dependency()
{
return $this->hasOne('Models\Dependency');
}
}
One solution is to create aliases in app.php, but this has to be done for every single file that I want to have automatically namespaced:
'MyClass' => 'Repository\MyClass'
Is it possible to make the ioc container recognize the correct namespace for the classes without using aliases? Can we in any way use Composer for this?
It is not possible unless you want to register an alias for every class in your namespaces. My recent Laravel 4 project has IOC Container bindings to namespaced classes which I declared explicitly.
App::bind('Foo\Bar\Repositories\UserRepositoryInterface', 'Foo\Bar\Repositories\DbUserRepository');
You could use
cd /path/to/laravel/
php composer dump-autoload
to have laravel see all the (new) classes and load them correctly
Add app/repository to the autoload section of the composer.json file, like this for example:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/repository"
]
},
Then run composer dump-autoload.
I am currently running into a problem when trying to use a custom helper class in Laravel 4.
I've created a folder in app/libraries which has a custom class MenuComposer.
app/libraries/folder/MenuComposer.php
<?php
namespace 'folder\MenuComposer'
class MenuComposer {
// Code here
}
I've edited composer.json to autoload the app/libraries folder and ran the dump-autoload command in console.
composer.json
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/libraries"
]
},
And finally I call the class like so:
View::composer('layouts.back', 'folder/MenuComposer');
Whatever I try, Laravel keeps returning the message Class 'MenuComposer' not found
Does anyone here know what the problem might be?
Your namespace should be declared as the following rather than with quotes:
namespace folder\MenuComposer;
Composer dump-autoload then generates the following in your "/vendor/composer/autoload_classmap":
'folder\\MenuComposer\\MenuComposer' => $baseDir . '/app/libraries/folder/MenuComposer.php'
Which would indicate the class can be reached at:
folder/MenuComposer/MenuComposer
Hope this helps!