Composer dump-autoload, issue - php

While Working on a project using Laravel 4 to be precise, I decided that i wanted to make my own helper file to house my custom functions.. one of which is this below...
function pr($ar=array(), $bool=false){
echo '<pre>';
print_r($ar);
echo '</pre>';
if($bool){
exit;
}
}
in my composer.json file, just after the autoload: classmap , i added myne, autoload:files -arrar and included my custom file, app/helpers as illustrated below..
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"others":[
"app/helpers.php"
]
and i switched to my terminal window and ran the following commands
composer dump-autoload -o
but i still got errors that my pr() function was undefined... then i tried the artisan alternative... [-o ] to optimize the files
php artisan dump-autoload
but still it refused to work... and then i changed the array name from
"others":[
"app/helpers.php"
]
to
"files":[
"app/helpers.php"
]
then i got the desired response, my code could now see the custom function i wrote, please i'd like to know if there is a pattern i was supposed to follow or otherwise, in my case, i mistook " files ", for " others " and i got errors, but incase, what did i miss here, all i see is just a name-string value for the array representation....

This is how composer works. In autoload section you need to use files when you want to load some files. In my Laravel 5 project I have for example:
"autoload": {
"classmap": [
"database",
"tests/TestCase.php"
],
"psr-4": {
"App\\": "app/",
"verify\\": "verify/"
},
"files": [
"app/Helpers/functions.php"
]
},
If you look at documentation you will see that you need to use files to load any extra files by autoloader.

According to the official documentation
Currently PSR-0 autoloading, PSR-4 autoloading, classmap generation
and files includes are supported. PSR-4 is the recommended way though
since it offers greater ease of use (no need to regenerate the
autoloader when you add classes).
So the reason that "others" did not work was because it is not supported by composer. "others" is simply meaningless, while "files" actually have a specific autoloading mechanism.

Related

How do I change the namespace of my application in Laravel?

I can't change the namespace of my application in Laravel 5.8.
I'm using artisan to change it:
php artisan app:name TestApp
Result is: There are no commands defined in the "app" namespace.
Looking at php artisan you should have a php artisan app:name NewNamespace
command to change the namespace. Make sure that you are on the latest laravel version.
Old answer
To change the namespace of your app you have to edit the composer.json file:
"autoload": {
"psr-4": {
"CustomNamespace\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
but you also have to edit each file in the app subfolders and in the various configurations files (for example in config/app.php, config/auth.php, etc).
After you have done all of that you can run: composer dump-autoload
Keep in mind that this is an error prone method, because if you forget to replace a namespace in any of your files anything can stop working as expected.
Another option would be to create a custom package with the name you want and register it with a custom namespace. For example:
Create a lib/yourpackage/src folder in the root of laravel installation
Edit the composer.json file to load your custom code:
"autoload": {
"psr-4": {
"App\\": "app/",
"CustomNamespace\\": "lib/yourpackage/src/",
},
"classmap": [
"database/seeds",
"database/factories"
]
},
Run composer dump-autoload as before
You can now use anywhere your files in your project by referring to the custom namespace you have choosen.
laravel change this command to app:namespace
you have to do this
php artisan app:namespace TestApp
Are you trying to change the namespace of your application or just the name ?
What do you want to change exactly in the namespace ?
EDIT
I think you must have touched something in your application because the command php artisan app:name AppName should work, I just tested it.
Have you ever tried before to change the namespace by yourself?
Otherwise, try composer dump-autoload before to make sure your autoloading is up to date.
This feature has been removed as of Laravel 6. It's not recommended to change the namespace.
However if you want to do it, you can add the command to your Console/Commands directory
https://gist.github.com/isluewell/b824c0aef32f5007170fcd0d8498b657

Laravel 5.7 gRPC Integration

I have setup gRPC in core PHP and it is working fine but I want to use it with laravel 5.7. I have downloaded gRPC using composer.json. Now I don't know what I have to do for using gRPC in it.
I have added below in my composer.json file
"require": {
"datto/protobuf-php": "dev-master",
"grpc/grpc": "^1.15.0"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/stanley-cheung/Protobuf-PHP"
}
]
But now I am not getting what should be next to do. So anyone knows then please let me know. I have my core PHP code for gRPC(core library example) which I have setup from this web url. gRPC in PHP. So I just want to integrate into laravel.
How to use protoc generated files with Laravel (5.7)
Prepare your .proto file describing your services as you would normally do
Set as package name package app.grpc;
Run the generation command: protoc --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=/your/path/to/grpc_php_plugin yourfile.proto
Now copy App/Grpc inside laravel App
Copy GPBMetadata folder as new folder in laravel root
Change composer.json file inserting GPBMetadata location in the autoload classmap section (see below)
Run command composer dump-autoload
Now you can use your generated classes in any controller, remember to add use App\Grpc\YourClassName at the top.
let me know if someone have better solutions to this
Relevant part of composer.json:
...
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"GPBMetadata"
],
"psr-4": {
"App\\": "app/"
}
}
...
you don't need these anymore
"datto/protobuf-php": "dev-master",
"repositories": [
{
"type": "vcs",
"url": "https://github.com/stanley-cheung/Protobuf-PHP"
}
]
Instead, you should add "google/protobuf": "^3.6.1" to your require.
See this as an example:
https://github.com/grpc/grpc/blob/master/examples/php/composer.json
The versions were out-of-date - just take the latest releases.

Composer runs locally but errors on production

I'm working within the Laravel framework, and have a file loading successfully, pulled in through my Composer's files array. I can run composer update locally without issue -- I'm using the functions in the file.
However, when deploying to Digital Ocean, Composer throws an error, suggesting that it's looking for the file in the vendors directory (even though it's not looking there locally).
Fatal error: composerRequire0b4716b00b8bec4a70dbf5ea5e415661(): Failed
opening required
'/home/forge/myapp.com/vendor/composer/../../app/helpers/myHelper.php'
(include_path='.:/usr/share/php') in
/home/forge/myapp.com/vendor/composer/autoload_real.php on line 66
And the autoload section:
"autoload": {
"classmap": [
"database"
],
"files": [
"app/helpers/myHelper.php"
],
"psr-4": {
"App\\": "app/"
}
},
I think this question probably has what I need but I don't know if the difference between loading classes and files makes it irrelevant to this issue (clearly I'm confused about more than just this issue!):
How to I use Composer to autoload classes from outside the vendor?
try to delete all text in composer.lock and update composer

Adding Third party library to Laravel

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/"
}

How to autoload classes without namespaces with Composer without reinstalling?

I just need to autoload some classes, and I don't like the psr-0 namespace insanity (no offense).
This used to work just fine in my project:
"psr-0": {
"": [
"app/controller/",
"app/model/"
]
}
For some reason it doesn't work anymore, even though I'm using the same Composer version. I need it for a new project that is also using Silex. Could this be a conflict with Silex?
I know about the "classmap" option, but it's kind of useless because it requires that I run "composer install" every time I add a new class.
Any ideas?
Try to use "primitive" JSON properties; not an array (like in your example).
This works for me with psr-4 like you say, with "": "app/":
{
"autoload": {
"psr-4": {
"Robbie\\": "core/",
"": "app/"
}
},
"require": {
"monolog/monolog": "1.2.*"
}
}
This gives me the Robbie namespace under the directory core, as an example of sources not controlled by composer, the 3rd party (vendor) Monolog namespace and my default or non-namespace for sources underneath the app directory.
After a composer update, all of them are available when including the generated autoload.php:
<?php
require_once 'vendor/autoload.php';
// ...
?>
Use classmap in instead of psr-4:
"autoload": {
"classmap": ["models/"]
}
If you just want to regenerate the autoload file use composer dump-autoload.

Categories