Putting composer.json in the root of a CakePHP installation - php

My current website installation looks like this:
.htaccess
app/composer.phar
app/composer.json
app/Config
app/Controller
app/Model
app/Plugin
app/Test
app/tmp
app/Vendor (CakePHP and most other composer packages get installed here)
app/webroot
I would quite like the composer files to be in the root of the structure. I know I can change vendor-dir in composer.json to app/Vendor however the issue I am having is that some of my required packages are of the cakephp-plugin so they automatically get installed to the Plugin folder - this is working fine for the above folder structure. But if I move composer.json to the root, I am unsure how to make them go to app/Plugin without defining the path for each package in composer.json to override where composer would want to install to. What is the correct way to do this?

It looks like you just need to add this to your composer.json. I haven't tested it.
{
"extra": {
"installer-paths": {
"app/Plugin/{$name}/": ["type:cakephp-plugin"]
}
}
}
More info: https://github.com/composer/installers#custom-install-paths

Related

Composer.json to autoload classes

So I was following this tutorial on how to autoload classes, the guy teaching it already had Composer installed, he went ahead and created a composer.json file in his project directory with the following content:
{
"autoload": {
"classmap": [
"./"
]
}
}
which supposedly autoloads any classes within the main project directory. And i cannot do this because if if i create a composer.json that is located anywhere but inside located in C:/Users/Mypc, the composer.json file cannot be found, i cannot install it in command line wich is the tutorial's step, composer install creates his autoloading. My autoloader cannot seem to find any classes inside any path I create in the class map, be it absolute or relative, I also managed to install and move composer file to usr/local/bin/composer but that's all I can do.
I can't create an autoloader no matter what I do and I also can't move the composer.json to my project directory inside xampp/htdocs/myproject, because if i do so i can't composer install since composer.json can't be found. I am using xampp.
So i finaly found out what the issue was.
I simply had to use the change director with cd C:\whateverpath\totheprojectdir and then install the composer on whatever the project was on.

How can i change to location where composer creates the vendor folder

I have project's folder dummyProject and inside of it two folders, src and test, and composer.json file. Problem is when i run the composer install command the vendor folder being created in the dummyProject folder. How can i tell composer to create the vendor folder inside src?? thx..
According to the following: https://groups.google.com/forum/#!topic/composer-dev/CS_I0FNMHwY
You can set a Vendor Path like so:
{
"config": {
"vendor-dir": "libs/"
}
}
Looking at this question How to get multiple vendor directories with composer?, it appears you cannot set two different paths for vendor files (not sure if you will need to do this also), but the questions shows that with a symlnk this can be achieved.
Also see the following regarding changing the vendor location:
How to specify Composer install path?

Where exactly does laravel require the illuminate folder?

Looking at the laravel composer.json it seems to autoload the app directory but not the laravel illuminate framework.
It is listed as a require in the composer file. So ok, you can do a composer install and it will pull in the framework to the vendor directory. But where does the laravel app require the illuminate framework now for usage? Maybe I'm lacking composer knowledge here but I can't figure it out.
Looking at the illuminate environment it seems to come with it's own composer file that autoloads its entire folder.
I'm trying to look at the laravel structure because I'm currently working on a little project of my own with a src directory and an app directory but I can't seem to autoload both folder with something like this:
{
"autoload": {
"psr-4": {
"Cinematix\\": "src",
"App\\": "app"
}
}
}
Should this be able to work? If not, how would I implement something like this? Make a php package of my src folder myself?
Composer creates vendor/autoload.php which is then required by Laravel in bootstrap/autoload.php.
The way composer works is when you update or install a package it will regenerate that file by scanning each of the packages composer.json files, so theres conveniently just 1 file you need to include in your project to load all of your dependencies.
As for your own package, what you have there should work. Have you run composer dump-autoload after updating your composer.json?

running phpspec from windows CMD creates the spec and src folders in vendor/bin instead of the project root

Lately I've been learning about phpspec and gulp and decided to integrate them in my workflow.
When I installed phpspec via composer, I get the file structure of:
vendor
bin
phpspec
there are more files but and folders but these are the relevant.
In my CMD I'm currently pointing the root of my project and trying to run:
vendor/bin/phpspec
However, I get an error saying 'vendor' isn't recognizes as internal or external command...
so I moved into my bin folder by doing cd vendor/bin and then run phpspec and it indeed works. However, when I try to describe a class phpspec describe SeatsHandler it creates my files not in the root directory it creates them inside the vendor/bin folder:
vendor
bin
phpspec
spec
src
I tried to see how I can configure that and read about phpspec.yml. I've created such file along with app directory in my main folder (would love to get rid of app directory):
suites:
app_suite:
src_path: app
I tried putting this file in my main directory, but it didn't work, I got the same result as before. I also tried moving this file into vendor/bin but no avail.
How can I make phpspec create the files in my root directory instead of inside vendor/bin? Also, why does it ignore the phpspec.yml?
EDIT:
I managed to make it work by modifying my phpspec.yml into:
suites:
app_suite:
src_path: ../../src/
spec_path: ../../
And put it in vendor/bin. However, a question still remains, how can I run phpspec without going inside the vendor/bin directory in CMD?
in your composer.json file, just add the "bin-dir": "bin" under config like so:
{
"require-dev": {
"phpspec/phpspec": "~2.0"
},
"config": {
"bin-dir": "bin"
},
"autoload": {"psr-0": {"": "bin/src"}}
}
and youll get a bin folder on your root directory along with the vendor directory.
bin
vendor
then you can run phpspec on your root directory exactly as you want.
You can install it globally as following;
composer global require phpspec/phpspec
now you can call it phpspec

where do I put composer.json

I have installed composer.
My project dir tree looks something like this
/home/myproject/public_html/myproject.com
I initially installed it in:
/home/myproject/public_html/myproject.com/bin/composer/
But later moved it to:
/home/myproject/usr/local/bin/composer
Questions:
Where to I create composer.json ?
In the official docs they mention that in order to install new packages I need to write a require key in the json format in that file, does this mean that I dont have to upload the package through ftp?
The docs further say that I can simply install dependencies like ths:
php composer.phar install
I dont understand the workflow of this process (im fairly new).. what exactly do I need to do to get some packages going (like Respect)
Composer has 2 basic elements for you to consider:
The composer.php file itself - this can be located anywhere on your system - usually it is convenient to have it in you search path so you can invoke it by name (no path) from the command line.
Composer.json - this file is the configuration for your project. This is usually best located at the top level of your project. Ideally this is a directory outside the scope of your web server - so that it will never be exposed or served.
Symfony2 has some great documentation and examples of composer in use.
Also be aware that some packages you reference via composer will themselves have composer files - to ensure they match your required dependancies - and they may also have their own dependancies that need to be considered.
I would install composer.json in the following
/home/myproject/composer.json
It would be out of scope of the web server and could be used to manage many assets e.g.
public_html/
libs/
config/
docs/
vendor/
Where to I create composer.json ?
You should create composer.json to your project root like /home/myproject/public_html/myproject.com/composer.json. If all files of your application live inside your myproject.com folder.
In the official docs they mention that in order to install new
packages I need to write a require key in the json format in that
file, does this mean that I dont have to upload the package through
ftp?
Yes as long as you're not in shared hosting because most of them don't allow CLI (SSH).
The docs further say that I can simply install dependencies like this
php composer.phar install
Yes you can simple type the above command and composer.json will install the latest version of your package.
Composer.json (Respect Package)
{
"require": {
"respect/validation": "dev-master"
}
}
Now run composer install will install the require package.
For further packages
{
"require": {
"respect/validation": "dev-master",
"doctrine/orm": "2.*"
}
}
Now run update composer update it will download the doctrine/orm as well.

Categories