So I have a helper file: App\helpers.php, added in my JSON as follows:
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
},
"files": [
"app/helpers.php"
]
},
In this helper file I have for example a simple userName() method for displaying the full name of a user:
function userName()
{
return auth()->user() ? auth()->user()->present()->name : '';
}
Everything works fine in my local environment. But everytime I push to my production environment, I get errors:
Call to undefined function userName()
Whenever in Laravel Forge I do a composer update after deploying, the error disappears and my website works flawlessly.
I have never had this issue with any other projects, and I am using the helpers.php file the exact same way.
My question is, why is this happening and of course my second question would be, how to solve this?
if you look at forge deploy script, you'll see the line:
$FORGE_COMPOSER install --no-dev --no-interaction --prefer-dist --optimize-autoloader
the --no-dev flag will skip your autoload-dev rules, so you just need to move the helper reference into autoload instead of autoload-dev
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
"files": [
"app/Helpers.php"
]
},
Related
I am trying to run a seeder in a lumen 8 project but have been unsuccessful for a while now. Here's the folder structure view
When I run php artisan db:seed --class="Database\Seeds\SampleTableSeeder" or php artisan db:seed --class="Database\Seeders\Seeds\SampleTableSeeder" or php artisan db:seed --class=SampleTableSeeder it fails and returns a target class not found like
Target class [Database/Seeds/SampleTableSeeder] does not exist.
I have added the namespace to SampleTableSeeder to look like
<?php
namespace Database\Seeds;
use App\Models\Sample;
class SampleTableSeeder
{
// ....
}
And in DatabaseSeeder run function, I am calling it like this
$this->call('Database/Seeds/SampleTableSeeder');
What is the right way to run this particular seeder?
Database/Seeds will never work, because you have not declare it on the composer.json section...
See the original file, it has this section:
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
}
You have to declare that new section:
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/",
"Database\\Seeds\\": "database/seeds/"
}
},
}
Then it will work, but I personally do NOT recommend to do that... you already have Database/Seeders...
Quick update, if running php artisan db:seed --class="Database\Seeds\SampleTableSeeder" does not work, try adding \ at the beginning, like this php artisan db:seed --class="\Database\Seeds\SampleTableSeeder"
I just created a new 2.8 Symfony project then added SonataAdminBundle
when I tried to add SonataMediaundle as you can see in the screenshot after completing step 2.5 in the documentation I get this error that I just couldn't explain.
(I'm a new user so I can't add pictures yet so you'll only see the link)
first of all, Cerad, the problem solved thanks to you,
by changing the autoload in my composer.json file from this:
...
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle"
},
To this:
...
"autoload": {
"psr-4": {
"": "src/"
},
then run command:
composer dumpautoload
and VoilĂ everything come back to work.
i try to create a bundle by console with symfony 3.3.8 after this version i dont have any problem with that but now i have one,
image1
image2
Any suggestion please!!!!!!
In your composer.json
replace your autoload part with:
"autoload": {
"psr-4": {
"": "src/"
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
thinks i also execut the command bellow
composer dump-autoload.
Problem resolve.
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 added this to my composer.json file
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Claremontdesign\\Cdbase\\": "packages/Claremontdesign/cdbase/src",
"Claremontdesign\\Narbase\\": "packages/Claremontdesign/narbase/src",
"Claremontdesign\\Nhr\\": "packages/Claremontdesign/nhr/src"
},
"files": [
"packages/Claremontdesign/cdbase/src/Helpers/helpers.php",
"packages/Claremontdesign/narbase/src/Helpers/helpers.php",
"packages/Claremontdesign/nhr/src/Helpers/helpers.php"
]
},
then, I ran composer update from the command line, and it gave me this error:
symfony component debug exception fatalerrorexception class "Claremontdesign\Cdbase\ServiceProvider" not found
Has anyone else encountered this?
Also, I added a service provider in add.php
Claremontdesign\Cdbase\ServiceProvider::class
Did you try running just composer dump? composer update runs some scripts before it actually runs - for example php artisan clear-compiled. When artisan runs, it'll probably fail because it tries to register the serviceprovider which isn't autoloaded yet. composer dump only generates the autoload files, which is what you need in this case.
i think you are missing one more slash after every src folder
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Claremontdesign\\Cdbase\\": "packages/Claremontdesign/cdbase/src/",
"Claremontdesign\\Narbase\\": "packages/Claremontdesign/narbase/src/",
"Claremontdesign\\Nhr\\": "packages/Claremontdesign/nhr/src/"
},
"files": [
"packages/Claremontdesign/cdbase/src/Helpers/helpers.php",
"packages/Claremontdesign/narbase/src/Helpers/helpers.php",
"packages/Claremontdesign/nhr/src/Helpers/helpers.php"
]
},