Laravel 5.1 Namespace for Controller give Reflection Exception - php

Good Afternoon, I don´t understand this exception
ReflectionException in Container.php line 737: Class app\Http\Controllers\Login\LoginController does not exist
I know thats is related with the namespace but i configured my composer.json autoload. When i changed it to app\Http\Controllers\Login it works ok.
I also did artisan clear-compiled and composer dump-autoload
My Class is like this
<?php
namespace Login;
use Controller;
use Validator;
use Input;
use Auth;
use Redirect;
use View;
class LoginController extends Controller{}
My composer.json autoload
"autoload": {
"classmap":
[ "app/Http/Controllers" , "app/Models" , "database" ],
"psr-4": {
"app\\": "app/"
}
},
My Controllers Directory is like this
- app
-- Http
--- Controllers
---- Login
----- LoginController.php
-- Models
--- User
---- User.php
Thanks in advance
Edit: Why i don't have problems with my model files when i use a namespace like this:
namespace User;
In my config auth i have this:
'model' => User\User:class,

Your controller is in Login namespace, while it should be in app\Http\Controllers\Login namespace.
No entries in composer.json can change the way PHP's namespaces work - the mapping in there can just be used to tell autoloader where to look for physical files from given namespace.

Related

in laravel 8 with seeding , i has this issue Target class [TableSeeder] does not exist

Illuminate\Contracts\Container\BindingResolutionException
Target class [Database\Seeders\CountriesTableSeeder] does not exist.
at C:\......\blog\vendor\laravel\framework\src\Illuminate\Container\Container.php:811
807▕
808▕ try {
809▕ $reflector = new ReflectionClass($concrete);
810▕ } catch (ReflectionException $e) {
➜ 811▕ throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
812▕ }
813▕
814▕ // If the type is not instantiable, the developer is attempting to resolve
815▕ // an abstract type such as an Interface or Abstract Class and there is
1 C:\......\blog\vendor\laravel\framework\src\Illuminate\Container\Container.php:809
ReflectionException::("Class Database\Seeders\CountriesTableSeeder does not exist")
2 C:\......\blog\vendor\laravel\framework\src\Illuminate\Container\Container.php:809
ReflectionClass::__construct("Database\Seeders\CountriesTableSeeder")
From laravel 8 Seeders and factories are now namespaced
To accommodate for these changes, add Database\Seeders namespace to your seeder classes.
namespace Database\Seeders;
In addition, move all seeder files from previous database/seeds directory to database/seeders folder.
In your case remove all lines started with use Database\Seeders\...
from DatabaseSeeder.php file
It should solve the issue,
You can also run dump-autoload & fresh migration with seed,
composer dump-autoload
php artisan migrate:fresh --seed
For Laravel 8 you need to make the below changes to an existing project for seeding to work:
Add the Database\Seeders namespace to your DatabaseSeeder.php and other seeder files :
<?php
namespace Database\Seeders;
Change the folder name of database/seeds to database/seeders.
Update composer.json like below:
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
Finally, run the below commands:
composer dump-autoload
php artisan db:seed
remove using TableSeeder at the top of the DatabaseSeeder.php file.
When working with new seeders we might get
Target class [...TableSeeder] does not exist.
In my case, simply running
composer dump-autoload
did the trick
working with laravel 8
change directory name database/seeds to database/seeders.
add namespace Database\Seeders; to seeders file.
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class TableSeeder extends Seeder{
}
Update Compose.json file
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}},
run in terminal
composer dump-autoload
php artisan db:seed --class=TableSeeder
DatabaseSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
$this->call([
CountriesTableSeeder::class,
ProvincesTableSeeder::class,
RegionsTableSeeder::class,
PermisionsTableSeeder::class,
RolesTableSeeder::class,
StatusTypesTableSeeder::class,
StatusesTableSeeder::class,
]);
}
}
CountriesTableSeeder.php
<?php
use App\Country;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class CountriesTableSeeder extends Seeder
{
private $numberOfCountries = 10;
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('countries')->insert([
['country_name' => 'iraq'],
['country_name' => 'qater'],
]);
}
}
After running: composer dump:autoload
I could just include the below code in my DatabaseSeeder and It would include the data from SubsidyregimeTableSeeder
$this->call([
SubsidyregimeTableSeeder::class
]);
In my case, I had an existing code of version 5.8 of Laravel.
So the files were duplicated in Seeds folder, the namespace was missing in and I had to reinstall Voyager with dummy like so :
composer dump-autoload
php voyager:install --with-dummy
fixed the namespace to namespace Database\Seeders; instead of namespace Database\Seeds;
Rename directory database/seeds to database/seeders
Open composer.json file and change on autoload section.
"autoload": {
"psr-4": {
"App\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
}
Open Database Seeder file database/seeders/DatabaseSeeder.php and add namespace Database\Seeders; on top of page.
Open your others seeders file and add namespace Database\Seeders; to all seeders class.
Run composer dump-autoload and php artisan migrate:fresh --seed
Remember if you upgrade ti laravel 8 you need to change directory name database/seeds to database/seeders.

Class not found after auto loading in Laravel

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/"
}

Laravel extending Illuminate\Foundation\Testing\TestCase with custom namespace

I'm trying to set up unit tests for my Laravel (5.2) API project. Before I use the unit tests I'd like to define a custom namespace for them so I created the namespace Test; in the default TestCase.php file. Like so:
namespace Test;
class TestCase extends Illuminate\Foundation\Testing\TestCase
{
...
}
Then I created a folder UnitTests under the tests folder and put my unit tests in that folder with the following namespace:
namespace Test\UnitTests;
use Test\TestCase;
class CreateAccountTest extends TestCase
{
...
}
Now when I want to run my unit tests I get the following error:
PHP Fatal error: Class 'Test\Illuminate\Foundation\Testing\TestCase' not found in /var/www/ops/tests/TestCase.php on line 6
So basically, Laravel thinks the Illuminate\Foundation\Testing\TestCaseclass is found within the Test namespace instead of the Illuminatenamespace from Laravel.
I also have the following autoload configured in the composer.json file:
"autoload": {
"classmap": [
"database",
"tests"
],
"psr-4": {
"App\\": "app/",
"Test\\": "tests/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
}
I've also tried running both commands with no success:
php artisan config:cache
composer dump-autoload
As #xmike mentioned in the comments of the question I didn't add a backslash to the namespace.
So instead of
class TestCase extends Illuminate\Foundation\Testing\TestCase
it should be
class TestCase extends \Illuminate\Foundation\Testing\TestCase

Controller class not found in Laravel 4

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.

Laravel could not autoload a model from within Acme Directory?

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

Categories