PHP Include class by using "use" with PSR-4 - php

I'm using PSR-4, I defined the psr-4 section on composer.json
"autoload": {
"classmap":[
],
"psr-4": {
"App\\": "app/"
}
},
When I tried to import a class by using "use" keyword
use App\Http\Controllers\Controller;
I get this error:
FatalErrorException in UploadFilesController.php line 13:
Class 'Controllers\Controller' not found
If I set the static route to the file
include("C:\xampp\...");
it works, but after that found the same problem with other files including PHP clases like Illuminate.
What's the problem?

Related

Laravel Newly created seeder does not exist

I have a new Laravel Seeder but is giving a class does not exist error. The said seeder works in my local machine but not in production. The only file that I pushed was the seeder file itself.
File is in database\seeders
Filename: SeederName.php
Here is the code structure:
namespace Database\Seeders;
use App\Models\Model1;
use App\Models\Model2;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class SeederName extends Seeder
{}
When I run the command php artisan db:seed --class=SeederName, it returns a Target class [Database\Seeders\SeederName] does not exist.
One of the top answers given was to run the command composer dump-autoload However, when I run the said command, I am getting a Class Database\Seeders\SeederName located in ./database/seeders/SeederName.php does not comply with psr-4 autoloading standard. Skipping.
My composer.json
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
I also compared this new seeder with my other seeder file and code structure is the same.
I don't know why, but when I create a new seeder, now it works. Everything is the same except for the File Name and Class Name.

Composer psr-4 autoload class not found error after changing prefix

i've already been looking for solutions to my problem but could'nt find any so far.
{
"name": "petersil98/thresh",
"version": "1.0.0",
"type": "library",
"autoload": {
"psr-4": {
"Thresh\\": "src/"
}
}
}
This is my public/test.php:
<?php
require_once '../vendor/autoload.php';
use Thresh\Helper\Config;
Config::setPlatform("euw1");
And this is my src/Helper/Config.php:
<?php
namespace Thresh\Helper;
class Config{...}
This is the error i get: Fatal error: Uncaught Error: Class 'Thresh\Helper\Config' not found
First i had my psr-4 autoload registered with the prefix 'src'
"autoload": {
"psr-4": {
"src\\": "src/"
}
}
After changing the psr-4 autoload prefix to Thresh (and updating the namespaces) and running composer dump-autoload it doesnt work anymore
PS: composer dump-autoload returns Generated autoload files containing 0 classes

Composer load single class with namespace

I'm trying to use symfony flex with a bundle.
I have this directory structure
/
src/
AppBundle/
AppBundle.php
# Many classes
Kernel.php
I want to load Kernel.php class with this namespace App and classes inside AppBundle with the namespace AppBundle.
I've tried many composer configurations but I couldn't load them.
"psr-4": {
"AppBundle\\": "src/AppBundle/",
"App\\": "src/"
}
But I got errors like this:
Expected to find class "App\AppBundle\AppBundle" in file "/var/www/vhosts/flex/src/AppBundle/AppBundle.php"
UPDATE
src/Kernel.php class have a different namespace and I couldn't change it because other classes used it, the namespace is App. Some scripts call that class using use App\Kernel
src/AppBundle/AppBundle.php class has this namespace AppBundle
There is way to do this?
UPDATE 2
I sorted it out:
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle/"
},
"classmap": [
{ "App\\Kernel": "src/Kernel.php" }
]
},
I would suggest using a classmap for Kernel.php instead:
"autoload": {
"psr-4": { "AppBundle\\": "src/AppBundle" },
"classmap": [ "src/Kernel.php" ]
}
See https://getcomposer.org/doc/04-schema.md#classmap
You only need
"psr-4": { "App\\": "src/" }

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

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