Problem: I'm trying to bootstrap L4 framework for some files (php-resque workers at app/workers) to use, but the workers dont seem to be able to find the class DB on a line that used DB::table('tablename').... composer dumpautoload does not help.
Did the bootstrap I'm using not load the DB classes? It seemed to have loaded the worker classes.
Failed L4 Bootstrapping
<?php
// Bootstrap the laravel environment for our resque workers.
require __DIR__.'/bootstrap/autoload.php';
error
PHP Fatal error: Class 'DB' not found in /var/www/dev/app/libraries/test.php on line 50
composer.json
{
"require": {
"laravel/framework": "4.0.*",
"chrisboulton/php-resque": "#stable"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/libraries",
"app/workers"
]
},
"scripts": {
"post-update-cmd": "php artisan optimize"
},
"minimum-stability": "dev"
}
You could try to:
1) Load the Composer autoload file, located in vendor/autoload.php (you are actually calling the laravel one)
2) Replicate the calls in the public/index.php file in your test.php file, specifically:
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';
$app->run();
$app->shutdown();
Edit: after further inspection, you could try to do this with a bare minium:
<?php
require '../vendor/autoload.php';
use Illuminate\Support\Facades\DB as DB;
// Test the DB Instance
$dbInstance = new DB;
var_dump ($dbInstance);
will output
object(Illuminate\Support\Facades\DB)[2]
Related
I have an RSA algorithm Library giving to me by a payment gateway and When I do a
include (app_path().'/PaymentGateway/Crypt/RSA.php');
this and try to make an object as $rsa = new Crypt_RSA(); this it gives me and error saying
Class 'App\Http\Controllers\Crypt_RSA' not found
I tried including it in web.php and making an object it worked the problem occur when I try to include it in a Controller.
This is what I did. Oh and a little back ground I use to have this in Laravel 4, PHP 5, jpgraph 2.
I am using jpgraph 4.1 on Laravel 5.5 using PHP 7.
Created a folder under app called jpgraph
Placed the src folder that is in the tarball of jpgraph in that folder
Created file call Graph1.php, is my code using jpgraph, with the class Custom_GraphsJM in the jpgraph folder.
In composer.json added "app/jpgraph/Graph1.php" to the "classmap"
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/jpgraph/Graph1.php"
],
"psr-4": {
"App\\": "app/"
}
},
In the application folder:
composer dump-autoload
Checked the autoload_classmap.php and I have
'Custom_GraphsJM' => $baseDir . '/app/jpgraph/Graph1.php',
In my Model at the top I have
use Custom_GraphsJM;
To create a class
$Two_Graphs_Temp = new Custom_GraphsJM();
You can tell Composer to autoload any (non-PSR) class by adding the base folder to:
"autoload": {
"classmap": [
"app/commands",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
....
And you can also autoload autoloaders by adding them to the files section:
"autoload": {
"files": [
"temboo/src/Temboo_Loader.php"
],
...
After adding those entries, execute:
composer dumpautoload
And check the file vendor/composer/autoload_classmap.php, the available classes must be all listed in it, if one file is not there it will not be autoloaded.
On default, everything included in the app folder of your laravel project is autoloaded, that is described in the composer.json of your project:
...
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
...
The only thing you will need to do is simply use the namespace:
use App/Path/To/Third/Party/plugin/Class;
If, however, the plugin is placed outside of the scope of App, then simply add it to the psr-4 autoloader:
"psr-4": {
"ProjectRootNs\\": "projects/myproject/"
}
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
I get the following error when I run phpunit with Laravel 4.
PHP Fatal error: Class 'Illuminate\Foundation\Testing\TestCase' not found in
composer.json
"require": {
"laravel/framework": "4.0.*",
"phpunit/phpunit": "3.7.*"
},
app.php
'Illuminate\Foundation\Testing\TestCase'
What ı should do?
It looks like the autoload doesn't include the new requirement.
Be sure to run composer update to ensure that the file are downloaded and the autoloader is updated with that source.
If the files were downloaded and 'installed' manually run php composer dump-autoload to rebuild the autoload file.
I just ran into the same problem, so I thought I'd post my solution, even though it might be a different solution to what you were after.
I wanted to autoload my own libraries, so I added the following to my composer.json:
"autoload": {
"psr-0": {
"Fhc": "app"
}
},
What I didn't realise was that just above that line was the following:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
},
In essence, my modification had completely overridden the the code above. The solution was to merge the two together (as I should have done to begin with).
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-0": {
"Fhc": "app"
}
},
Now it's all working as expected.
I hope this helps anyone else in the same situation.
Try to remove your "vendor" folder and the file named composer.lock after that run:
composer install
Pay attention to the output produced by composer.
I am currently running into a problem when trying to use a custom helper class in Laravel 4.
I've created a folder in app/libraries which has a custom class MenuComposer.
app/libraries/folder/MenuComposer.php
<?php
namespace 'folder\MenuComposer'
class MenuComposer {
// Code here
}
I've edited composer.json to autoload the app/libraries folder and ran the dump-autoload command in console.
composer.json
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/libraries"
]
},
And finally I call the class like so:
View::composer('layouts.back', 'folder/MenuComposer');
Whatever I try, Laravel keeps returning the message Class 'MenuComposer' not found
Does anyone here know what the problem might be?
Your namespace should be declared as the following rather than with quotes:
namespace folder\MenuComposer;
Composer dump-autoload then generates the following in your "/vendor/composer/autoload_classmap":
'folder\\MenuComposer\\MenuComposer' => $baseDir . '/app/libraries/folder/MenuComposer.php'
Which would indicate the class can be reached at:
folder/MenuComposer/MenuComposer
Hope this helps!
I am migrating a working production L3 site to use L4. When a controller calls a library class (app/libraries/adminthing.php), I get the error Error: Class 'adminthing' not found in /var/www/l4/app/controllers/AdminController.php line 15
start/global.php
ClassLoader::addDirectories(array(
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/libraries',
));
I have also done composer dumpautoload after adding the library class. What else did I miss out?
You can autoload folders from composer.json. If you have some custom classes in a folder under /app you can add the folder to composer.json, and after this the classes are auto loaded.
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/customlib" <-- add this
]
},
Then composer dump-autoload, and you can use the classes!