I have created a project in Laravel 5.7 and created the file constants.php in config directory. When I tried to run php artisan config:cache, I am getting the following error.
"ErrorException : Constant XXX_XXX already defined"
I have defined all the constants as define('XXXX_XXXX', 'bla bla'); All these are standard constants needed for my package.
I have done the following:
I have searched the whole project and it is clear that I have never defined the same anywhere else.
If I remove that constant, it shows error at the next define().
If I remove the file from config directory, the artisan command gets executed successfully and I can see the config cache file in bootstrap/cache directory. but my program aborts due to error (as constants are not defined) even after I paste back the file in config directory.
If I remove the config cache file from the bootstrap/cache directory, the code gets executed perfectly normal ( i have not ran the command config:cache).
I very much need this constants and the same time cache the configs to run. I am not able guess the problem.
Note : Contents of config/contants.php all are define('XXX', 'xxx'); Simply echo XYZ; wherever needed. Standard php constants.
try this way
define constant value below
if (!defined('constant')) define('constant', 'value');
I am not sure whether it is correct but the way it worked is as follows:
I moved the constants.php from App/config/ to App/ (along with other models)
I modified the composer.json in the "autoload" with
"files": [
"app/constants.php"
]
I ran composer dump-autoload
Then I ran the php artisan config:cache
It had build the new config file in bootstrap and the code is executing normal.
i have a great solution for this. please follow this step :
1 -> add new file constantHelper.php inside directory app/Helpers
2 -> edit file composer.json and set auto load files
{
"autoload": {
"files": [
"app/Helpers/constantHelper.php"
]
},
"autoload-dev": {
"files": [
"app/Helpers/constantHelper.php"
]
}
}
this is my complete code inside composer.json
{
"name": "nurzazin/lumen",
"description": "The Laravel Lumen Framework.",
"keywords": [
"laravel",
"lumen"
],
"license": "MIT",
"type": "project",
"require": {
"php": "7.3.*",
"ext-json": "*",
"ext-openssl": "*"
},
"require-dev": {
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^7.0",
"wn/lumen-generators": "^1.3"
},
"autoload": {
"files": [
"app/Helpers/constantHelper.php"
],
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"files": [
"app/Helpers/constantHelper.php"
],
"classmap": [
"tests/"
]
},
"minimum-stability": "dev",
"prefer-stable": true
}
3 -> run command composer install or composer dump-autoload
Related
I have wamp64, php 7.4 and using composer I ran this from the console
composer create-project --prefer-dist "cakephp/app:^4.0" myapp
and
composer create-project --prefer-dist cakephp/app:~4.0 my_app_name
The installation cut out towards the end with this error
Script App\Console\Installer::postInstall handing the post create project cmd event terminated with an exception
[Symfony\Component\Console\Exception\RunTimeException\ Aborted
and some Symfony runtime exception when you get to the set folder permission y/n
It created this JSON file
{
"name": "cakephp/app",
"description": "CakePHP skeleton app",
"homepage": "https://cakephp.org",
"type": "project",
"license": "MIT",
"require": {
"php": ">=7.2",
"cakephp/cakephp": "~4.2.0",
"cakephp/migrations": "^3.0",
"cakephp/plugin-installer": "^1.3",
"mobiledetect/mobiledetectlib": "^2.8"
},
"require-dev": {
"cakephp/bake": "^2.3",
"cakephp/cakephp-codesniffer": "~4.2.0",
"cakephp/debug_kit": "^4.4",
"josegonzalez/dotenv": "^3.2",
"phpunit/phpunit": "~8.5.0 || ^9.3",
"psy/psysh": "#stable"
},
"suggest": {
"markstory/asset_compress": "An asset compression plugin which provides file concatenation and a flexible filter system for preprocessing and minification.",
"dereuromark/cakephp-ide-helper": "After baking your code, this keeps your annotations in sync with the code evolving from there on for maximum IDE and PHPStan/Psalm compatibility.",
"phpstan/phpstan": "PHPStan focuses on finding errors in your code without actually running it. It catches whole classes of bugs even before you write tests for the code."
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Test\\": "tests/",
"Cake\\Test\\": "vendor/cakephp/cakephp/tests/"
}
},
"scripts": {
"post-install-cmd": "App\\Console\\Installer::postInstall",
"post-create-project-cmd": "App\\Console\\Installer::postInstall",
"check": [
"#test",
"#cs-check"
],
"cs-check": "phpcs --colors -p src/ tests/",
"cs-fix": "phpcbf --colors -p src/ tests/",
"stan": "phpstan analyse",
"test": "phpunit --colors=always"
},
"prefer-stable": true,
"config": {
"sort-packages": true
}
}
How to Fix Cakephp 4 Composer Install Error
(i didnt understand the solution to this)
Cakephp 4 Windows Installation Issues
It should be possible to get it to work. What Windows version do you use? Pro or Home? Which distribution? 2010?
Please make sure to navigate to the WAMP install folder and then specifically the www folder.
The command I use is this;
composer create-project --prefer-dist cakephp/app:~4.0 cake
It worked for me.
Alternatively, download Cakephp version 4.0, place the contents in the www folder, and configure it manually through the file /config/app.php. If you want, we can look at it together. There are not many people around me that can or want to work on Cakephp, so it'd be a pleasure. :)
acknowledgement:
First of all this is my first post for help, as i went trough a lot of articles on stackoverflow and other resources and just could not find solution.
Problem sounds familiar but i just can't figure it out, maybe because im stuck on it for 2 days now. Another note, this is my first package so yes im not 100% sure what im doing.
Structure:
- laravel-project
- ...
- packages
- my-custom-package
- src
- MyCustomPackageServiceProvider.php
- composer.json
- public
- resources
- routes
- ...
- composer.json
So it is basic Laravel 5.8 + inside added package folder for local package devolment (it has been moved to repo as well)
Current Code (simplified as its actual work project):
File: laravel-project/packages/my-custom-package/composer.json
{
"name": "company/my-custom-package",
"type": "library",
"require": {
"php": ">=7.2",
},
"require-dev": {
"ext-curl": "*"
},
"authors": [
{
"name": "John Smith",
"email": "jsmith#test.tv"
}
],
"autoload" : {
"prs-4": {
"Company\\MyCustomPackage\\": "src/"
}
},
"minimum-stability": "dev"
}
File: laravel-project/packages/my-custom-package/src/MyCustomPackageServiceProvider.php
<?php
namespace Company\MyCustomPackage;
use Illuminate\Support\ServiceProvider;
class MyCustomPackageServiceProvider extends ServiceProvider {
public function boot()
{
$this->loadRoutesFrom(__DIR__ . '/routes/web.php');
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
}
public function register()
{
}
}
File: laravel-project/composer.json
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"repositories": [
{
"type": "path",
"url": "~/testlaravel/packages/my-custom-package",
"options": {
"symlink": true
}
}
],
"require": {
"php": "^7.1.3",
"fideloper/proxy": "^4.0",
"laravel/framework": "5.8.*",
"laravel/tinker": "^1.0",
"guzzlehttp/guzzle": "^6.3.3",
"company/my-custom-package": "dev-master"
},
"require-dev": {
"beyondcode/laravel-dump-server": "^1.0",
"filp/whoops": "^2.0",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^3.0",
"phpunit/phpunit": "^7.5"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
"autoload-dev": {
"prs-4": {
"Company\\MyCustomPackage\\": "~/testlaravel/packages/my-custom-package/src"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover --ansi"
],
"post-root-package-install": [
"#php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"#php artisan key:generate --ansi"
]
}
}
Explanation: ~/testlaravel/ is folder path inside Vagrant machine for project originaly url was "packages/my-custom-package" buti changed for this extended version just in pure attempt of making it work.
File: config/app.php (added service provider)
Company\MyCustomPackage\MyCustomPackageServiceProvider::class
Issue:
First i made this thing localy and it worked i was following for quick setup instructions from
"Create Laravel Composer Package from scratch to upload on packagist" Author: Bitfumes, and package worked, I could run composer update, then i added to app.php my package and i could run custom route and it all worked.
After that i moved package to git repo, and required it in same way only changed "repositories" field to
"repositories": [
{
"type": "vcs",
"url": "git#git.fulllink.co:john/my-custom-package.git"
}
]
and when you run composer update, you can see that afterwards inside vendor/ files you have Company folder with package inside it, BUT if you leave config/app.php service provider it starts to complain that there is
*Class 'Company\MyCustomPackage\MyCustomPackageServiceProvider' not fo und*
After some f*** around i found out that yes my autoload_classmap (cant remember original post) did not contain my package , so today i decided to try again local and see the difference, does it been added to map file, and now i get same error when requiring local package and i can't make it work anymore.
I been deleting vendor folder and lock file , composer dump-autoload, but that doesnt help. Im just thinking is thgere anything else supercached?
!!UPDATE!!
Im not even more confused. In composer.json (project one i tested on actual project repo now where is required another 2 local git repos). So i have now 4 packages in vendor/Company/{4 packages} , i compared all 4 package composer.json and i can't find any major difference but all of them have been loaded to autoload mapping except mine. Only major difference i can see, those two packages doesnt use ServiceProvider.php file , but i dont see how that impacts or changes things.
I am trying to manage branch with version number to have a control of the code. So I have a branch "0.7" and from this I create another branch "0.7.1" with a lot of changes.
I'm trying to install in other project my package with "0.7.1" branch code, and as the composer documentation say, the way to do this is define the jida dependency as "0.7.x-dev". But when I run "composer update" composer only download the 0.7 version and not the latest. I tried a lot of things but i can't download the 0.7.1 version. I want to fix this because my idea is later make a 0.7.2 versions and keep like that.
{
"type": "website",
"minimum-stability": "dev",
"license": "mit",
"authors": [
{
"name": "jr0driguez",
}
],
"require": {
"phpmailer/phpmailer": "^5.2",
"ext-json": "*",
"ext-gd": "*",
"jida/framework": "0.7.x-dev",
"ext-curl": "*"
},
"autoload": {
"psr-4": {
"App\\": [
"./Aplicacion/"
]
}
},
"scripts": {
"post-install-cmd": [
"npm install"
],
"post-update-cmd": [
"npm update"
]
}
}
I put an image with composer message where shows the ""
If your branch name is 0.7.1, you should use 0.7.1.x-dev as a constraint:
"jida/framework": "0.7.1.x-dev",
I resolved the trouble adding the "repositories" property into the composer.json from the project where I was installing the dependency.
This was the part:
{
"type": "git",
"url": "https://github.com/jidadesarrollos/jida",
"name": "jida/framework"
}
I set the "name" property to cofirm the dependency name, I think must be the same. And with this I could specify the branch name that I was needing to install
"jida/framework": "dev-desarrollo",
The branch where I'am working. I hope the post be useful to another.
I am building a website with Laravel 5.
Laravel is using Composer as dependency management.
Whenever I run composer dump-autoload the autoload classes are generated.
Unfortunately the path in two of these files are wrong.
I am using one Seed class and some migrations within Laravel which is located in database/seeds respectively database/migrations.
The generated paths to all classes (migrations, seeds) below the database folder is wrong.
The path looks like $baseDir . '/atabase/migrations/2017_02_02_153131_create_cards_table.php'
As you can see the "d" from "database" is missing.
As far as I know the autoload classes are defined in the composer.json file.
But here it seems to be correct:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
}
The migration is working, but the seeds are not.
When I do a search&replace and correct the path everything is fine unless I do composer dump-autoload
I am developing on a Windows 10 machine with Laravel 5.4.8 and Composer 1.3.2.
Edit:
Here is the output of composer dump-autoload -vvv
PS C:\Users\phoef\Documents\Projekte\CardViewer> composer dump-autoload -vvv
Reading ./composer.json
Loading config file ./composer.json
Checked CA file C:\Users\phoef\AppData\Local\Temp\composer-cacert- 300c74d6960d46715ffed607c8e36acfa7cbcad49862bb1b67d3c43dea9bfb40.pem: valid
Executing command (C:\Users\phoef\Documents\Projekte\CardViewer): git branch --no-color --no-abbrev -v
Reading C:/Users/phoef/AppData/Roaming/Composer/composer.json
Loading config file C:/Users/phoef/AppData/Roaming/Composer/composer.json
Reading C:\Users\phoef\Documents\Projekte\CardViewer/vendor/composer/installed.json
Reading C:/Users/phoef/AppData/Roaming/Composer/vendor/composer/installed.json
Running 1.3.2 (2017-01-27 18:23:41) with PHP 7.1.1 on Windows NT / 10.0
Generating autoload files
The two files containg the wrong path are: autoload_classmap.php and autoload_static.php.
Edit 2:
Please find below the complete composer.json
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.4.*",
"laravel/tinker": "~1.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~5.7"
},
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"scripts": {
"post-root-package-install": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"php artisan key:generate"
],
"post-install-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postInstall",
"php artisan optimize"
],
"post-update-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan optimize"
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true
}
}
The seeds and migrations live in the database/ directory (from project root), not inside app/. Do you'll have database/seeds/CardsTableSeeder.php, for example.
Did you create them using artisan?
php artisan make:migration create_cards_table
php artisan make:seed CardsTableSeeder
It's not required to do it this way but it's handy to be sure you get them in the right directory.
Here's my autoload, you're missing the psr-4 key.
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
}
I have noidea why it's doing atabase, sorry
You are doing something wrong. You need to reset project and start from start.
I am using laravel-analytics (https://github.com/spatie/laravel-analytics/) and have installed everything locally, works fine.
Whenever I try to upload to the server, however, I get the following error visiting the page: Class 'LaravelAnalytics' not found and I am trying to access it via: use \LaravelAnalytics as Analytics;
I am using both linux operating systems, both locally and on the server. I have also ran: composer update and now get: Nothing to install or update
I have checked the .json file and I can see the package I am trying to install is there.
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.1.*",
"illuminate/html": "5.*",
"spatie/laravel-analytics": "^1.2"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~4.0",
"phpspec/phpspec": "~2.1"
},
"autoload": {
"classmap": [
"database"
],
"files":
["app/Http/helpers.php"],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"pre-update-cmd": [
"php artisan clear-compiled"
],
"post-update-cmd": [
"php artisan optimize"
],
"post-root-package-install": [
"php -r \"copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
}
}
Anyone have any ideas to what I am missing or where I am going wrong?
I faced a similar problem. Uploading the vendor/composer folder worked for me !
Being myself confronted with this problem, I first would like to thank riza who put me on the path.
According to the documentation, after installed the service provider and facade, we have to do this command :
php artisan vendor:publish --provider="Spatie\Analytics\AnalyticsServiceProvider"
Which causes the following error : Class 'Spatie\Analytics\AnalyticsServiceProvider' not found
If we see into vendor/spatie/laravel-analytics/src, we can see that the required classes contain Laravel string at the beginning of their names.
Solution :
So to solve this error, we need to change the call made from the config/app.php file :
providers section. Change this :
Spatie\Analytics\AnalyticsServiceProvider::class,
by :
Spatie\LaravelAnalytics\LaravelAnalyticsServiceProvider::class,
aliases section, change this :
'Analytics' => Spatie\Analytics\AnalyticsFacade::class,
by :
'Analytics' => Spatie\LaravelAnalytics\LaravelAnalyticsFacade::class,
Then, run again the following command, to make it work (added Laravel string too) :
php artisan vendor:publish --provider="Spatie\LaravelAnalytics\LaravelAnalyticsServiceProvider"
Then go to config/ folder. You should see a file called laravel-analytics.php.
I had similar problem when I created own package. PHP Storm was finding class but when scripts started then status 500 was shown. The reason was simple - be sure that your package from vendor include in it's composer.json
"autoload": {
"classmap": [
"src/"
]
}
Then it will be autoloaded.
Change your psr-4 to this:
"psr-4": {
"App\\": "app/",
"vendor\\spatie\\" : "spatie/"
}
hope this work for you.
You have to add the class to the aliases (or use the FQCN):
// config/app.php
'aliases' => [
...
'LaravelAnalytics' => 'Spatie\LaravelAnalytics\LaravelAnalyticsFacade',
...
];
"require: {
"php": ">=5.5.9",
"laravel/framework": "5.1.*",
"illuminate/html": "5.*",
"spatie/laravel-analytics": "^1.2"**
}
Seems that you are using the php 5.* and that's why composer pulling the 1.2 version of the package which has different class.
I got the same problem a month ago
The read me file that you read to setup package is for 2.4 version which is for php 7.0
Just update your php 5.* to php 7.* or get the read me file of 1.2 version of package to setup package correctly
For details see my blog: Class not found error due to php version mismatch