Where exactly does laravel require the illuminate folder? - php

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?

Related

Source from vendor loaded and compiled as files from src directory

I am trying the new Symfony4 (framework bundle) and I wanted to move some my reusable classes from src directory to vendor one (loaded via composer PSR-4).
So I created a package (new git repository), added classes there (into src directory), added PSR-4 configuration into package's composer.json like this:
"autoload": {
"psr-4": { "\\": "src/" }
}
and I ran composer require <name_of_my_package> and I dumped autoload.
All went smooth, but when I wanted to access any class from my package - for my surprise - Symfony cannot find and use any of these classes (even though they are in autoload files of composer).
I think that I need to somehow add these classes to Symfony container builder, but I am not sure how.
Thanks for all help.

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.

Laravel 5.1 Package Development - loading packages dependencies in development

Hi I'm trying to develop a package in Laravel 5.1. Thanks to help here I have the basics set up.
My current problem is how to load dependencies for the package while I'm developing it.
In the packages composer.json I have added dependencies and have these installed now in a vendor folder within my packages development folder. This is not the frameworks root vendor folder.
Here's my require section of the packages composer.json:
"require": {
"illuminate/support": "~5.1",
"php" : ">=5.3.0",
"google/apiclient": "dev-master"
},
Because they are not part of the main autoload process what is the best approach to ensuring the dependencies for my package are loaded correctly from within the development folder? How do I include the autoload? I'm concerned that if I reference them to their current location/namespace that it will break when later installed as a package in another app.
in my code I have the following:
$client = new \Google_Client($config);
which gives the error:
Class 'Google_Client' not found
I can get round this by adding this dependency to the main composer.json but don't think that is the correct approach to keep the package development independent (if that makes sense)
When I developed in L4.2 there was the workbench which took care of the loading which of course no longer features in L5.1
Any help and best practice appreciated
Because they are not part of the main autoload process
I think you misunderstood how composer dependencies are managed. When in your main compose.json file you list a dependency, composer will add it to the main autoload process as well as all their dependencies, and the dependencies of their dependencies, and so on recursively.
You don't have to worry about where the dependencies are stored or how Composer will load them. Composer will automatically add them to the autoload file and all you have to do is make sure you require the composer autoload file. Once you require the composer autoload file, all the classes and functions loaded by composer will be available. Provided you required the composer autoload file all you have to do to use the classes from any of the installed packages is to make sure you address them using the proper namespace. Composer is smart enough to know where all classes are stored and how to load them (that is what psr-0, psr-4,... are for).
So if you are developing a Composer package, lets call it 'A', and you list the package 'C' as one of the dependencies of your package 'A', composer will add it to the autoload file for you. If you use another package, lets say, Laravel, which has a dependency of you package 'A', then also the package 'C' will be available within Laravel, since it is a dependency of 'A'.
I.e: If this is your composer.json file
{
"name": "foo/bar",
"require": {
"google/apiclient": "1.0.*"
}
}
This code will work
require_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$youtube = new Google_Service_YouTube($client);
Note I've required the composer autoload file, which seems to be your problem. When you are using Laravel, it will add that file for you.

Custom composer autoload directory, loading multiple packages

Im developing a group of packag within a Laravel 5 app.
all the packages have the following structure.
Folder structure:
Vendor/Package/src
composer.json
"autoload": {
"psr-4": {
"Vendor\\Package\\": "src/"
}
},
I would like to be able to have a separate folder outside of the vendors/ folder that contains the packages, under development. As they are the basis for the application. NOTE: These packages will not be downloaded through composer, just auto-loaded with composer.
e.g.
\Laravel-App
\app
\bootstrap
\config
\database
\public
...
\src
The \src directory containing all the packages mentioned above.
It's not relevant that there is a composer.json inside each package, because the are not managed by composer (, yet). Their autoload definition will only be used for an autoload dump by Composer, if the package is installed/updated.
Here there are not handled by Composer, just sitting around in a folder.
There are multiple ways of solving the autoloading problem.
You could:
use the PHP autoload functions to register your own autoloader (during bootstrap)
use Laravel's Autoloader to add the path to these packages (during app bootstrap, somewhere after laravel autoloader init)
use Composer's Autoloader to add the paths to these packages (during app bootstrap, after Composer init)
but the easiest way would be to define the mapping in the autoload section of the composer.json file of your main application. Then they will be included in the dumped autoloader, after install or update of your Composer managed dependencies.

Putting composer.json in the root of a CakePHP installation

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

Categories