Use package installed via composer in Laravel - php

I have installed library called chosen js via composer. Now I have it in my composer.json file
"require": {
"harvesthq/chosen": "^1.8"
},
How can i use it in my project? Where do I need to require it ?

Install any packages in the Laravel
(1)first way: via composer
open project path in terminal and fire this command
composer require harvesthq/chosen
(2)Second way: add package name in composer.json file
"require": {
"harvesthq/chosen": "^1.8"
},
and fire this command
composer update

Related

How to run scripts after a user installs my package via Composer?

I use the command composer require otra/otra:dev-develop --no-update --no-cache && composer update --no-autoloader to install my own framework.
I have put this section in my composer.json from my framework :
"scripts": {
"pre-install-cmd": "#composer config bin-dir bin/"
}
But Composer does not run it. Is this normal, does Composer consider this as a dependency and not the root package so it does not allow my script to run?
If this is the case, how can I have the same behaviour?
I want to :
have my binary in the bin folder, not vendor/bin without having to ask the user to do a symlink manually (or another solution)
copy a web folder from my framework to the root of the project.
Edit : with create-project command
If I type composer create-project otra/otra:dev-develop crashtest --remove-vcs, I get this composer.json :
{
"name": "otra/otra",
"type": "library",
"description": "The OTRA PHP framework",
"keywords": ["framework"],
"homepage": "https://github.com/lperamo/otra",
"license": "X11",
"authors": [
{
"name": "Lionel PĂ©ramo",
"email": "contact#lionel-peramo.com",
"homepage": "https://wexample.com"
}
],
"bin" : ["otra.php"],
"config": {
"bin-dir" : "bin/",
"sort-packages": true
},
"require": {
"ext-mbstring": "*",
"php": ">=7.4.0",
"symfony/yaml": "^4.0"
},
"require-dev": {
"ext-pdo": "*",
"ext-pdo_mysql": "*"
},
"scripts": {
"pre-install-cmd": "#composer config bin-dir bin/"
}
}
which is exactly the same as my framework so I cannot update it via Composer. I could with git if I do not use --remove-vcs but it is not the goal.
The output of the composer command is :
Installing otra/otra (dev-develop ab37237565155dab11812a7b2982d30ee240f051)
Installing otra/otra (dev-develop ab37237): Cloning ab37237565 from cache
Created project in crashtest
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Only scripts defined in the project's proper composer.json file are executed.
Scripts from required and installed packages are never executed because it would be terrible safety risk.
This is summarily explained in the docs:
Only scripts defined in the root package's composer.json are executed. If a dependency of the root package specifies its own scripts, Composer does not execute those additional scripts.
If your package users need to perform additional steps to use your package or library, explain those steps in your package documentation, and or provide scripts that they can execute manually and will perform those steps for them.
If your package is a "framework", as opposed to a library, what you could do is take advantage of composers create-project command.
That would require you to setup are repository with the default structure for a project, which would in turn depend on your package.
That's the way it's done with Symfony's Skeleton, for example.
With this kind of setup, you can create custom installation scripts and activate them with the post-create-project-cmd, and do some some extra setup steps, even interactive one, with something like this. (docs)
Be mindful that this script would only run when the package is installed using create-project, and never when using require.
Nobody mentioned, this can be achieved by creating a composer plugin and defining event handler for event post-package-install

Trouble installing Clickatell Php Library

I'm trying to install the clickatell php library after this instruction:
https://github.com/arcturial/clickatell
But so far i was only able to install composer.
They say: this library uses composer and can be acquired using the following in your composer.json file.
{
"require": {
"arcturial/clickatell": "*"
}
}
So I have to add this to the composer json file? Then require the json file in my php script? I've no idea what to do next. Any help would be great.
When you are in your project root, run the following:
create a composer.json file with the following contents:
{
"name": "your/project",
"description": "project description",
"require": {
"arcturial/clickatell": "*"
}
}
php -r "readfile('https://getcomposer.org/installer');" | php
This will download a file called composer.phar
php composer.phar install
This will install the dependencies you specified in your composer.json file. Once complete, your project will now have a folder called "vendor".
Include the "vendor/autoload.php" file in your script
require_once __DIR__ . '/vendor/autoload.php';
...
Now you should be able to use the library as specified in the documentation.

install Slim3 beta with composer?

how can I install Slim3 beta with composer?
I tried --dev with no luck
The official docs say nothing and composer search revealed nothing.
Run the following command from from commandline.
$ composer require slim/slim:~3.0#dev
Another option is to create a composer.json with the following content.
{
"require": {
"slim/slim": "~3.0#dev"
}
}
After creating the file in the same folder run
$ composer install

Install PHPUNIT with Composer

I have project on Symfony 2 and i would like use PHPUNIT on Windows 7.
On githut phpunit is:
Composer
Simply add a dependency on phpunit/phpunit to your project's composer.json file if you use Composer to manage the dependencies of your project. Here is a minimal example of a composer.json file that just defines a development-time dependency on PHPUnit 3.7:
{
"require-dev": {
"phpunit/phpunit": "3.7.*"
}
}
For a system-wide installation via Composer, you can run:
composer global require 'phpunit/phpunit=3.7.*'
Make sure you have ~/.composer/vendor/bin/ in your path.
First i use system-wide installation but i dont know when this installed.
Next i add to my composer.json require-dev.
This installed phpunit in C:/wamp/www/myproject/vendor/symfony. Next i try commands:
composer install --dev
And i can't use phpunit. In cmd.exe i enter "phpunit" and i have error:
'phpunit' is not recognized as an internal or external command operable program or batch file
How can i use phpunit? I have Windows 7, Wamp server and php 5.4.12.
When you install PHP-Unit in windows via composer, the global installation will create files in
C:\Users\YOUR_USERNAME\AppData\Roaming\Composer
To execute phpunit easily via command line you need to add path of phpunit.bat file in windows Environment Variables. For this:
Right click My Computer
Go to Properties -> Advance system settings and
Click Environment variables from the Advance tab.
Now add C:\Users\YOUR_USERNAME\AppData\Roaming\Composer\vendor\bin to the windows PATH.
You can now run the phpunit from command. Note that you may need to restart your command prompt for the changes to take effect.
The bin file of packages are put in the configured bin directory. By default, this is vendor/bin and when you use the symfony standard edition, this is the bin folder.
To execute this bin file, run ./bin/phpunit (or ./vendor/bin/phpunit when not using the Symfony Standard Edition)
Windows users have to put this in double quotes: "bin/phpunit" (or "vendor/bin/phpunit")
composer require --dev phpunit/phpunit ^7
The above example assumes, composer is already on your $PATH variable.
You composer.json should look similar to;
{
"name": "vendor_name/package_name",
"description": "This project is for practicing writing php unit tests",
"minimum-stability": "stable",
"license": "proprietary",
"authors": [
{
"name": "Umair Anwar",
"email": "umair.anwar#gmail.com"
}
],
"autoload": {
"classmap": [
"src/"
]
},
"require-dev": {
"phpunit/phpunit": "^7",
"phpunit/dbunit": "^4.0"
}
}
Easiest way to install phpunit via composer is to run from project root.
$ composer require phpunit/phpunit
What this would do is, it will create a phpunit folder inside vendor/bin
and you can run unit tests like this..
$ ./vendor/bin/phpunit
Too simple operation on Windows with composer and works for me following way:
Install composer
https://getcomposer.org/doc/00-intro.md#installation-windows Go to
your symphony folder e.g C:\wamp64\www\symfony\UserManagement where is
composer.json and run this command.
Should be register with global to not have issue $phpunit bash: phpunit: command not found
//old version is 5.7 new 6.4 or put newest version.
composer global require --dev phpunit/phpunit ^5.7
I remember futzing around with the composer dependency stuff for phpunit and never could get it to work.
Instead, from your git bash shell:
mkdir ~/bin
cd ~/bin
curl https://phar.phpunit.de/phpunit.phar > phpunit
chmod +x phpunit
exit out of bash and then start a new bash session.
And you should be good to go. You can echo $PATH to verify you have a path to ~/bin but one seems to added by default.
https://phar.phpunit.de/phpunit.phar
I also came across the same issue and find out the solution by following steps
To run PHPUnit in Windows 7 under WAMP installation
Composer Install
{
"require-dev": {
"phpunit/phpunit": "3.7.*"
}
}
Simply Set Environment Variable
The php unit will be install in a vendor dir in vendor/bin
Path : C:\wamp\www\myproject\vendor\bin;
Open a new Command Prompt
C:\Users\guny >phpunit --version
PHPUnit 3.7.30 by Sebastain Bergmann

how to install the doctrine mongodb odm via composer

I am working on a project with the php and mongodb stacks. I need the assistance of an ODM to speed up development time. A problem I have now however is this line,
$ composer require "doctrine/mongodb-odm=~1.0"
in which directory should i execute this command, the application's root directory or anywhere in my command line???
Copy the following to composer.json
{
"require": {
"doctrine/mongodb-odm": "~1.0",
"doctrine/mongodb-odm-bundle": "~3.0"
},
}
after that run the command in respective folder
php composer.phar update doctrine/mongodb-odm doctrine/mongodb-odm-bundle

Categories