About L5 Autoload How add load all class Written? - php

The follow is my composer.json
autoload": {
"classmap": [
"database",
"app/library/"
if I use folder divide them like follow
how to add the code could load api & Identity
"app/library/* <---could use like wildcard ?
in Uniqid.php
<?php namespace app\library\identity;
in rgAPI,php
<?php namespace app\library\api;

If you see class not found then you can try these commands
composer dump-autoload
php artisan clear-compiled
php artisan optimize
This can make the classes in these folders found again.

Related

Laravel Dusk says Class 'Tests\Browser\Components\...' could not be found

After I installed laravel-dusk following the official Laravel documentation and run this command:
php artisan make:component CardComponentTest
Then try to run immediately:
php artisan dusk tests/Browser/Components/CardComponentTest.php
I get this error:
Class 'Tests\Browser\Components\CardComponentTest' could not be found in '/var/www/html/tests/Browser/Components/CardComponentTest.php'.
I tested file and path are correct:
ls -l /var/www/html/tests/Browser/Components/CardComponentTest.php
And it says:
-rw-r--r-- 1 djw djw 6917 Dec 3 11:25 /var/www/html/tests/Browser/Components/CardComponentTest.php
So it is exists and readable.
I checked the namespace in the file:
<?php
namespace Tests\Browser\Components;
It also looks good.
I checked the composer.json and in this I have this section:
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
So the file exists, the namespace is good and the namespace is picked up in the composer.json.
I tried to run composer dump-autoload too. All good.
Any ide what's wrong whit this?
the error:
Class 'Tests\Browser\Components\CardComponentTest' could not be found in '/var/www/html/tests/Browser/Components/CardComponentTest.php'.
indicate you don't have CardComponentTest test case. Because CardComponentTest is not test case but just an component/part of test.
As aless said, component is not a test itself. But if you really want to test CardComponentTest you can replace
use Laravel\Dusk\Component as BaseComponent;
class CardComponentTest extends BaseComponent
to :
use Tests\DuskTestCase;
class CardComponentTest extends DuskTestCase
after you do that you can run php artisan dusk tests/Browser/Components/CardComponentTest.php , You just need to add test method first like. An example:
public function testComponentExample()
{
$this->browse(function (Browser $browser) {
$browser->visit('/')
->assertSee('Laravel');
});
}
But create test case in component folder is not best practice. You should create test case like official documentation said. Run dusk:make instead of dusk:component. An example:
php artisan dusk:make SimpleBrowserTest
and now you can run
php artisan dusk tests/Browser/SimpleBrowserTest.php
If you take a look at the documentation, the Component is not a test itself. Hence, you should not name the component "*Test" and you should not try to run the component like a dusk test.
You will use your component in your dusk test as described here:
https://laravel.com/docs/9.x/dusk#using-components
Components are for repeated actions, like selecting a date in a picker, which is then further used in your test case for example in a form or maybe some change in your UX happens that is assertable.

Laravel: Created a service but class is not found

Created the following file:
File: App\Services\Custom\Auth\AuthService.php
Name space: App\Services\Custom\Auth
Class name: AuthCustom
Method inside: foo()
In my controller I'm trying to call the foo method from the Service I created.
App\Services\Custom\Auth\AuthService\AuthCustom::foo()
Why does it keep returning Class 'App\Services\Custom\Auth\Authservice\AuthCustom' not found
What am I doing wrong?
Thanks!!
EDIT:
I added this in the composer.json and run composer dump-autoload without errors.
And it works!
"autoload": {
"classmap": [
"database",
"app/Services/Custom/Auth/AuthService.php"
],
"psr-4": {
"App\\": "app/"
}
},
Your namespace does not match your directory structure. If your class is in App\Services\Custom\Auth\AuthService.php, then your namespace needs to be App\Services\Custom\Auth. If you really want your namespace to be App\Custom\Auth, then your file needs to be App\Custom\Auth\AuthService.php.
Once you fix this, make sure you do a composer dump-autoload on the command line.
It seems that you didn't run composer dump-autoload or php composer.phar dump-autoload.
The composer.json is very important for autoloading!
Laravel needs an big file with all your php files required, usually generated via calling either artisan or composer with : php artisan dump-autoload / composer dump-autoload
It just regenerates the list of all classes that need to be included in the project (autoload_classmap.php). Ideal for when you have a new class inside your project.
More details: http://developed.be/2014/08/29/composer-dump-autoload-laravel/

How do I require a file or class in a vendor library in Yii2 that is not already included?

I have this class that Yii2 cannot see. All the other classes work. I tried the following. The commented lines did not work.
// use app\vendor\googleads\googleads-php-lib\src\Google\Api\Ads\Common\Util\ErrorUtils;
// require_once UTIL_PATH . '/ErrorUtils.php';
require_once('../vendor/googleads/googleads-php-lib/src/Google/Api/Ads/Common/Util/ErrorUtils.php');
use \ErrorUtils;
This works, but it doesn't look right. Also it doesn't work in the command mode, which I need.
$ yii cron
PHP Warning: Uncaught exception 'yii\base\ErrorException' with message 'require_once(../vendor/googleads/googleads-php-lib/src/Google/Api/Ads/Common/Util/ErrorUtils.php): failed to open stream: No such file or directory' in /cygdrive/c/Users/Chloe/workspace/xxx/models/GoogleAdWords.php:36
How can I require or use this class in Yii2?
Fisrt addto composer (shell command):
$ composer require googleads/googleads-php-lib
Then simply use te class:
\ErrorUtils::GetApiErrors($var);
Note that googleads don't use namespaces so it's "\" NS
The library that you use doesn't provide psr-4 autoloader settings for its classes. You need to add autoload classmap for classes that you want to load, into your composer.json in project's root directory like following:
"autoload": {
"classmap": [
"vendor/googleads/googleads-php-lib/src/Google/Api/Ads/Common/Lib",
"vendor/googleads/googleads-php-lib/src/Google/Api/Ads/Common/Util"
]
},
and then in your console: composer dump-autoload
This will update the composer autoloader. After that you'll be able to call library classes with: \ErrorUtils::GetSourceOperationIndex($error)

Laravel 5: How can I add seeder class to autoload?

I follow the docs: http://laravel.com/docs/master/migrations#database-seeding
I placed UserTableSeeder file near DatabaseSeeder. In resources/database/seeds/ folder.
These files are without namespaces (only classes in app/ are namespaced).
Of course there is an exception: exception 'ReflectionException' with message 'Class UserTableSeeder does not exist'
What is the best way to solve this problem?
The default Laravel 5 project has a classmap defined in its composer.json:
{
// ...
"autoload": {
"classmap": [
"database"
],
// ...
}
}
Run composer dump every time you add or remove a class on your database directory to update the Composer autoloader
Reference: https://github.com/laravel/laravel/blob/develop/composer.json
You should use composer dump-autoload command. From the docs:
Once you have written your seeder, you may need to regenerate Composer's autoloader using the dump-autoload command:
composer dump-autoload
Reference here.

Add 'Gregwar's Image class' to Laravel

I want add this class to laravel - https://github.com/Gregwar/Image
There is 'Using with composer' - I added this line to composer.json
"gregwar/image": "dev-master"
I was run:
php artisan optimize
composer dump-autoload
But if i want use this class - have this error:
Class 'Image' not found
class is installed (by composer) in root: vendor/gregwar/...
As the readme says, you have to "require" it using its namespace:
use Gregwar\Image\Image;
So your code should look like:
<?php
use Gregwar\Image\Image;
echo Image::open('test.png')
->sepia()
->jpeg();

Categories