In my Laravel Project, I want to use Github project having just 1 file.
No composer package included.
This file have some formulas to calculate the result and have 7 functions.
My Question:
Where to save this file in my project?
Or
Can I directly copy those functions in my controller/Model?
Please suggest.
Usually when I include code in laravel that is not part of a composer package I create a folder in the App folder with my nickname or companyname:
/project_folder/app/company/helperfunctions.php
You can still use composer to autoload the file if you want. Just include the classmap in your composer.json and add the path to your folder
"autoload": {
"classmap": [
"app/company"
],
},
Related
I have a Laravel package I would like to use in my project. I have tested it already and I know I'd need to customize quite a lot of it to fit my needs.
Overriding files in the vendor directory is always quite a chore, especially if you need to touch PHP, Vue, CSS files, etc. Hence, I don't want to go via the usual Composer installation. I also don't care much about future updates to the package, as it already has a solid base.
So the question is: After I download the package files from GitHub and after I place them in their respective directories in my Laravel app - is there a specific list of steps to take in order to make sure the inserted package works? Or is it more like, do it and fix issues one by one until it works?
Cheers.
Composer allows you to choose the local package instead of the remote one.
Create a packages folder in the base directory (the packages folder and Laravel's composer.json file must be in the same directory).
Put your downloaded package into packages (packages/packageName).
Replace the following line in your Laravel's composer.json.
"require": {
by
"repositories": [
{
"type" : "path",
"url" : "./packages/*",
"options" : {
"symLink" : true
}
}
],
"require": {
Run composer require authorName/packageName command in the console.
I have downloaded a library with composer. Now I want to require that file from vendor directory in my controller. So how do I require that file.
To be more specific I want to require this library
https://github.com/jumbojett/OpenID-Connect-PHP
Once composer is done downloading your libraries it generates the autoload namespaces for your app.
To check what the generated autoload namespace for a library will be just look at its composer.json file.
"autoload": {
"classmap": ["OpenIDConnectClient.php"]
}
and the generated autoload map will be in vendor/composer/autload_psr4.php
Here is the relevant part for your library.
Then open vendor/composer/autoload_namespaces and check what is the actual namespace generated by composer.
when composer.json has the 'classmap' key it basically means that you will access the librabry via '\LibraryName.phar' for example.
When it has the 'psr-4' key it means that you will access your library from the namespace specified there
Example:
"autoload": {
"psr-4": {
"Blast\\BaseEntitiesBundle\\": ""
}
},
You just need to require "vendor/autoload.php" all packages namespaces will be available for you to use any where.
I would like to use following GitHub repo in my Laravel application:
https://github.com/khanamiryan/php-qrcode-detector-decoder
It doesn't have composer set up nor it can be found from Packagist. I tried to use regular php_require but it tells me "Class 'App\Http\Controllers\QrReader' not found".
Using php_require feels wrong anyways. What is the correct way to handle situation like this?
Create a new directory in your app root
mkdir third-party
cd third-party
Clone the repo
git clone https://github.com/khanamiryan/php-qrcode-detector-decoder
Edit your composer.json file and add it to the classmap:
"classmap": [
"database",
"third-party/php-qrcode-detector-decoder"
],
Update class maps:
composer dumpautoload
And you should see in your vendor/composer/autoload_classmap.php
'Zxing\\Binarizer' => $baseDir . '/third-party/php-qrcode-detector-decoder/lib/Binarizer.php',
'Zxing\\BinaryBitmap' => $baseDir . '/third-party/php-qrcode-detector-decoder/lib/BinaryBitmap.php',
...
Then you just have to use it:
use Zxing\Reader;
I think the major class is QrReader(). You can use this class as controller class but you need to extend the controller class and fix imports. You can import this class as third party class on your laravel controller too.
Do you need to use QrReader() class?
Then just put all the library files App\Libraries and the main class in App\classes. Or you can do in our own way too. But follow the following
1) Manage namespaces
2) import the class to your controller by using
use App\classes\QrReader
Finally, you will have access to all the methods defined in the imported class. But in you main class you need to correct the path and dependencies of libraries files.
You can try this tutorial too:
How to use external classes
You can read the discussion here (Nice)
Best way to import third party classes
Because this github project does not have a composer.json file, I don't think you can use it with composer.
However, you could branch the repo, make your own copy, and add a composer file to it. Then you'd be able to add it to your main project's composer.json file:
{
"repositories": [
{
"type": "git",
"url": "https://github.com/your-git-account/php-qrcode-detector-decoder"
}
],
"require": {
"your-git-account/php-qrcode-detector-decoder": "dev-master"
}
Hope this helps!
(Source https://getcomposer.org/doc/05-repositories.md#loading-a-package-from-a-vcs-repository)
I am attempting to create a sort of custom directory structure, my proposed structure is as follows
App/ - Contains all Laravel core code
Repo/ - Contains packages, each package contains Controllers, Views, Modals, Seeds and Migrations specific to that package
Is it possible via Composer or would it take a lot of core modification?
Controller routing in routes.php
Route::resource('account', '\Repo\Accounts\Accounts');
The first occurrence of accounts is the folder and the second being the class. I know I could write each directory seperetly then dump composer autoload, however when you have 30 seperate packages per app, it is a little time consuming. Am I missing something super straight forward?
This is possible with composer. Add inside of your composer.json:
"autoload": {
"classmap": [
// ...
],
"psr-4": {
"Repo\\" : "Repo"
}
},
Then you can use classes inside of the /Repo directory, and classes in there will reside in the \Repo namespace.
Assume I have folder layout as
projectA
projectA\src
projectA\vendor\autoload.php
projectB
projectB\src
projectB\vendor\autoload.php
projectC
projectC\src
projectC\vendor\autoload.php
These projects need to be in the same level and they need to coexist with each others, e.g. projectA might use codes from projectB and projectC and vice vera, so they cannot be placed into the vendor folder.
The thing is: the autoload.php in each project is able to autoload their own src and vendor folder, but how to autoload for the others as well?
Assume their neighbor's project will have the folder name as the PHP namespace, is it possible to setup a autoload.php (via composer) such that in the future when I add new project folder, the autoload will magically work?
You can write in each project a composer.json with custom autoload configuration..
examples:
ProjectA:
"autoload": {
"psr-0": {
"ProjectB\\": "path/ProjectB/src/",
"ProjectC\\": "path/ProjectC/src/"
}
},
ProjectB:
"autoload": {
"psr-0": {
"ProjectA\\": "path/ProjectA/src/",
"ProjectC\\": "path/ProjectC/src/"
}
},
Composer is conceived for manage dependencies of single project.. Load more autoload.php of different projects is not a good idea..
but with this method you can create a complete autoloader for each projects