laravel 4 command 'class not found' - laravel-4

I'm create new command "php artisan command:make mycommand"
The command was added, but when I try to add "Artisan::add(new mycommand)" in app/start/artisan.php it doesn't work for any command, like "php artisan -V" and show this:
[{"error":
{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Class 'mycommand' not
found","file":"\/usr\/home\/xxxx.com\/htdocs\/local\/app\/start\/artisan.
php","line":15}}[xxx#xxx~/htdocs/local]$ ]
I've tried "composer dump-autoload" but also show this:
[ [RuntimeException]
Could not scan for classes inside "app/extensions" which does not
appear to be a file nor a folder ]

Did you include the commands folder to your composer.json and app/start/global.php files ?
global.php
ClassLoader::addDirectories ( array (
app_path () . '/commands',
...
));
composer.json
"autoload": {
"classmap": [
"app/commands",
....

Did you apply a namespace to your command?

Related

How to add a class that autoloads in Laravel?

I am using Laravel 5.8 and I created a custom class named StatusLib.php in the app/library folder.
StatusLib.php
namespace App\library;
class StatusLib
{
CONST SUCCESS = '100';
CONST SUCCESSWITHMESSAGE = '101';
}
I can call this status .
StatusLib::SUCCESS
When I add this following use code in the controller.
use app\library\StatusLib;
How can I add this StatusLib class in autoload and access from anywhere in the project?
Namespaces are case-sensitive.
In your StatusLib class you have App\library;, however, in your controller you've used app\library -- these are not the same.
Change your use statement in your controller to be:
use App\library\StatusLib;
You may also need to run:
composer dumpautoload
Just FYI, Laravel comes with the app directory already set up for autoloading.
In your composer.json file, after the classmap array, add a psr-0:
"autoload" :{
"classmap": [
...
],
"psr-0": {
"library": "app/"
}
}
Run composer dump-autoload.
Hope it helps.
where do you want to use it?
it will be automatically autoloaded because app folder is loaded in composer.json
here:
"autoload": {
"psr-4": {
"App\\": "app/"
},
},

Laravel 5.6: How to use all models contained in one folder

I have a folder named "Models" located at app/models in my laravel project directory.
I want to use all models by a single line of code in controller, instead of using each one like:
use App/Models/User;
use App/Models/Roles;
You could try this
spl_autoload_register(function($className) {
include_once $_SERVER['DOCUMENT_ROOT'] . '/app/models/' . $className . '.php';
});
You need to add this folder in your composer.json file.
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/Models"
]
}
then run composer update

Laravel Test Cases

I am using Laravel 5.3 and for testing purpose, I have created a folder called "Helpers" is "app" folder. In "Helpers" folder, there is a file named credentials.php where I will declare some strings then I will use it later. Credentials.php looks like
`
<?php
namespace App\Helpers;
class Credential
{
}
`
In the 'tests' folder there is folder called 'links' and in this folder there is file called something.php and it looks like
<?php
use App\Helpers\Credential;
class something extends TestCase
{
}
and its working correctly...Problem arising when I am trying to put the "helpers" folder from "app" to "tests" folder. I dont want to put the "Helper" folder in "app" directory. I have changed namespace and other things, nothing works. I am new to laravel and seeking your help and suggestion.
Update Composer.json and add files key in autoload section.
"autoload": {
"files": [
"tests/helpers.php"
],
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
After Adding this, run the following command.
composer dump-autoload

Laravel 5.2.45 Tcpdf installation failed with php version 5.5.11

I need to generate a PDF using TDPDF with Laravel 5.2.45.
I used the following command (reference: here):
composer require elibyy/tcpdf-laravel
and received the following message:
Message: Your PHP version <5.5.11> does not satisfy that requirement.
Is there any other way to download TCPDF with Laravel if my PHP version do not satisfy the requirement?
I updated my composer with following command
composer update
Then added the following line to my composer.json file
{
"require": {
"elibyy/tcpdf-laravel": "5.4.*"
} }
Next added the service provider to config/app.php.
'providers' => [
//...
Elibyy\TCPDF\ServiceProvider::class,
]
//...
'aliases' => [
//...
'PDF' => Elibyy\TCPDF\Facades\TCPDF::class
]
and again run the following command
composer update
And then to my controller I added the following lines:
use PDF; // at the top of the file
PDF::SetTitle('Hello World');
PDF::AddPage();
PDF::Write(0, 'Hello World');
PDF::Output('hello_world.pdf');
Now I am able to generate a PDF.

How to tell Laravel 5 to aoutoload my custom package?

I have create in vendor directory the following structure for my package:
/vendor/
koala/
bamboo-sdk/
src/
Engine2/
Utils/
I then run
$composer init
inside koala folder.
Lately I went to Laravel5 composer.json main. And modified the autoload.psr-4 part like this:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
...
"Koala\\BambooSdk\\": "vendor/koala/bamboo-sdk/src"
}
},
I then run
$composer dump-autoload
in order to have /vendor/composer/autoload_classmap.php updated with the new references.
But I dont see it changing.
Also having a new controller with directive:
use Koala\BambooSdk\MyClass;
and then below
$myobj = new MyClass();
will result in a error:
FatalErrorException in MyController.php line 165:
Class 'Koala\BambooSdk\MyClass' not found

Resources