What i'm trying to do : setup PSR-0 autoloading but composer is generating a PSR-4 autoloader file.
I'm Using:
1) Laravel 4.2
2) Composer version e77435cd0c984e2031d915a6b42648e7b284dd5c 2014-07-02 15:44:54
My composer.json:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/libraries"
],
"files": [
"app/libraries/custom_helpers.php"
],
"psr-0":{
"MyApp":"app/"
}
}
//rest of the file is omitted
After this if i run composer dump-autoload in the terminal it should generate a file in the
vendor/composer directory called autoload_psr0.php
Instead it generates a autolooad_psr4.php and it looks like this :
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'),
);
From the composer docs i understand that psr-0 is still supported.
Could this be due to to the Monolog package requiring PSR-4 namespacing ?
Well it seems laravel actually doesn't create a separate config for the psr0 standards.
It in fact has a autoload_real.php file by default which references a ClassLoader class and checks for psr-0.
These files can be found in the vendor/composer directory.
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/"
}
I'm new to Laravel, and I'm trying to autoload an entire directory of my own classes. The name of my directory is "templates" so, based on this post in the Laravel forum, I have added my "templates" directory to app/start/global.php like this:
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/templates',
));
I then ran composer dump-autoload successfully, but my classes still aren't found. What am I missing?
You may add the directory in composer.json file (in autoload->classmap):
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/controllers/admin",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/templates" // <--------
]
},
Then run (From your project's root folder on terminal/command prompt):
composer dump-autoload
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!
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]
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!