I have a serious problem and I have tried everything to solve it without success. I am trying to call the Traits ParamTrait and MsgTrait from a Scheduler (Command) called MsgCron. Traits work fine for me throughout the app except Command.
Here my code MsgCron.php
<?php
namespace App\Traits;
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use App\Models\Cita;
class MsgCron extends Command
{
protected $signature = 'msg:cron';
protected $description = 'Command description';
public function __construct()
{
parent::__construct();
}
use ParamTrait;
use MsgTrait;
public function handle()
{
$dia = date("d");
$mes = date("n");
$ano = date("Y");
Log::channel('balancemed')->info($dia.$mes.$ano);
return 0;
}
}
My composer.json:
"autoload": {
"files": [
"app/helpers.php",
"app/Traits/MsgTrait.php",
"app/Traits/ParamTrait.php",
"app/Console/Commands/MsgCron.php"
],
Running the php artisan schedule:run command gives me a fatal error:
ubuntu:~/environment/balancemed $ php artisan schedule:run
PHP Fatal error: Trait "App\Console\Commands\ParamTrait" not found in /home/ubuntu/environment/balancemed/app/Console/Commands/MsgCron.php on line 12
PHP Stack trace:
PHP 1. {main}() /home/ubuntu/environment/balancemed/artisan:0
PHP 2. require() /home/ubuntu/environment/balancemed/artisan:18
PHP 3. ComposerAutoloaderInita6e7bc892212647700001d53e511801f::getLoader() /home/ubuntu/environment/balancemed/vendor/autoload.php:7
PHP 4. composerRequirea6e7bc892212647700001d53e511801f($fileIdentifier = 'c272475c5c4e0eb0a144305d0ca49ad6', $file = '/home/ubuntu/environment/balancemed/vendor/composer/../../app/Console/Commands/MsgCron.php') /home/ubuntu/environment/balancemed/vendor/composer/autoload_real.php:61
PHP 5. require() /home/ubuntu/environment/balancemed/vendor/composer/autoload_real.php:71
Al ejecutar el comando composer dumpautoload me arroja el siguiente error:
ubuntu:~/environment/balancemed $ composer dumpautoload
Generating optimized autoload files
Class App\Http\Controllers\MsgTrait located in ./app/Traits/MsgTrait.php does not comply with psr-4 autoloading standard. Skipping.
Class App\Http\Controllers\ParamTrait located in ./app/Traits/ParamTrait.php does not comply with psr-4 autoloading standard. Skipping.
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
PHP Fatal error: Trait "App\Console\Commands\ParamTrait" not found in /home/ubuntu/environment/balancemed/app/Console/Commands/MsgCron.php on line 12
Fatal error: Trait "App\Console\Commands\ParamTrait" not found in /home/ubuntu/environment/balancemed/app/Console/Commands/MsgCron.php on line 12
I have tried with php artisan optimize and nothing. It also gives me an error.
Can somebody help me ?
You should add your trait path to the MsgCron.php
use App\Http\Traits\MsgTrait;
use App\Http\Traits\ParamTrait;
Your Class Should Be Like This
use App\Http\Traits\MsgTrait;
use App\Http\Traits\ParamTrait;
class MsgCron extends Command
{
use MsgTrait;
use ParamTrait;
protected $signature = 'msg:cron';
protected $description = 'Command description';
...
}
Related
Many people have asked about this error: Target class [DatabaseSeeder] does not exist. It seems to have many root causes, but I cannot determine my root cause.
I am using Laravel 6.20.43. The software does not produce any errors when run in the browser.
The error
The error appears when I use this command: php artisan db:seed
Here is DatabaseSeeder.php:
<?php
namespace Database\seeds;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use App\User;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
// factory$this->call(UsersTableSeeder::class);
// factory(App\User::class, 10)->create();
// dd('DatabaseSeeder.php TESTING...');
}
}
What have I tried?
I have tried to add a dd(...) inside DatabaseSeeder::run(). The dd(...) is not executed.
I have tried composer update. The update was performed nicely, but did not resolve the error.
I have tried several use clauses in DatabaseSeeder.php.
I have tried php artisan migrate:fresh
I have tried several combinations of solutions, for example to run migrations before dump-autoload and vice versa.
I have tried composer dump-autoload and this is the output:
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> #php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
Generated optimized autoload files containing 4381 classes
I have tried to redirect the output of php artisan db:seed to xdebug so I can analyse step by step what is happening. Good luck is what I needed here, but I ran out of luck.
I have tried to examine the error using php artisan db:seed -vvv. Here is the full output:
Illuminate\Contracts\Container\BindingResolutionException : Target class [DatabaseSeeder] does not exist.
at /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:805
801|
802| try {
803| $reflector = new ReflectionClass($concrete);
804| } catch (ReflectionException $e) {
> 805| throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
806| }
807|
808| // If the type is not instantiable, the developer is attempting to resolve
809| // an abstract type such as an Interface or Abstract Class and there is
Exception trace:
1 ReflectionException::("Class DatabaseSeeder does not exist")
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:803
2 ReflectionClass::__construct()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:803
3 Illuminate\Container\Container::build()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:681
4 Illuminate\Container\Container::resolve()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:785
5 Illuminate\Foundation\Application::resolve()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:629
6 Illuminate\Container\Container::make()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:770
7 Illuminate\Foundation\Application::make()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php:76
8 Illuminate\Database\Console\Seeds\SeedCommand::getSeeder()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php:63
9 Illuminate\Database\Console\Seeds\SeedCommand::Illuminate\Database\Console\Seeds\{closure}()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php:129
10 Illuminate\Database\Eloquent\Model::unguarded()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php:64
11 Illuminate\Database\Console\Seeds\SeedCommand::handle()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
12 Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Util.php:37
13 Illuminate\Container\Util::unwrapIfClosure()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
14 Illuminate\Container\BoundMethod::callBoundMethod()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37
15 Illuminate\Container\BoundMethod::call()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:590
16 Illuminate\Container\Container::call()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Console/Command.php:134
17 Illuminate\Console\Command::execute()
/home/billybob/laravel-cursus1/vendor/symfony/console/Command/Command.php:255
18 Symfony\Component\Console\Command\Command::run()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Console/Command.php:121
19 Illuminate\Console\Command::run()
/home/billybob/laravel-cursus1/vendor/symfony/console/Application.php:1009
20 Symfony\Component\Console\Application::doRunCommand()
/home/billybob/laravel-cursus1/vendor/symfony/console/Application.php:273
21 Symfony\Component\Console\Application::doRun()
/home/billybob/laravel-cursus1/vendor/symfony/console/Application.php:149
22 Symfony\Component\Console\Application::run()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Console/Application.php:93
23 Illuminate\Console\Application::run()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:131
24 Illuminate\Foundation\Console\Kernel::handle()
/home/billybob/laravel-cursus1/artisan:37
I truly cannot understand how to debug the output of php artisan db:seed -vvv. All those files reside in the vendor directory, meaning I cannot examine the program flow easily. Please also explain how I can debug such an error by myself in the future.
It cannot find your DatabaseSeeder class because your namespace is incorrect.
You need to change the namespace of your DatabaseSeeder class from:
namespace Database\seeds;
to:
namespace Database\Seeders;
Make sure your composer autoload is setup correctly:
"autoload": {
"psr-4": {
"Database\\Seeders\\": "database/seeders/"
}
}
Alternatively, completely remove all namespaces from your DatabaseSeeder class and revert the above changes to composer.json and then run:
composer dump-autoload
then try running:
php artisan migrate:fresh --seed
I am developing a package for Laravel 5.8. When I try to create a console command that extends Illuminate\Console\Command then "composer dump-autoload" fails with error message:
c:\Program Files (x86)\Ampps\www\ptest>composer dump-autoload
Generating optimized autoload files> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> #php artisan package:discover --ansi
ReflectionException : Class TestVendor\TestPackage\TestCommand does not exist
at C:\Program Files (x86)\Ampps\www\ptest\vendor\laravel\framework\src\Illuminate\Container\Container.php:790
786| if ($concrete instanceof Closure) {
787| return $concrete($this, $this->getLastParameterOverride());
788| }
789|
> 790| $reflector = new ReflectionClass($concrete);
791|
792| // If the type is not instantiable, the developer is attempting to resolve
793| // an abstract type such as an Interface or Abstract Class and there is
794| // no binding registered for the abstractions so we need to bail out.
Exception trace:
1 ReflectionClass::__construct("TestVendor\TestPackage\TestCommand")
C:\Program Files (x86)\Ampps\www\ptest\vendor\laravel\framework\src\Illuminate\Container\Container.php:790
2 Illuminate\Container\Container::build("TestVendor\TestPackage\TestCommand")
C:\Program Files (x86)\Ampps\www\ptest\vendor\laravel\framework\src\Illuminate\Container\Container.php:667
I have tried to create the package by hand inside of C:\Program Files (x86)\Ampps\www\ptest\packages folder and I tried to use the packager https://github.com/Jeroen-G/laravel-packager but the result is identical in both cases.
TestCommand.php
<?php
namespace TestVendor\TestPackage;
use Illuminate\Console\Command;
class TestCommand extends Command {
protected $signature = 'test:hello';
protected $description = 'say hello';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$this->info("hello!");
}
}
TestServiceProvider.php
<?php
namespace TestVendor\TestPackage;
use Illuminate\Support\ServiceProvider;
class TestServiceProvider extends ServiceProvider
{
public function boot()
{
if ($this->app->runningInConsole()) {
$this->bootForConsole();
}
}
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/testpackage.php', 'testpackage');
$this->app->singleton('testpackage', function ($app) {
return new TestPackage;
});
}
public function provides()
{
return ['testpackage'];
}
protected function bootForConsole()
{
// Registering package commands.
$this->commands([TestCommand::class]);
}
}
When I execute the TestCommand.php file directly from command line it fails with the error message
PHP Fatal error: Class 'Illuminate\Console\Command' not found
I have checked other working packages inside "Vendor" folder and all have the same structure as my package. It seems as if autoloading does not work properly.
The "console" folder was outside of "src" folder. Therefore it could not be discovered.
remove vendor and node_modules folder
then run following commands
composer update
npm install
it should work fine.
On running this test, I get Error: Call to undefined method OrderControllerTest::request()
<?php
use PHPUnit\Framework\TestCase;
class OrderControllerTest extends TestCase
{
public function testupload() {
$a='foo';
$output = $this->request('POST',['Order', 'upload',$a] );
}
}
PHPUNIT Version: 7.2.4. Appreciate any help
For the kenjis/ci-phpunit-test package if you run phpunit from the application/tests folder so it picks up on the included phpunit.xml and included TestCase Class then they should run.
cd application/tests
then, if you are using the composer installed version of phpunit in your project:
../../vendor/bin/phpunit
or if you're using the globally (apt etc.) installed version simply:
phpunit
see http://blog.a-way-out.net/blog/2015/06/12/codeigniter3-phpunit/#how-to-run-tests
The package kenjis/ci-phpunit-test extends the standard PHPUnit package so what you need to do is to extend the CIPHPUnitTestCase instead like in example below.
<?php
class OrderControllerTest extends CIPHPUnitTestCase
{
public function testupload() {
$a='foo';
$output = $this->request('POST',['Order', 'upload',$a] );
}
}
You may need to configure your IDE so that it can find CIPHPUnitTestCase class.
I am using Eclipse pdt for PHP Developers Version: Oxygen.2 Release (4.7.2).
I created a Composer Project and I added those dependencies:
then I created a TestCase file to test my class.
I could not change the superclass "PHPUnit_Framework_TestCase".
I got this warning when creating the TestCase file
There is no element 'PHPUnit_Framework_TestCase' in the project
'PayementAPI'
then into the default TestCase Class created I changed the extends "PHPUnit_Framework_TestCase" to "TestCase" and I added the import.
<?php
use PHPUnit\Framework\TestCase;
include 'otherClass.php';
/**
* MyClass1 test case.
*/
class MyClass1Test extends TestCase
{
...
}
Then I tried to run my class test as PHPUnit test but got this error:
PHP Fatal error: Declaration of PHPUnitLogger::flush() must be compatible with PHPUnit\Util\Printer::flush(): void in C:\Users\User\AppData\Local\Temp\phpunit_printer\PHPUnitLogger.php on line 33
Try one with a lower version of phpunit.phar, download the lower version here.
I tried to run a PHP file (of cakePHP) by the following
$ /bin/sh /var/www/html/app/Console/cake HumanResource UpdateData
But there is an error occur:
PHP Fatal error: Class 'Inflector' not found in /var/www/html/app/lib/Cake/Console/ShellDispatcher.php on line 209
Some information in my server:
Centos: 6.5
PHP: 5.3
Httpd: 2.2.15
CakePHP version: 2.1.3
Please help me to fix it. Thanks
You forgot add:
use Cake\Utility\Inflector;
Example:
<?php
namespace App\Shell;
use Cake\Console\Shell;
use Cake\Utility\Inflector;
class CamerasShell extends Shell {
public function main() {
$this->out(Inflector::slug('Please Generate Me Slug', '-'));
}
}